import java.io.*; class ShowIntConv { public static void PrintBits(byte x) { int i; for (i = 7; i >= 0; i--) if ( (x & (1 << i)) != 0 ) System.out.print("1"); else System.out.print("0"); } public static void PrintBits(short x) { int i; for (i = 15; i >= 0; i--) if ( (x & (1 << i)) != 0 ) System.out.print("1"); else System.out.print("0"); } public static void PrintBits(int x) { int i; for (i = 31; i >= 0; i--) if ( (x & (1 << i)) != 0 ) System.out.print("1"); else System.out.print("0"); } public static void main(String[] arg) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); byte b; short s; int i; System.out.print ("Enter **byte** integer b: (try negative values too) "); b = (byte) Integer.parseInt(stdin.readLine()); System.out.println ("... assigning byte b to short s and int i....\n"); s = b; i = b; System.out.print("b = " + b + "\t 2's complement repr = "); PrintBits(b); System.out.println(); System.out.print("s = " + s + "\t 2's complement repr = "); PrintBits(s); System.out.println(); System.out.print("i = " + i + "\t 2's complement repr = "); PrintBits(i); System.out.println(); System.out.println("=============================================\n"); System.out.print ("Enter **short** integer s: (try 256) "); s = (short) Integer.parseInt(stdin.readLine()); System.out.println ("... assigning short s to byte b and int i....\n"); b = (byte) s; i = s; System.out.print("b = " + b + "\t 2's complement repr = "); PrintBits(b); System.out.println(); System.out.print("s = " + s + "\t 2's complement repr = "); PrintBits(s); System.out.println(); System.out.print("i = " + i + "\t 2's complement repr = "); PrintBits(i); System.out.println(); System.out.println("=============================================\n"); System.out.print ("Enter **int** integer i: (try 65536, 65792) "); i = (int) Integer.parseInt(stdin.readLine()); System.out.println ("... assigning int i to byte b and short s....\n"); b = (byte) i; s = (short) i; System.out.print("b = " + b + "\t 2's complement repr = "); PrintBits(b); System.out.println(); System.out.print("s = " + s + "\t 2's complement repr = "); PrintBits(s); System.out.println(); System.out.print("i = " + i + "\t 2's complement repr = "); PrintBits(i); System.out.println(); System.out.println("=============================================\n"); } }