// BitOperatorTests.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-11 File created. // 2005-05-12 Last modification. import java.util.* ; class BitOperatorTests { 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 ; } } // The following method works like the method above but it // takes a parameter of type short. static void print_in_binary_form( short given_short_integer ) { int given_integer = given_short_integer ; int bit_mask = 0x8000 ; int one_bit_in_given_integer ; for ( int bit_counter = 0 ; bit_counter < 16 ; 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 ) { System.out.printf( "\n (0x61 & 0xDF) produces 0x%02X", ( 0x61 & 0xDF) ) ; System.out.printf( "\n (0xF0 & 0x0F) produces 0x%02X\n", (0xF0 & 0x0F) ) ; System.out.printf( "\n (0x41 | 0x20) produces 0x%02X", (0x41 | 0x20) ) ; System.out.printf( "\n (0xF0 | 0x0F) produces 0x%02X\n", (0xF0 | 0x0F) ) ; System.out.printf( "\n (0xAF ^ 0xFF) produces 0x%02X", (0xAF ^ 0xFF) ) ; System.out.printf( "\n (0xFF ^ 0xFF) produces 0x%02X", (0xFF ^ 0xFF) ) ; System.out.printf( "\n ( ~ 0xE1 ) produces 0x%02X", ( ~ 0xE1 ) ) ; System.out.printf( "\n ( ~ 0xF0 ) produces 0x%02X\n", ( ~ 0xF0 ) ) ; System.out.printf( "\n ( 0x49 >> 2 ) produces 0x%02X", // >> operator ( 0x49 >> 2 ) ) ; System.out.printf( "\n ( 0x49 >> 3 ) produces 0x%02X\n", ( 0x49 >> 3 ) ) ; System.out.printf( "\n ( 0x49 >>> 2 ) produces 0x%02X", // >>> operator ( 0x49 >>> 2 ) ) ; System.out.printf( "\n ( 0x49 >>> 3 ) produces 0x%02X\n", ( 0x49 >>> 3 ) ) ; System.out.printf( "\n ( 0x12 << 3 ) produces 0x%02X", ( 0x12 << 3 ) ) ; System.out.printf( "\n ( 0x12 << 4 ) produces 0x%02X\n", ( 0x12 << 4 ) ) ; // A = 1010 // C = 1100 System.out.printf( "\n ( 0xA0000000 >> 1 ) produces 0x%02X", ( 0xA0000000 >> 1 ) ) ; System.out.printf( "\n ( 0xA0000000 >>> 1 ) produces 0x%02X\n", ( 0xA0000000 >>> 1 ) ) ; System.out.printf( "\n ( 0xA0000000 >> 2 ) produces 0x%02X", ( 0xA0000000 >> 2 ) ) ; System.out.printf( "\n ( 0xA0000000 >>> 2 ) produces 0x%02X\n", ( 0xA0000000 >>> 2 ) ) ; System.out.printf( "\n ( 0x80000000 >> 1 ) produces 0x%02X", ( 0x80000000 >> 1 ) ) ; System.out.printf( "\n ( 0x80000000 >>> 1 ) produces 0x%02X\n", ( 0x80000000 >>> 1 ) ) ; System.out.printf( "\n ( 0x80000000 >> 3 ) produces 0x%02X", ( 0x80000000 >> 3 ) ) ; System.out.printf( "\n ( 0x80000000 >>> 3 ) produces 0x%02X\n", ( 0x80000000 >>> 3 ) ) ; // The following statements prove that operator ~ // produces a byte value when its operand is a byte. System.out.print( "\n " ) ; byte first_byte = (byte) 0xE1 ; byte second_byte = (byte) 0xF0 ; System.out.printf( "\n ( ~ first_byte ) produces 0x%02X", ( ~ first_byte ) ) ; System.out.printf( "\n ( ~ second_byte ) produces 0x%02X\n", ( ~ second_byte ) ) ; short variable_of_16_bits = 0x626B ; // short variable_of_16_bits = (short) 0xA26B ; System.out.printf( "\n Variable to shift contains %X", variable_of_16_bits ) ; System.out.print( "\n Before shifting: " ) ; print_in_binary_form( variable_of_16_bits ) ; System.out.print( "\n Shifting with >>>: " ) ; print_in_binary_form( (short) ( variable_of_16_bits >>> 3 ) ) ; System.out.print( "\n >>> shift, no cast:" ) ; print_in_binary_form( ( variable_of_16_bits >>> 3 ) ) ; variable_of_16_bits = (short) ( variable_of_16_bits >> 3 ) ; System.out.print( "\n Shifting with >>: " ) ; print_in_binary_form( variable_of_16_bits ) ; System.out.print( "\n " ) ; char some_character = 'a' ; System.out.print( "\n some_character is " + some_character ) ; some_character = (char) ( some_character & 0xDF ) ; System.out.print( "\n some_character is " + some_character ) ; some_character = (char) ( some_character | 0x20 ) ; System.out.print( "\n some_character is " + some_character ) ; } }