Skip to content

Instantly share code, notes, and snippets.

@Fuitad
Forked from araipiyo/scramble.rb
Created September 6, 2016 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fuitad/5f422396aadc5c54ea8d13a516936fb5 to your computer and use it in GitHub Desktop.
Save Fuitad/5f422396aadc5c54ea8d13a516936fb5 to your computer and use it in GitHub Desktop.
def trim(v)
v & 0xffffffff
end
def scramble(v)
v ^= 0x1ca7bc5b
v *= 0x1ca7bc5b
v = trim(v)
v = ((v >> 1) & 0x55555555) | trim((v & 0x55555555) << 1)
v = ((v >> 2) & 0x33333333) | trim((v & 0x33333333) << 2)
v = ((v >> 4) & 0x0F0F0F0F) | trim((v & 0x0F0F0F0F) << 4)
v = ((v >> 8) & 0x00FF00FF) | trim((v & 0x00FF00FF) << 8)
v = ( v >> 16 ) | trim( v << 16)
v *= 0x6b5f13d3
v = trim(v)
v ^= 0x1ca7bc5b
end
def test
0.step(0xffffffff,10000) { |x|
raise unless x == scramble(scramble(x))
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment