The source code to check nth bit of the 16-bit number is set or not is given below. The given program is compiled and executed successfully.
// Java program to check nth bit of
// 16-bit number is set or not
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
short n = 0;
short k = 0;
System.out.printf("Enter a 16 bit number: ");
k = SC.nextShort();
System.out.printf("Enter the bit number: ");
n = SC.nextShort();
if (n < 0 || n > 16) {
System.out.printf("Enter between 0-15\n");
return;
}
k = (short)(k >> n);
if ((k & 1) == 1)
System.out.printf("BIT number %d is set\n", n);
else
System.out.printf("BIT number %d is not set\n", n);
}
}
Output:
Enter a 16 bit number: 9
Enter the bit number: 3
BIT number 3 is set
Explanation:
In the above program, we imported the "java.util.Scanner" package to read the variable's value from the user. And, created a public class Main. It contains a static method main().
Program/Source Code:
The source code to check nth bit of the 16-bit number is set or not is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported the "java.util.Scanner" package to read the variable's value from the user. And, created a public class Main. It contains a static method main().