Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3d3b9e

Browse files
committed
"Number of 1 Bits": add method to count 0 bits
1 parent d163551 commit e3d3b9e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

‎src/191.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ int hammingWeight_2(uint32_t n) {
3535
ret ++;
3636
}
3737
return ret;
38-
}
38+
}
39+
40+
/*****************************
41+
Code to count 0 bits
42+
43+
while (n != 0xFFFFFFFF) {
44+
n = n | (n + 1);
45+
ret++;
46+
}
47+
48+
Or,
49+
50+
n = ~n;
51+
count1Bits(n);
52+
53+
******************************/
3954

4055
/* 5 steps */
4156
int hammingWeight_3(uint32_t n) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /