Java program to count total HIGH bits of the given number

Given/input an integer number, we have to count total HIGH bits of the given number.
Submitted by Nidhi, on March 07, 2022

Problem Solution:

In this program, we will read a short integer from the user and find the total HIGH bits in the input number.

Program/Source Code:

The source code to count the total HIGH bits of the given number is given below. The given program is compiled and executed successfully.

// Java program to count total HIGH bits 
// of the given number

import java.util.Scanner;

public class Main {
  static int countHighBits(short num) {
    byte i;
    int count = 0;

    for (i = 0; i < 16; i++) {
      if ((num & (1 << i)) != 0)

    return count;

  public static void main(String[] args) {
    Scanner SC = new Scanner(;

    short num = 0;

    System.out.printf("Enter number: ");
    num = SC.nextShort();

    System.out.printf("\nTotal HIGH bits are : %d\n", countHighBits(num));


Enter number: 7

Total HIGH bits are : 3


In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contain two static methods countHighBits() and main().

The countHighBits() method is used to count the total HIGH bits of a given short integer using bitwise operators and return the result to the calling method.

The main() method is an entry point for the program. Here, we read a short integer number from the user using the Scanner class. Then we called the countHighBits() method and got the total number of HIGH bits and printed the result.

Java Basic Programs »


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.