For one’s complement, (Java unary ~ operator) take the bit pattern for the equivalent positive number, invert all bits 1⇒0 and 0⇒1, e.g. ~5 is 00000101 ⇒ 11111010.
For two’s complement, (Java unary  operator) take the bit pattern for the equivalent positive number, invert all bits 1⇒0 and 0⇒1, then add one, e.g. 5 is 00000101 ⇒ 11111010 ⇒ 11111011.
Binary is rather bulky to write out, so instead it is often written in terser hexadecimal (hex) or sometimes octal. Unfortunately, there is no way in Java prior to 1.7 to include binary literals in programs other than by encoding them in hex or the more old fashioned octal. In version 1.7+ you can write them with a lead 0b, the same way you can write hex with a lead 0x. Further, you can use _ to group digits e.g. 0b1010_1100.
Common boolean operators include & (bitwise AND for masking),  (bitwise OR for combining), >> (right signed shift), >>> (right unsigned shift), << (left signed/unsigned shift), ~ logical not and ^ bitwise xor. Unfortunately Java does not support binary literals 0b111 or underscores as shown in the examples. You must use hexadecimal notation without underscores instead.
Bitwise Operators  

binary operation  result  notes 
0b0101_0101 & 0b0001_1100  0b0001_0100  & logical AND, logical carryless bitwise
multiply, used for masking (getting rid of parts of a word you don’t
want), 1s where both operands have a 1 otherwise 0. Don’t confuse this
with &&.
0 &
0 → 0 & Tips

0b1110_0000  0b1000_0001  0b1110_0001   logical OR, logical carryless bitwise
addition, 1s where either operand has a 1, otherwise 0. Useful for combining
bit masks. Don’t confuse this with . Given that you have a mask to
describe the bit (all zeros, one one), to turn on a bit, use  mask.
0  0 → 0 
0b0000_0000_1001_0001 << 2  0b0000_0010_0100_0100  << signed/unsigned shift left. Slide to left 2bit places, dropping high order bits, shifting into the sign bit, filling on right with 0s. Shifting left by one bit is equivalent to multiplying by 2. You can calculate 2 to the nth power with 1 <<n . 
0b1101_0000_1001_0001 << 2  0b0100_0010_0100_0100  ditto. Example with sign bit on. 
0b0000_0000_1001_0001 >> 2  0b000_0000_0001_00100  >> signed shift right. Slide to right 2 bit places, drop low order bits, filling on left with the 0 sign bit. Shifting right by one bit is equivalent to dividing by 2, with the following exception 1 >> 1 = 1 because of sign extension. 1 / 2 should be 0. Other negative numbers work fine. 
0b1111_1111_1001_0001 >> 2  0b1111_1111_1110_0100  ditto. Example with sign bit on. 
0b0000_0000_1001_0001 >>> 2  0b000_0000_0001_00100  >>> unsigned shift right. Slide to right 2bit places, drop low order bits, filling on left with 0 bit. Shifting right by one bit is equivalent to dividing by 2, however, this won’t work for negative numbers because the sign bit gets converted to 0 with an unsigned shift. 
0b1111_1111_1001_0001 >>> 2  0b0111_1111_1110_0100  ditto. Example with sign bit on. 
~ 0b1111_1111_1001_0001  0b0000_0000_0110_1110  ~ logical not, bitwise 1’s complement. 0s become 1s and 1s become 0s. Given that you have a mask to describe the bit (all zeros, one one), to turn off a bit, use & ~mask . 
 0b1111_1111_1001_0001  0b0000_0000_0110_1111   negation, 2’s complement. 0s become 1s and 1s become 0s, then you add 1. 
0b1100_0000 ^ 0b1010_0001  0b0110_0001  ^ logical XOR (exclusive OR), exclusive or, bitwise difference. 1s where operands differ, 0 where they are the same. Useful in cryptography because xor has a magic symmetry: encrypted = plain ^ key; plain = encrypted ^ key; 
0b0000_0000_0000_1010  0b0000_0000_0000_0011  0b0000_0000_0000_0111   subtraction. 10  3 = 7. Works much like decimal subtraction, with borrowing. 
0b0000_0000_0000_1010 + 0b0000_0000_0000_0011  0b0000_0000_0000_1101  + addition. 10 + 3 = 13. Works much like decimal addition, with carrying. Since + works like  when there are no carries, programmers sometimes get into the dangerous habit of using + to combine masks instead of  . 
This page is posted 
http://mindprod.com/jgloss/binary.html  
Optional Replicator mirror

J:\mindprod\jgloss\binary.html  
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission.  
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[34.229.131.158] 
 
Feedback 
You are visitor number  