-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add reverse bits * impl reverse bits
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "TestHelper.h" | ||
#include "problem.h" | ||
#include "solution.h" | ||
|
||
using namespace std; | ||
|
||
IMPLEMENT_PROBLEM_CLASS(PReverseBits, 190, DIFFI_EASY, TOPIC_ALGORITHMS, | ||
"Reverse Bits", | ||
"Reverse bits of a given 32 bits unsigned integer.", | ||
{"Bit"}); | ||
|
||
class SReverseBits : public ISolution { | ||
public: | ||
size_t problemId() const override { return 190; } | ||
string name() const override { | ||
return ("Solution for " + string("Reverse Bits")); | ||
} | ||
string location() const override { return __FILE_NAME__; } | ||
int test() const override { | ||
return testHelper<uint32_t, uint32_t>( | ||
{0b00000010100101000001111010011100, | ||
0b11111111111111111111111111111101}, | ||
{0b00111001011110000010100101000000, | ||
0b10111111111111111111111111111111}, | ||
[this](auto input) { return this->reverseBits(input); }); | ||
}; | ||
int benchmark() const override { return 0; } | ||
|
||
private: | ||
uint32_t reverseBits(uint32_t n) const { | ||
const int N = 32; | ||
uint32_t res = 0; | ||
|
||
for (int i = 0; i < N; i++) { | ||
res = (res << 1) + ((n >> i) & 0x1); | ||
} | ||
|
||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters