Java’s Unary Bitwise Complement Operator “~” The ~ operator takes an integer type primitive. If smaller than int , the primitive value will be converted to an int . The result simply switches the sense of every bit. ~ is Java’s unary bitwise complement operator. It operates on individual bits of integer values (int and long). If an operand is shorter than an int, it is promoted to int before doing the operations. It inverts a bit patter; making every “0” a “ 1” and every “1” a “0”. For example, a decimal number 4 represented by a binary number with 8 bits is: 0000 0100 If we apply the ~ operator to this integer, after the ~ operator flips each bit, we get: 1111 1011 What does this bit pattern represent internally with Java? Java internally uses two's complement arithmetic. Positive numbers are stored in binary numbers, for example the number 13 would be written 00001101. We don’t need to do anything else. For the negative numbers, we fix a “1” in the high order bit (the left most
This is the end of the preview. Sign up
access the rest of the document.