day 10: 190-reverse-bits

so far was on easy, moving to medium difficulty
This commit is contained in:
Kaushik Narayan R 2023-10-07 10:31:48 -07:00
parent 01f1ea79b0
commit 692280761f
3 changed files with 38 additions and 0 deletions

View File

@ -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;
}

18
190-reverse-bits/soln.cpp Normal file
View File

@ -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;
}

10
190-reverse-bits/soln.hpp Normal file
View File

@ -0,0 +1,10 @@
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
uint32_t reverseBits(uint32_t n);
bool test(uint32_t n, uint32_t answer);
};