Leetcode/cpp/190.cpp
2024-06-21 01:36:27 +01:00

26 lines
584 B
C++

#include <cstdint>
// #include <iostream>
class Solution {
private:
uint32_t setBit(uint32_t in, char value, char pos) {
uint32_t ones = -1;
uint32_t zero_mask = (ones ^ (value << pos));
in = in & zero_mask;
in = in ^ (value << pos);
return in;
}
public:
uint32_t reverseBits(uint32_t n) {
uint32_t out = 0;
for (char i = 0; i < 32; i++) {
out = setBit(out, n & 1, 31-i);
n >>= 1;
}
return out;
}
};
// int main() {
// std::cout << setBit(8, 1, 3) << std::endl;
// }