// Binary.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2002-01-15 File created. // 2005-05-12 Last modification. class Binary { static void print_in_binary_form( int given_integer ) { int bit_mask = 0x80000000 ; int one_bit_in_given_integer ; for ( int bit_counter = 0 ; bit_counter < 32 ; bit_counter ++ ) { one_bit_in_given_integer = given_integer & bit_mask ; if ( one_bit_in_given_integer == 0 ) { System.out.print( "0" ) ; } else { System.out.print( "1" ) ; } bit_mask = bit_mask >>> 1 ; } } public static void main( String[] not_in_use ) { int test_number = 0x9A9A ; System.out.print( "\n Original test number: " ) ; print_in_binary_form( test_number ) ; System.out.print( "\n Twice left-shifted form: " ) ; test_number = test_number << 2 ; print_in_binary_form( test_number ) ; System.out.print( "\n Back to original form: " ) ; test_number = test_number >> 2 ; print_in_binary_form( test_number ) ; System.out.print( "\n Last four bits zeroed: " ) ; test_number = test_number & 0xFFF0 ; print_in_binary_form( test_number ) ; System.out.print( "\n Last four bits to one: " ) ; test_number = test_number | 0x000F ; print_in_binary_form( test_number ) ; System.out.print( "\n A complemented form: " ) ; test_number = ~test_number ; print_in_binary_form( test_number ) ; System.out.print( "\n Exclusive OR with 0xF0F0: " ) ; test_number = test_number ^ 0xF0F0 ; print_in_binary_form( test_number ) ; System.out.print( "\n Double right shift with >>: " ) ; test_number = test_number >> 2 ; print_in_binary_form( test_number ) ; System.out.print( "\n Double right shift with >>>: " ) ; test_number = test_number >>> 2 ; print_in_binary_form( test_number ) ; } }