In many programming languages (including those in the C family), the bitwise NOT operator is "
~" (tilde). This operator must not be confused with the "logical not" operator, "
!" (exclamation point), which treats the entire value as a single Boolean — changing a true value to false, and vice versa. The "logical not" is not a bitwise operation.
In the C programming language family, the bitwise OR operator is "
|" (pipe). Again, this operator must not be confused with its Boolean "logical or" counterpart, which treats its operands as Boolean values, and is written "
||" (two pipes).
The bitwise OR may be used in situations where a set of bits are used as flags; the bits in a single binary numeral may each represent a distinct Boolean variable. Applying the bitwise OR operation to the numeral along with a bit pattern containing 1 in some positions will result in a new numeral with those bits set. For example:
can be considered as a set of four flags. The first, second, and fourth flags are not set (0); the third flag is set (1). The first flag may be set by applying the bitwise OR to this value, along with another value in which only the first flag is set:
This technique is often used to conserve memory in programs dealing with large numbers of Boolean values.
In the C programming language family, the bitwise XOR operator is "
Assembly language programmers sometimes use the XOR operation as a short-cut to set the value of a register to zero. Performing XOR on a value against itself always yields zero, and on many architectures, this operation requires fewer CPU clock cycles than the sequence of operations that may be required to load a zero value and save it to the register.
The bitwise XOR may also be used to toggle flags in a set of bits. Given a bit patterns:
The first and third bits may be toggled simultaneously by a bitwise XOR with another bit pattern containing 1 in the first and third positions:
This technique may be used to manipulate bit patterns representing sets of Boolean variables.
In the C programming language family, the bitwise AND operator is "
&" (ampersand). Again, this operator must not be confused with its Boolean "logical and" counterpart, which treats its operands as Boolean values, and is written "
&&" (two ampersands).
The bitwise AND may be used to perform a bit mask operation. This operation may be used to isolate part of a string of bits, or to determine whether a particular bit is 1 or 0. For example, given a bit pattern:
To determine whether the third bit is 1, a bitwise AND is applied to it and another bit pattern containing 1 in the third bit:
Since the result is 0010 (non-zero), the third bit in the original pattern was 1. Using bitwise AND in this manner is called bit masking, by analogy to the use of masking tape to cover, or mask, portions that should not be altered, or are not of interest. In this case, the 0 values mask the bits that are not of interest.
The bitwise AND can also be combined with the bitwise NOT to clear bits. For example:
The second bit may be cleared (i.e. set to 0) by applying the bitwise AND to this value, along with the complement (i.e. NOT) of another value in which only the second bit is set:
In an arithmetic shift, the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the sign bit is shifted in on the left, thus preserving the sign of the operand. This example uses an 8-bit register:
In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out (perhaps into the carry flag), and a new 0 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits. For example:
A left arithmetic shift by n is equivalent to multiplying by 2n (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is equivalent to dividing by 2n and rounding toward negative infinity. If the binary number is treated as ones' complement, then the same right-shift operation results in division by 2n and rounding toward zero.
A single rotate through carry can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then
x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is a logical right-shift, and if the carry flag contains a copy of the sign bit, then
x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is an arithmetic right-shift. For this reason, some microcontrollers such as PICs just have rotate and rotate through carry, and don't bother with arithmetic or logical shift instructions.
Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native word size, because if a large number is stored in two registers, the bit that is shifted off the end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
<<" and "
>>", respectively. The number of places to shift is given as the second argument to the shift operators. For example,
assigns x the result of shifting y to the left by two digits.
In C and C++, computations with the left operand as an unsigned integer use logical shifts. In C, the results with the left operand as a signed integer are:
<<": left×2right (undefined if an overflow occurs);
>>": implementation-defined (most often the result of the arithmetic shift: left/2right).
In Java, all integer types are signed, and the "
<<" and "
>>" operators perform arithmetic shifts. Java adds the operator "
>>>" to perform logical right shifts, but since the logical and arithmetic left-shift operations are identical, there is no "
<<<" operator in Java. These general rules are affected in several ways by the default type promotions; for example, since the eight-bit type
byte is promoted to
int in shift-expressions, the expression "
b >>> 2" effectively performs an arithmetic shift of the byte value
b instead of a logical shift. Such effects can be mitigated by judicious use of casts or bitmasks; for example, "
(b & 0xFF) >>> 2" effectively results in a logical shift.
Although machines often have efficient built-in instructions for performing arithmetic and logical operations, in fact all these operations can be performed just by combining the bitwise operators and zero-testing in various ways.
For example, here is a pseudocode example showing how to multiply two arbitrary integers
b using only bitshifts and addition:
c := 0
while b ≠ 0
if (b and 1) ≠ 0
c := c + a
shift a left by one
shift b right by one