
- 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 an integer in C++
We are given an integer number let’s say, num and the task is to firstly calculate the binary digit of a number and then calculate the total set bits of a number.
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
Output − Count of total set bits in a number are − 3
Explanation − Binary representation of a number 50 is 110010 and if we calculate it in 8-digit number then two 0’s will be appended in the beginning. So, the total set bits in a number are 3.
Input − int number = 10
Output − Count of total set bits in a number are − 2
Explanation − Binary representation of a number 10 is 00001010 and if we calculate it in 8-digit number then four 0’s will be appended in the beginning. So, the total set bits in a number are 2.
Approach used in the below program is as follows
Input the number in a variable of integer type
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
Print the count
Example
#include<iostream> using namespace std; //Count total set bits in a number unsigned int bits(unsigned int number){ 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 set bits in a number while (number){ count += number & 1; number >>= 1; } cout<<"\nCount of total set bits in a number are: "<<count; } int main(){ int number = 50; bits(number); return 0; }
Output
If we run the above code it will generate the following output −
8-bit digits of 50 is: 00110010 Count of total set bits in a number are: 3
- Related Articles
- Python Program to Count set bits in an integer
- Java Program to Count set bits in an integer
- C/C++ Program to Count set bits in an integer?
- Golang Program to count the set bits in an integer.
- C/C++ Program to the Count set bits in an integer?
- Shift the bits of an integer to the right and set the count of shifts as an array with signed integer type in Numpy
- Shift the bits of an integer to the left and set the count of shifts as an array in Numpy
- Python Count set bits in a range?
- Sort an array according to count of set bits in C++
- Count set bits in a range in C++
- Count set bits using Python List comprehension
- Count pairs in an array such that both elements has equal set bits in C++
- C# program to count total set bits in a number
- Previous smaller integer having one less number of set bits in C++
- Next greater integer having one more number of set bits in C++
