Write an efficient program to count number of 1s in binary representation of an integer.
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
1. Simple Method Loop through all bits in an integer, check if a bit is set and if it is then increment the set bit count. See below program.
Recursive Approach :
Please refer complete article on Count set bits in an integer for more details!
- Program to Convert Set of Integer to Array of Integer in Java
- Java Program to convert integer to boolean
- Program to Convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Java Program to convert boolean to integer
- Java Program to Print a Square Pattern for given integer
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Java Program to Check if count of divisors is even or odd
- Java program to count the occurrences of each character
- Java Program for Count ways to reach the n'th stair
- Java program to count the characters in each word in a given sentence
- Java Program to Count trailing zeroes in factorial of a number
- Java Program to Count number of binary strings without consecutive 1's
- Java program to count the occurrence of each character in a string using Hashmap