Created
May 16, 2024 15:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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