- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count set bits in a range in C++

We are given an integer number let’s say, num and the range with left and right values. The task is to firstly calculate the binary digit of a number and then set the loop from the left digit till the right digit and then in the given range calculate the set bits.

Set bits in a binary number is represented by 1. Whenever we calculate the binary number of an integer value then it is formed as the combination of 0’s and 1’s. So, the digit 1 is known as set bit in the terms of the computer.

**Input** − int number = 50, left = 2, right = 5

**Output** − Count of total set bits in a range are − 2

**Explanation**-: Binary representation of a number 50 is 110010 and we have a range starting from left = 2 which is having bit as 1 and ending to right = 5 which is having bit 1 and in between the range we only have 0’s. So the count is 2.

**Input** − int number = 42, left = 3, right 4

**Output** − Count of total set bits in a range are − 1

**Explanation** − Binary representation of a number 42 is 101010 and we have a range starting from left = 3 which is having bit as 1 and ending to right = 4 which is having bit 0 and in between the range we only have one digit. So the count is 1.

## Approach used in the below program is as follows

Input the number in a variable of integer type and also the range with left and right integer values.

Declare a variable count to store the total count of set bits of type unsigned int

Start loop FOR from i to 1<<7 and i > 0 and i to i / 2

Inside the loop, check num & 1 == TRUE then print 1 else print 0

Start loop while to calculate the total count of bits till number isn’t 0

Inside the loop, set count = count + number & 1 and also set number >>=1

Set a temporary variable let’s say, a with ((1 << right) - 1) ^ ((1 << (left - 1)) - 1);

Also, set count with count & a

Print the count

## Example

#include<iostream> using namespace std; //Count total bits in a range unsigned int bits(unsigned int number, unsigned int left, unsigned int right){ unsigned int count = 0; unsigned i; //display the total 8-bit number cout<<"8-bit digits of "<<number<<" is: "; for (i = 1 << 7; i > 0; i = i / 2){ (number & i)? cout<<"1": cout<<"0"; } //calculate the total bits in a number while (number){ count += number & 1; number >>= 1; } //calculate the set bit in a range int a = ((1 << right) - 1) ^ ((1 << (left - 1)) - 1); count = count & a; cout<<"\nCount of total set bits in a range are: "<<count; } int main(){ unsigned int number = 42; unsigned int left = 2, right = 5; bits(number, left, right); return 0; }

## Output

If we run the above code it will generate the following output −

8-bit digits of 42 is: 00101010 Count of total set bits in a range are: 2

- Related Articles
- Python Count set bits in a range?
- Count unset bits in a range in C++
- Copy set bits in a range in C++
- Python program to count unset bits in a range.
- Count set bits in an integer in C++
- C# program to count total set bits in a number
- Program to count total number of set bits of all numbers in range 0 to n in Python
- Python Program to Count set bits in an integer
- Java Program to Count set bits in an integer
- How to count set bits in a floating point number in C?
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Count set bits using Python List comprehension
- C/C++ Program to Count set bits in an integer?
- Golang Program to count the set bits in an integer.
- Sort an array according to count of set bits in C++