Skip to content

Logic Quest

VLSI Interview Questions

  • Wave Diagram Analysis
  • Design Questions
  • Gate Level Questions
  • Some Coding
  • Contact us
  • Toggle search form

Count Set Bits

Wrtie a function that get a number and returns the number of set bits at this number.

int countSetBits(int num)

Answer

int countSetBits(int num){
	int count = 0;
	while (num) {
		count += num & 1;
		num >>= 1;
	}
	return count;
}
INPUT  : 5
OUTPUT : 2

INPUT  : 31
OUTPUT : 5

INPUT  : 46
OUTPUT : 4 

Next Question>>>

Copyright © 2023 Logic Quest.

Powered by PressBook Blog WordPress theme