Count set bits in an integer using Lookup Table
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 232-1 “). So we break 32 bits into 8 bits of chunks( How lookup table of size (28-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
LookupTable[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
Below is the code implementation of the above approach:
CPP
#include <iostream>
using namespace std;
#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)
unsigned int lookuptable[256]
= { B6(0), B6(1), B6(1), B6(2) };
unsigned int countSetBits( int N)
{
unsigned int count = lookuptable[N & 0xff] +
lookuptable[(N >> 8) & 0xff] +
lookuptable[(N >> 16) & 0xff]
+ lookuptable[(N >> 24) & 0xff];
return count;
}
int main()
{
unsigned int N = 354;
cout << countSetBits(N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static ArrayList<Integer> lookuptable
= new ArrayList<Integer>();
static void B2( int n)
{
lookuptable.add(n);
lookuptable.add(n + 1 );
lookuptable.add(n + 1 );
lookuptable.add(n + 2 );
}
static void B4( int n)
{
B2(n);
B2(n + 1 );
B2(n + 1 );
B2(n + 2 );
}
static void B6( int n)
{
B4(n);
B4(n + 1 );
B4(n + 1 );
B4(n + 2 );
}
static int countSetBits( int N)
{
int count = lookuptable.get(N & 0xff )
+ lookuptable.get((N >> 8 ) & 0xff )
+ lookuptable.get((N >> 16 ) & 0xff )
+ lookuptable.get((N >> 24 ) & 0xff );
return count;
}
public static void main(String[] args)
{
B6( 0 );
B6( 1 );
B6( 1 );
B6( 2 );
int N = 354 ;
System.out.println(countSetBits(N));
}
}
|
Python3
lookuptable = []
def B2(n):
lookuptable.extend([n, n + 1 , n + 1 , n + 2 ])
def B4(n):
B2(n), B2(n + 1 ), B2(n + 1 ), B2(n + 2 )
def B6(n):
B4(n), B4(n + 1 ), B4(n + 1 ), B4(n + 2 )
lookuptable.extend([B6( 0 ), B6( 1 ), B6( 1 ), B6( 2 )])
def countSetBits(N):
count = lookuptable[N & 0xff ] + lookuptable[(N >> 8 ) & 0xff ] + lookuptable[(
N >> 16 ) & 0xff ] + lookuptable[(N >> 24 ) & 0xff ]
return count
N = 354
print (countSetBits(N))
|
Javascript
let lookuptable = [];
function B2(n)
{
lookuptable.push(n);
lookuptable.push(n + 1);
lookuptable.push(n + 1);
lookuptable.push(n + 2);
}
function B4(n)
{
B2(n), B2(n + 1), B2(n + 1), B2(n + 2)
}
function B6(n)
{
B4(n), B4(n + 1), B4(n + 1), B4(n + 2)
}
lookuptable.push(B6(0));
lookuptable.push(B6(1));
lookuptable.push(B6(1));
lookuptable.push(B6(2));
function countSetBits(N)
{
let count = lookuptable[N & 0xff] + lookuptable[(N >> 8) & 0xff] + lookuptable[(N >> 16) & 0xff] + lookuptable[(N >> 24) & 0xff];
return count;
}
let N = 354;
console.log(countSetBits(N));
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List< int > lookuptable = new List< int >();
static void B2( int n)
{
lookuptable.Add(n);
lookuptable.Add(n + 1);
lookuptable.Add(n + 1);
lookuptable.Add(n + 2);
}
static void B4( int n)
{
B2(n);
B2(n + 1);
B2(n + 1);
B2(n + 2);
}
static void B6( int n)
{
B4(n);
B4(n + 1);
B4(n + 1);
B4(n + 2);
}
static int countSetBits( int N)
{
int count = lookuptable[N & 0xff]
+ lookuptable[(N >> 8) & 0xff]
+ lookuptable[(N >> 16) & 0xff]
+ lookuptable[(N >> 24) & 0xff];
return count;
}
public static void Main( string [] args)
{
B6(0);
B6(1);
B6(1);
B6(2);
int N = 354;
Console.WriteLine(countSetBits(N));
}
}
|
Output:
4
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...