Given a large binary number.The task is to count the number of **1’s** in a given range from **L** to **R** (1 based indexing).

**Examples:**

Input :s = “101101011010100000111”, L = 6, R = 15

Output :5

s [L : R] = “1011010100”

There is only 5 set bits.

Input :s = “10110”, L = 2, R = 5

Output :2

**Approach:**

- Convert the string of size
**len**to the bitset of size**N**. - There is no need of (N – len) + (L – 1) bits in the left side and (N – R) bits in the right side of the bitset .
- Remove those bits efficiently using left and right shift bitwise operation.
- Now there are all zeroes in the left side of L and right side of R, so just use count() function to get the count of 1’s in the bitset as all positions except [L, R] are ‘0’.

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 32 ` ` ` `// C++ function to count ` `// number of 1's using bitset ` `int` `GetOne(string s, ` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `int` `len = s.length(); ` ` ` ` ` `// Converting the string into bitset ` ` ` `bitset<N> bit(s); ` ` ` ` ` `// Bitwise operations ` ` ` `// Left shift ` ` ` `bit <<= (N - len + L - 1); ` ` ` ` ` `// Right shifts ` ` ` `bit >>= (N - len + L - 1); ` ` ` `bit >>= (len - R); ` ` ` ` ` `// Now bit has only those bits ` ` ` `// which are in range [L, R] ` ` ` ` ` `// return count of one in [L, R] ` ` ` `return` `bit.count(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `string s = ` `"01010001011"` `; ` ` ` ` ` `int` `L = 2, R = 4; ` ` ` ` ` `cout << GetOne(s, L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `N ` `=` `32` ` ` `# function for converting binary ` `# string into integer value ` `def` `binStrToInt(binary_str): ` ` ` `length ` `=` `len` `(binary_str) ` ` ` `num ` `=` `0` ` ` `for` `i ` `in` `range` `(length): ` ` ` `num ` `=` `num ` `+` `int` `(binary_str[i]) ` ` ` `num ` `=` `num ` `*` `2` ` ` `return` `num ` `/` `2` ` ` ` ` `# function to count ` `# number of 1's using bitset ` `def` `GetOne(s, L, R) : ` ` ` ` ` `length ` `=` `len` `(s); ` ` ` ` ` `# Converting the string into bitset ` ` ` `bit ` `=` `s.zfill(` `32` `-` `len` `(s)); ` ` ` ` ` `bit ` `=` `int` `(binStrToInt(bit)) ` ` ` ` ` `# Bitwise operations ` ` ` `# Left shift ` ` ` `bit <<` `=` `(N ` `-` `length ` `+` `L ` `-` `1` `); ` ` ` ` ` `# Right shifts ` ` ` `bit >>` `=` `(N ` `-` `length ` `+` `L ` `-` `1` `); ` ` ` `bit >>` `=` `(length ` `-` `R); ` ` ` ` ` `# Now bit has only those bits ` ` ` `# which are in range [L, R] ` ` ` ` ` `# return count of one in [L, R] ` ` ` `return` `bin` `(bit).count(` `'1'` `); ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"01010001011"` `; ` ` ` ` ` `L ` `=` `2` `; R ` `=` `4` `; ` ` ` ` ` `print` `(GetOne(s, L, R)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- std::bitset::to_ullong and std::bitset::to_ulong in C++ STL
- Count number of trailing zeros in Binary representation of a number using Bitset
- Subset Sum Queries in a Range using Bitset
- Check if bits of a number has count of consecutive set bits in increasing order
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- bitset count() in C++ STL
- Print numbers having first and last bits as the only set bits
- Python | Count set bits in a range
- Range query for count of set bits
- Count total set bits in all numbers from range L to R
- Count numbers in range [L, R] having K consecutive set bits
- Count set bits in a range
- Toggle bits of a number except first and last bits
- bitset set() function in C++ STL
- Subset sum queries using bitset
- Set all the bits in given range of a number
- Smallest number whose set bits are maximum in a given range
- Count Set-bits of number using Recursion
- Check if all bits can be made same by flipping two consecutive bits
- Count unset bits in a range

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.