Bitwise operators manipulate patterns of bits in computer programs. Common operators include AND, OR, XOR, NOT, shift right, and shift left. They are used to control hardware, encryption, and compression. These operators are represented symbolically in programming languages and apply a logical operation to each pair of bits in their operands. Care must be taken with different bit lengths and signed/unsigned operands.
In a computer program, bitwise operators are operators for manipulating patterns of bits in expressions. Bitwise AND, OR, exclusive OR (XOR), NOT, shift right, and shift left are common operations. Most computer programming languages include all six of these operators. They are often used to set, clear, or flip individual bits in programs that control computer hardware. Bitwise operators are also often used in data encryption and compression algorithms.
Common bitwise operators are usually represented symbolically in programming languages such as C, C++, and Java. They are used in expressions just as arithmetic operators such as plus or minus signs would be. The bitwise operator AND is represented by the ampersand &, OR by the vertical bar | and XOR with the caret ^. NOT, also known as the complement operator, is indicated with a tilde ~.
The right-shift operator uses a right-pointing double cursor >>. Conversely, moving left is indicated by a double cursor pointing left. Java includes another shift right operation, shown with a triple cursor pointing right >>>.
Bitwise operators apply a logical operation to each pair of bits in their operands. NOT, right shift, and left shift have only one real operand; the second value in the move operations is the bit count. To better understand how bitwise operators work, an individual can view their operands as binary numbers. For example, the complement operator flips each bit of its single operand into the opposite state. A one becomes a zero and a zero becomes a one. The complement of the 8-bit binary value 00110101 is 11001010.
The AND operator has two operands. Bitwise, a logical AND operation is performed using a corresponding bit of each operand. The outcome of each bit operation is placed in that bit position of the overall result. For example, the AND operator will process bit 7 of one operand with bit 7 of the other operand. The result will be stored in bit 7 of the overall result.
In an AND operation, both bits of the operand must be one for the result to be one, otherwise the result is zero. For example, if the 8-bit binary values of the operands are 00110101 and 11110000, the result will be 00110000. A common use of the AND operator is to clear certain bits in the result. This is done by placing zeros in those bit positions in one of the operands.
In an OR operation, both bits of the operand must be zero for the result to be zero; otherwise the result is one. A common use of the OR operator is to set some bits of the result to one. This is done by putting those in those bit positions into one of the operands. For the XOR operation, the result is zero if both bits of the operand are zeros or if both bits of the operand are ones, otherwise the result is one.
Shift left and shift right shift the bits in the operand left or right by the specified number of bit positions. A logical right shift shifts a zero to the leftmost bit as part of the shift. An arithmetic shift right copies the leftmost bit, the sign bit, before the shift to the same position after the shift. The original bit is also shifted right along with the rest. Both types of left shift shift a zero to the rightmost bit.
When an operand is shifted to the right, the rightmost bit before the shift is simply discarded. Likewise, the leftmost bit before a left shift is deleted. It does not wrap around the other end of the operand.
Bitwise shift operations are language and implementation dependent. For example, in C and C++, >> and performs logical shifts if the operand is an unsigned integer. If the operand is a signed integer, an arithmetic shift is likely to be performed instead. In Java, all operands are considered signed and arithmetic shifts are always performed with >> and . The >>> operator is used for a logical right shift, but it is still possible to accidentally perform an arithmetic right shift without careful type conversion.
Complications can also arise when the operands have different bit lengths or when some are signed and some are unsigned. Bitwise operators and numeric constants within a complex expression may not evaluate as expected. Care should be taken to specify the size and signed/unsigned nature of each quantity in the expression. This can be done with careful typecasting or intermediate assignments to specific types of variables in the program code.
Protect your devices with Threat Protection by NordVPN