Skip to content

Instantly share code, notes, and snippets.

@crozone
Last active June 10, 2022 08:42
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 crozone/1d1468116ede32e35fe53a9784115ae4 to your computer and use it in GitHub Desktop.
Save crozone/1d1468116ede32e35fe53a9784115ae4 to your computer and use it in GitHub Desktop.
Get next largest power of two
public static class IntExtensions
{
public static uint GetNextPowerOfTwo(this uint input)
{
--input;
input |= input >> 1;
input |= input >> 2;
input |= input >> 4;
input |= input >> 8;
input |= input >> 16;
return input + 1;
}
public static ulong GetNextPowerOfTwo(this ulong input)
{
--input;
input |= input >> 1;
input |= input >> 2;
input |= input >> 4;
input |= input >> 8;
input |= input >> 16;
input |= input >> 32;
return input + 1;
}
}
@crozone
Copy link
Author

crozone commented Aug 26, 2021

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