Java Reference
In-Depth Information
3rd bit, 0x4
// Try the bitwise AND to select the third bit in indicators
System.out.println("indicators
= " +
toBinaryString(indicators));
System.out.println("selectBit3
= " +
toBinaryString(selectBit3));
indicators &= selectBit3;
System.out.println("indicators & selectBit3 = " +
toBinaryString(indicators));
// Try the bitwise OR to switch the third bit on
indicators = 0b1111_1111_0000_1001;
// Same as 0xFF09
System.out.println("\nindicators
= "+
toBinaryString(indicators));
System.out.println("selectBit3
= "+
toBinaryString(selectBit3));
indicators |= selectBit3;
System.out.println("indicators | selectBit3 = " +
toBinaryString(indicators));
// Now switch the third bit off again
indicators &= ~selectBit3;
System.out.println("\nThe third bit in the previous value of
indicators" +
" has been
switched off");
System.out.println("indicators & ~selectBit3 = " +
toBinaryString(indicators));
}
}
This example produces the following output:
indicators = 1111111100000111
selectBit3 = 100
indicators & selectBit3 = 100
indicators = 1111111100001001
selectBit3 = 100
indicators | selectBit3 = 1111111100001101
The third bit in the previous value of indicators has been switched
off
indicators & ~selectBit3 = 1111111100001001
Search WWH ::




Custom Search