Learn
Practice
Newsletter
Resources
Animations
New
F
Toggle theme
0
F
0
Toggle menu
← Back to All Animations
Number of 1 Bits
Bookmark
Input
Example 1: 1011
Example 2: 10000000
Example 3: 11111111
Custom
n
=
11
n
0
0
0
0
1
0
1
1
7
6
5
4
3
2
1
0
count = 0
algo
master
.
io
Step:
n = 11 (00001011). Count the 1-bits: n & (n-1) clears the lowest set bit, so loop until n = 0
0 / 11
Input
Example 1: 1011
Example 2: 10000000
Example 3: 11111111
Custom
n
=
11
0 / 11
n
0
0
0
0
1
0
1
1
7
6
5
4
3
2
1
0
count = 0
algo
master
.
io
Step:
n = 11 (00001011). Count the 1-bits: n & (n-1) clears the lowest set bit, so loop until n = 0