import java.io.*; class ShowSignExt { 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"); } }