diff --git a/reverse-bits/main.cpp b/reverse-bits/main.cpp new file mode 100644 index 0000000..770635d --- /dev/null +++ b/reverse-bits/main.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +int main() { + //Reverse bits of an unsigned 32 bit integer + + std::cout << reverse_bits(43261596) << std::endl; + + return 0; +} + + +uint32_t reverse_bits(uint32_t n) { + uint32_t result = 0; + + // Starting from the end of the bit string, traverse backwards + for (int i = 31; i >= 0; i--) { + result |= (n & 1) << i; // Bitwise OR assign the + n >>= 1; + } + + return result; +} \ No newline at end of file