Write an efficient program to count number of 1s in binary representation of an integer.

Examples

Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits

In the previous post we had seen different method that solved this problem in O(log n) time. In this post we solve in O(1) using lookup table. Here we assume that the size of INT is 32-bits. It’s hard to count all 32 bits in one go using lookup table (” because it’s infeasible to create lookup table of size 2^{32}-1 “). So **we break 32 bits into 8 bits of chunks( ow lookup table of size (2 ^{8}-1 ) index : 0-255 ).**

**LookUp Table **

In lookup tale, we store count of set_bit of every

number that are in a range (0-255)

LookupTable[0] = 0 | binary 00000000 CountSetBits 0

LookupTable[1] = 1 | binary 00000001 CountSetBits 1

LookupTable[2] = 1 | binary 00000010 CountSetBits 1

LookupTanle[3] = 2 | binary 00000011 CountSetBits 2

LookupTable[4] = 1 | binary 00000100 CountSetBits 1

and so…on upto LookupTable[255].

**Let’s take an Example How lookup table work.**

Let's number be : 354 in Binary : 0000000000000000000000101100010 Split it into 8 bits chunks : In Binary : 00000000 | 00000000 | 00000001 | 01100010 In decimal : 0 0 1 98 Now Count Set_bits using LookupTable LookupTable[0] = 0 LookupTable[1] = 1 LookupTable[98] = 3 so Total bits count : 4

`// c++ count to count number of set bits ` `// using lookup table in O(1) time ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Generate a lookup table for 32 bit integers ` `#define B2(n) n, n + 1, n + 1, n + 2 ` `#define B4(n) B2(n) \, B2(n + 1), B2(n + 1), B2(n + 2) ` `#define B6(n) B4(n) \, B4(n + 1), B4(n + 1), B4(n + 2) ` ` ` `// Lookup table that store the reverse of each table ` `unsigned ` `int` `lookuptable[256] = { B6(0), B6(1), B6(1), B6(2) }; ` ` ` `// function countset Bits Using lookup table ` `// ans return set bits count ` `unsigned ` `int` `countSetBits(` `int` `N) ` `{ ` ` ` `// first chunk of 8 bits from right ` ` ` `unsigned ` `int` `count = lookuptable[N & 0xff] + ` ` ` ` ` `// second chunk from right ` ` ` `lookuptable[(N >> 8) & 0xff] + ` ` ` ` ` `// third and fourth chunks ` ` ` `lookuptable[(N >> 16) & 0xff] + ` ` ` `lookuptable[(N >> 24) & 0xff]; ` ` ` `return` `count; ` `} ` ` ` `int` `main() ` `{ ` ` ` `// generate lookup table ` ` ` `for` `(` `int` `i = 0; i < 256; i++) ` ` ` `lookuptable[i] = (i & 1) + lookuptable[i / 2]; ` ` ` ` ` `unsigned ` `int` `N = 354; ` ` ` `cout << countSetBits(N) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

4

Time Complexity : O(1)

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:

- Count trailing zero bits using lookup table
- Reverse bits using lookup table in O(1) time
- Check if bits of a number has count of consecutive set bits in increasing order
- Count set bits in an integer
- Print numbers having first and last bits as the only set bits
- Next greater integer having one more number of set bits
- Previous smaller integer having one less number of set bits
- Find the largest number smaller than integer N with maximum number of set bits
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Toggle bits of a number except first and last bits
- Check if all bits can be made same by flipping two consecutive bits
- How to swap two bits in a given integer?
- Cyclic shifts of integer N by another integer m
- Count set bits using Python List comprehension
- Count number of set bits in a range using bitset
- Count Set-bits of number using Recursion
- Sort an array according to count of set bits | Set 2
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Program to count number of set bits in an (big) array

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.