Skip to content

Commit edb93aa

Browse files
committed
EPI: count_bits
1 parent dc25880 commit edb93aa

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

elements-of-programming-interviews/cpp/count_bits.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#include "test_framework/generic_test.h"
22

33
short CountBits(unsigned int x) {
4-
// TODO - you fill in here.
5-
return 0;
4+
short count = 0;
5+
for (int i = 0; i < sizeof(int) * 8; i++) {
6+
if (x & 1) {
7+
count++;
8+
}
9+
x >>= 1;
10+
}
11+
return count;
612
}
713

814
int main(int argc, char* argv[]) {

elements-of-programming-interviews/problem_mapping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ problem_mapping = {
22
"Chapter 04: Primitive Types": {
33
"4.00 Bootcamp: Primitive Types": {
44
"C++: count_bits.cc": {
5-
"passed": 0,
5+
"passed": 10001,
66
"total": 10001
77
},
88
"Java: CountBits.java": {

0 commit comments

Comments
 (0)