The program prompts the user to input an integer and then counts the number of high bits (bits with a value of 1) in the binary representation of that integer.
Here's how the program works:
import java.util.Scanner; public class Count_HighBits { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = 0; int tot = 0; System.out.printf("Enter the Number : "); num = input.nextInt(); while (num != 0) { if ((num & 1) == 1) { tot++; } num = num >> 1; } System.out.println("Number of HIGH Bits are : "+tot); } }
Enter the Number : 10 Number of HIGH Bits are : 2
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions