Count Bits in Range
EasyPractice library · +10 XPbit manipulation
Given two non-negative integers, L and R, compute the total number of set bits (1s) in the binary representation of all integers from L to R (inclusive).
Input
A single line containing two space-separated integers, L and R.
Output
A single integer representing the total count of set bits.
Constraints
0 <= L <= R <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests
Example 1
Input
2 5
Output
6
Binary representations: 2 (10), 3 (11), 4 (100), 5 (101). Set bits: 1 + 2 + 1 + 2 = 6.
Example 2
Input
0 1
Output
1
Binary representations: 0 (0), 1 (1). Set bits: 0 + 1 = 1.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
2 5
Example 2 input
0 1