Back
Close

Binary count

Statement

 Goal

Count how many times the given value 0 or 1 B occurs in the binary representation of a number N.
Input
Line 1: N the number to parse (in base-10).
Line 2: B the bit to count.
Output
Line 1: the number of times B occurs in base-2 N.
Constraints
0 ≤ N: int ≤ 1,000,000
B: either 0 or 1
Example
Input
5
1
Output
2

Game modes
Fastest, Shortest

Test cases
Test 1 Test
Input
5 1
Output
2

Validator 1 Validator
Input
21 1
Output
3

Test 2 Test
Input
3 1
Output
2

Validator 2 Validator
Input
7 1
Output
3

Test 4 Test
Input
3 0
Output
0

Validator 4 Validator
Input
7 0
Output
0

Test 5 Test
Input
10 1
Output
2

Validator 5 Validator
Input
42 1
Output
3

Test 6 Test
Input
10 0
Output
2

Validator 6 Validator
Input
42 0
Output
3

Test 7 Test
Input
4242 1
Output
4

Validator 7 Validator
Input
4242 0
Output
9

Test 8 Test
Input
8484 0
Output
10

Validator 8 Validator
Input
8484 1
Output
4

Test 9 Test
Input
987654321 1
Output
17

Validator 9 Validator
Input
987654320 1
Output
16

Test 10 Test
Input
987654321 0
Output
13

Validator 10 Validator
Input
987654320 0
Output
14

Solution language

Solution

Stub generator input