Skip to content

Instantly share code, notes, and snippets.

@araipiyo
Created March 10, 2015 10:53
Show Gist options
  • Save araipiyo/05c56d5e5d6ce87d5dc8 to your computer and use it in GitHub Desktop.
Save araipiyo/05c56d5e5d6ce87d5dc8 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
@randsina
Copy link

Lol comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment