Q:

Java program to check whether all bits of the number are UNSET (LOW)

belongs to collection: Java Basic Programs

0

Java program to check whether all bits of the number are UNSET (LOW)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to check whether all bits of the number are UNSET/LOW is given below. The given program is compiled and executed successfully.

// Java program to check whether all bits 
// of the number are UNSET/LOW

import java.util.Scanner;

public class Main {
  static boolean isAllBitsUnset(byte num) {
    int loop, cnt = 0;

    for (loop = 7; loop >= 0; loop--) {
      //check, if there is any SET/HIGH bit
      if ((num & (1 << loop)) != 0) {
        cnt = 1;
        break;
      }
    }
    if (cnt == 0)
      return true;
    else
      return false;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);
    byte number;

    System.out.printf("Enter an integer number (between 0-255): ");
    number = SC.nextByte();

    if (isAllBitsUnset(number))
      System.out.printf("All bits are UNSET/LOW.\n");
    else
      System.out.printf("All bits are not UNSET/LOW.\n");
  }
}

Output:

Enter an integer number (between 0-255): 4
All bits are not UNSET/LOW.

Explanation:

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 isAllBitsUnset() and main().

The isAllBitsUnset() method is used to check all bits of a specified number are LOW or not and return a Boolean value to the calling method.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to count total HIGH bits of the given... >>
<< Java program to swap the first and second bits of ...