diff --git a/190-reverse-bits/driver.cpp b/190-reverse-bits/driver.cpp new file mode 100644 index 0000000..c3f4230 --- /dev/null +++ b/190-reverse-bits/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + uint32_t n = 43261596; // 00000010100101000001111010011100 + uint32_t answer = 964176192; // 00111001011110000010100101000000 + Solution soln; + cout << "Reversed bits correctly? " << soln.test(n, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/190-reverse-bits/soln.cpp b/190-reverse-bits/soln.cpp new file mode 100644 index 0000000..a45f959 --- /dev/null +++ b/190-reverse-bits/soln.cpp @@ -0,0 +1,18 @@ +#include "soln.hpp" + +uint32_t Solution::reverseBits(uint32_t n) +{ + uint32_t result = 0; + for (int i = 0; i < 32; i++) + { + result = result << 1; // increase result size + result += (n & 1); // append last bit + n = n >> 1; // move to next bit + } + return result; +} + +bool Solution::test(uint32_t n, uint32_t answer) +{ + return reverseBits(n) == answer; +} \ No newline at end of file diff --git a/190-reverse-bits/soln.hpp b/190-reverse-bits/soln.hpp new file mode 100644 index 0000000..f9b853a --- /dev/null +++ b/190-reverse-bits/soln.hpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +class Solution +{ +public: + uint32_t reverseBits(uint32_t n); + bool test(uint32_t n, uint32_t answer); +}; \ No newline at end of file