Skip to content

Instantly share code, notes, and snippets.

@qqwqqw689
Created May 16, 2024 15:28
Show Gist options
  • Save qqwqqw689/88bb8dc964ba923de3cddb1e50fb7256 to your computer and use it in GitHub Desktop.
Save qqwqqw689/88bb8dc964ba923de3cddb1e50fb7256 to your computer and use it in GitHub Desktop.
A function to find the next power of two greater than a given number.
#include <iostream>
#include <bitset>
typedef unsigned int uint;
static uint nextPowerOfTwo(uint x) {
std::bitset<32> a(x);
std::cout << a << '\n';
--x;
a = x;
std::cout << a << '\n';
x |= x >> 1;
a = x;
std::cout << a << '\n';
x |= x >> 2;
a = x;
std::cout << a << '\n';
x |= x >> 4;
a = x;
std::cout << a << '\n';
x |= x >> 8;
a = x;
std::cout << a << '\n';
x |= x >> 16;
a = x;
std::cout << a << '\n';
return ++x;
}
int main()
{
nextPowerOfTwo(1025);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment