Skip to content

Instantly share code, notes, and snippets.

@tkouba
Created February 17, 2017 12:28
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 tkouba/738fcc6b631d955b9db3181067793ca2 to your computer and use it in GitHub Desktop.
Save tkouba/738fcc6b631d955b9db3181067793ca2 to your computer and use it in GitHub Desktop.
Reverse UInt64 with bits width of groups. Reverse bits (bits = 1), reverse half-bytes (bits = 4, same as reverse hexadecimal string value).
private static UInt64 ReverseUInt64(UInt64 x, int bits = 1)
{
UInt64 b = 0;
UInt64 mask = 0;
for (int i = 0; i < bits; i++)
{
mask = (mask << 1) | 0x1;
}
for (int i = 0; i < 64 / bits; i++)
{
b = ((b << bits) | (x & mask));
x = (x >> bits);
}
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment