We would like to write a program that prompts the user to enter a short integer.
Display the 16 bits for the integer.
Here are sample runs:
Enter an integer: 5 The bits are 0000000000000101 Enter an integer: -5 The bits are 1111111111111011
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int value = input.nextInt(); /*w w w . j a v a2 s . c om*/ System.out.print("The 16 bits are "); //your code here } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int value = input.nextInt(); System.out.print("The 16 bits are "); int mask = 1; for (int i = 15; i >= 0; i--) { int temp = value >> i; int bit = temp & mask; System.out.print(bit); } } }