• 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 Number of Ones in Binary [Set bits in an Integer]

Aniruddha Chaudhari/46875/0
C / C++Code

Problem Statement: You have given an integer number. You have to write a C Program to Count Number of Ones in Binary representation of a given integer number.

Note: One’s bit is also called as the set bit.

Suppose you have integer value 13. 
Its binary representation is 1101. 
So the number of set bits in 13 is 3.

Takes another example.

Suppose the number is 205. 
Its equivalent binary number is 11001101. 
The total number of set bits in 205 is 5.

Note: To convert the number from decimal to integer you can check out the online tool for Decimal to Binary.

Once you get the Binary equivalent to the decimal number, your task is…

How to count a number of ones in binary?

This question has been asked in the Riverbed interview and in many other interviews. It is somewhat tricky.

We know all, the computer (or any machine) only knows binary data i.e one or zero, on or off. The data we work on the computer is actually stored and retrieved in the binary form. So it is necessary many times to manipulate binary data. One of the clever examples is counting set bits in an integer value.

I kindly request you to minimize this window and try to get your logic for this question. If you don’t get any solution you can check the code below.

C Program to Count Number of Ones in Binary:

#include<stdio.h>
void main()
{
	int i=0;
	int nNum;
	int nCount=0;
	printf("\nEnter the number: ");
	scanf("%d",&nNum);
	for(;i<(sizeof(int)*8);i++) { if(1&nNum) nCount++; nNum = nNum>>1;
	}
	printf("\nThe number of set bits are %d \n", nCount);
}


Here we are using right shift << bit operator.

How does the Right shift “<<” operator work?

It shifts the one-bit of the number to its right and adds zero bit at the left side.

The number 11001101 will become 01100110 after the one-bit right operation.

The output of the program:

Enter the number: 205
The number of set bits are 5.

C Program to Count Number of Ones in Binary

If you are new to the C programming you can check out the commands for compiling and executing C code.

Let’s take another challenge…

If you want to explore and learn more about, bit operations, here is another challenge for you.

  • How to find 2’s complement of a number using shift operation.

This is the easiest method to calculate 2’s complement of any number. You should know this method if you want to do binary calculations very quickly.

If you have any questions understanding bit operation or regarding this challenge to count number of ones in binary number, write in a comment below.

Happy Programming!

bit manipulationcpp
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

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