• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

C++ Program to Count the Number of Set Bits in a Number

Om Tayade/771/0
C / C++Code

The easy solution to this problem is to convert the given number into a binary string. Loop over all the characters in the string. A number of ones in the string are nothing but the total set bits.

But, we want to find the optimal solution to this problem. We can use the AND operation to find the number of set bits. I will explain to you how you can do that.

Explanation:

Have you observed one thing? If we ‘bitwise AND’ a number n with a number (n-1) then the rightmost set bit or a least significant set bit of a number n is unset or set to zero.

Let’s understand it with the example:

n=6 (0110)

n-1 = 5 (0101)

n = n & n-1  (0100) 

Here we can see the rightmost set bit (3rd bit) of 6 is unset after performing logical AND with 5.

So using this trick we can calculate the number of set bits in the number.

Algorithm and Approach:

  1. Set count=0.
  2. Perform the logical AND between given n and n-1. (says result=a)
  3. Increment count by one.
  4. If a is not zero, follow step 1. Else, print count.

If you understand the logic, you can implement it with any programming language of your choice like C/C++, Java, Python, etc.

This trick is very handy while solving competitive coding challenges.

C++ Code:

#include <iostream>
using namespace std;
 
int main()
{
    int n = 6 , count=0;

    while(n){
       n = n & n-1 ;
       count++;
    }

    cout<<"The number of set bits: "<<count
    return 0;
}

Output:

The number of set bits: 2

To improve your bit manipulation skills, check the set of bit manipulation interview questions in data structure coding questions.

Om Tayade
I'm pursuing B.Tech in Computer Science from Pune Institute of Computer Technology. I'm avid learner and enthusiastic programmer.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard