Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← 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
n
0
0
0
0
1
0
1
1
7
6
5
4
3
2
1
0
count = 0
n
= 11
0
0
0
0
1
0
1
1
7
6
5
4
3
2
1
0
count = 0
n
= 11
0
0
0
0
1
0
1
1
7
6
5
4
3
2
1
0
n
0
0
0
0
1
0
1
1
= 11
& n-1
0
0
0
0
1
0
1
0
= 10
=
0
0
0
0
1
0
1
0
= 10
count = 0
n
= 10
0
0
0
0
1
0
1
0
7
6
5
4
3
2
1
0
count = 1
n
= 10
0
0
0
0
1
0
1
0
7
6
5
4
3
2
1
0
n
0
0
0
0
1
0
1
0
= 10
& n-1
0
0
0
0
1
0
0
1
= 9
=
0
0
0
0
1
0
0
0
= 8
count = 1
n
= 8
0
0
0
0
1
0
0
0
7
6
5
4
3
2
1
0
count = 2
n
= 8
0
0
0
0
1
0
0
0
7
6
5
4
3
2
1
0
count = 2
n
= 0
0
0
0
0
0
0
0
0
7
6
5
4
3
2
1
0
count = 3
n
= 0
0
0
0
0
0
0
0
0
7
6
5
4
3
2
1
0
count = 3
n
= 0
0
0
0
0
0
0
0
0
7
6
5
4
3
2
1
0
count = 3
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
algo
master
.
io
Step:
n = 11 (00001011). Count the 1-bits: n & (n-1) clears the lowest set bit, so loop until n = 0