Skip to content

Commit e79e038

Browse files
committed
reverse_bits.cc
1 parent f79e33b commit e79e038

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
#include "test_framework/generic_test.h"
2+
3+
void printBin(unsigned long long val, size_t len, const std::string& suffix) {
4+
std::string binval;
5+
for (size_t i = 0; i < len; i++) {
6+
binval.append(val & 1 ? "1" : "0");
7+
val >>= 1;
8+
}
9+
reverse(binval.begin(), binval.end());
10+
printf("%s (%s)\n", binval.c_str(), suffix.c_str());
11+
}
12+
213
unsigned long long ReverseBits(unsigned long long x) {
3-
// TODO - you fill in here.
4-
return 0;
14+
unsigned long long y = x & 1;
15+
for (int i = 0; i < (sizeof(x) * 8); i++) {
16+
y <<= 1;
17+
if (x & 1) {
18+
y |= 1;
19+
}
20+
x >>= 1;
21+
}
22+
return y;
523
}
624

725
int main(int argc, char* argv[]) {
@@ -10,3 +28,6 @@ int main(int argc, char* argv[]) {
1028
return GenericTestMain(args, "reverse_bits.cc", "reverse_bits.tsv",
1129
&ReverseBits, DefaultComparator{}, param_names);
1230
}
31+
32+
// 0000101101000100011001000110101010111001000000000000000000000000
33+
// 000010110100010001100100011010101011100100000000000000000000000

elements-of-programming-interviews/problem_mapping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ problem_mapping = {
4444
},
4545
"4.03 Reverse bits": {
4646
"C++: reverse_bits.cc": {
47-
"passed": 0,
47+
"passed": 10000,
4848
"total": 10000
4949
},
5050
"Java: ReverseBits.java": {

0 commit comments

Comments
 (0)