reverse-bits
This commit is contained in:
parent
fe0b56c447
commit
d4614dbe6a
24
reverse-bits/main.cpp
Normal file
24
reverse-bits/main.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <inttypes.h>
|
||||
#include <iostream>
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user