Java Bitwise Operators Explained
Java Bitwise Operators Explained
In Java, the left shift operator (<<) shifts the bits of a number to the left by a specified number of positions, filling the least significant bits with zeros. For example, with the value 60 (0011 1100 in binary) shifted left by 2 positions, it becomes 240 (1111 0000 in binary). The zero fill right shift (>>>) operator shifts bits to the right, padding the leftmost bits with zeros instead of the sign bit, making it unsigned. For the same 60 shifted right by 2 (>>>) results in 15 (0000 1111 in binary), as the empty positions are filled with zeros .
The binary right shift (>>) operator shifts bits to the right, preserving the sign bit (the leftmost bit), thus maintaining the number's sign. For example, shifting the integer 60 (0011 1100 in binary) by 2 using >> results in 15 (0000 1111 in binary). The zero fill right shift (>>>) operator shifts bits to the right, but the leftmost bits are filled with zeros regardless of the sign, which can alter the sign for negative numbers but remains the same for positive numbers like 60. For 60 >>> 2, the result is the same, 15 (0000 1111 in binary), as the preserved bits remain 0 .
Hexadecimal and binary systems are both base systems used to represent numerical values, with binary being base 2 and hexadecimal being base 16. One hexadecimal digit can be exactly represented by four binary digits (bits), making conversion between them straightforward. For instance, the binary number 1111 0000 corresponds to the hexadecimal number F0. Java developers might prefer hexadecimal because it provides a more compact and human-readable form compared to binary for the same numerical value, especially useful when dealing with memory addresses or bit-level manipulation .
Using right shift operators on negative numbers in Java has significant implications due to how negative numbers are represented with two's complement binary form. The arithmetic right shift operator (>>) preserves the sign bit, resulting in a behavior that mimics division by two but rounds toward negative infinity. For instance, -8 (11111000 in an 8-bit system) shifted right by 2 results in -2 (11111110), maintaining the negative sign. However, the zero fill right shift (>>>) does not preserve the sign, filling with zeros regardless. Thus, a negative number shifted using >>> can become a large positive number due to left-side zero padding, which may not be the intended outcome .
In Java, hexadecimal numbers can be written using two conventions: either by appending an 'h' at the end of the number, although uncommon, or by prefixing the number with '0x'. For example, 0x400 is the equivalent of the decimal number 1024. This is because in hexadecimal, '400' converts to 4 * 16^2, which equals 1024 .
The NOT bitwise operator (~) is unary and flips the bits of an integer, effectively inverting each bit: 1s become 0s and 0s become 1s. For the integer 60, which is 0011 1100 in binary, the NOT operator results in -61. The operation flips all bits of 60 to 1100 0011, which in two's complement form represents -61 .
In Java, using the NOT operator (~) on a signed integer flips its bits, affecting its representation through two's complement notation, which is used for encoding signed integers. In two's complement, the leftmost bit indicates the sign (0 for positive, 1 for negative). Taking 60 (0011 1100 in binary) as an example, applying ~ results in 1100 0011, which is interpreted as -61 in two's complement format. The inversion results in a negative number due to the first bit switch from 0 to 1 .
When the integer 240 (1111 0000 in binary) is left-shifted by 2 places in Java using the << operator, the operation shifts all bits two places to the left, introducing two zeros in the least significant spots. The two leftmost bits shift out of the visible range for a typical 8-bit integer, but in Java's int type, which uses 32 bits, they are simply removed. Therefore, the result is 960 (0011 1100 0000 in binary), as spaces created on the right side are filled with zeros .
Bitwise operators manipulate integer values by performing bit-by-bit operations on the binary representations of those values. The OR operator (|) copies a bit if it exists in either operand. For the integers 60 (0011 1100 in binary) and 13 (0000 1101 in binary), using the OR operator results in 61, which is 0011 1101 in binary. This is because whenever there is a 1 in either corresponding bit of the operands, the result bit is set to 1 .
The XOR (^) operator differs from the OR (|) operator in that XOR copies a bit if it is set in one operand but not both, whereas OR copies a bit if it exists in either operand. When applying XOR to the integers 60 (0011 1100 in binary) and 13 (0000 1101 in binary), the result is 49 (0011 0001 in binary). This is because the XOR operation results in a 1 only if the corresponding bits of the operands differ .