In digital computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations.
On simple lowcost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition.^{[clarification needed]} While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power because of the reduced use of resources.^{[1]}
In the explanations below, any indication of a bit's position is counted from the right (least significant) side, advancing left. For example, the binary value 0001 (decimal 1) has zeroes at every position but the first (i. e. the rightmost) one.
The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. For example:
NOT 0111 (decimal 7) = 1000 (decimal 8)
NOT 10101011 (decimal 171) = 01010100 (decimal 84)
The bitwise complement is equal to the two's complement of the value minus one. If two's complement arithmetic is used, then NOT x = x − 1
.
For unsigned integers, the bitwise complement of a number is the "mirror reflection" of the number across the halfway point of the unsigned integer's range. For example, for 8bit unsigned integers, NOT x = 255  x
, which can be visualized on a graph as a downward line that effectively "flips" an increasing range from 0 to 255, to a decreasing range from 255 to 0. A simple but illustrative example use is to invert a grayscale image where each pixel is stored as an unsigned integer.
A bitwise AND is a binary operation that takes two equallength binary representations and performs the logical AND operation on each pair of the corresponding bits, which is equivalent to multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:
0101 (decimal 5) AND 0011 (decimal 3) = 0001 (decimal 1)
The operation may be used to determine whether a particular bit is set (1) or clear (0). For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:
0011 (decimal 3) AND 0010 (decimal 2) = 0010 (decimal 2)
Because the result 0010 is nonzero, we know the second bit in the original pattern was set. This is often called bit masking. (By analogy, the use of masking tape covers, or masks, portions that should not be altered or portions that are not of interest. In this case, the 0 values mask the bits that are not of interest.)
The bitwise AND may be used to clear selected bits (or flags) of a register in which each bit represents an individual Boolean state. This technique is an efficient way to store a number of Boolean values using as little memory as possible.
For example, 0110 (decimal 6) can be considered a set of four flags, where the first and fourth flags are clear (0), and the second and third flags are set (1). The third flag may be cleared by using a bitwise AND with the pattern that has a zero only in the third bit:
0110 (decimal 6) AND 1011 (decimal 11) = 0010 (decimal 2)
Because of this property, it becomes easy to check the parity of a binary number by checking the value of the lowest valued bit. Using the example above:
0110 (decimal 6) AND 0001 (decimal 1) = 0000 (decimal 0)
Because 6 AND 1 is zero, 6 is divisible by two and therefore even.
A bitwise OR is a binary operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example:
0101 (decimal 5) OR 0011 (decimal 3) = 0111 (decimal 7)
The bitwise OR may be used to set to 1 the selected bits of the register described above. For example, the fourth bit of 0010 (decimal 2) may be set by performing a bitwise OR with the pattern with only the fourth bit set:
0010 (decimal 2) OR 1000 (decimal 8) = 1010 (decimal 10)
A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example:
0101 (decimal 5) XOR 0011 (decimal 3) = 0110 (decimal 6)
The bitwise XOR may be used to invert selected bits in a register (also called toggle or flip). Any bit may be toggled by XORing it with 1. For example, given the bit pattern 0010 (decimal 2) the second and fourth bits may be toggled by a bitwise XOR with a bit pattern containing 1 in the second and fourth positions:
0010 (decimal 2) XOR 1010 (decimal 10) = 1000 (decimal 8)
This technique may be used to manipulate bit patterns representing sets of Boolean states.
Assembly language programmers and optimizing compilers sometimes use XOR as a shortcut to setting 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 clock cycles and memory than loading a zero value and saving it to the register.
Assuming , for the nonnegative integers, the bitwise operations can be written as follows:
There are 16 possible truth functions of two binary variables, this defines a truth table.
Here is the bitwise equivalent operations of two bits P and Q:
p  q  F^{0}  NOR^{1}  Xq^{2}  ¬p^{3}  ↛^{4}  ¬q^{5}  XOR^{6}  NAND^{7}  AND^{8}  XNOR^{9}  q^{10}  If/then^{11}  p^{12}  Then/if^{13}  OR^{14}  T^{15}  

1  1  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  
1  0  0  0  0  0  1  1  1  1  0  0  0  0  1  1  1  1  
0  1  0  0  1  1  0  0  1  1  0  0  1  1  0  0  1  1  
0  0  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1  
Bitwise equivalents 
0  NOT (p OR q) 
(NOT p) AND q 
NOT p 
p AND (NOT q) 
NOT q 
p XOR q  NOT (p AND q) 
p AND q  NOT (p XOR q) 
q  (NOT p) OR q 
p  p OR (NOT q) 
p OR q  1 
The bit shifts are sometimes considered bitwise operations, because they treat a value as a series of bits rather than as a numerical quantity. In these operations the digits are moved, or shifted, to the left or right. Registers in a computer processor have a fixed width, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they determine the values of the shiftedin bits.
If the width of the register (frequently 32 or even 64) is larger than the number of bits (usually 8) of the smallest addressable unit (atomic element), frequently called byte, the shift operations induce an addressing scheme on the bits. Disregarding the boundary effects at both ends of the register, arithmetic and logical shift operations behave the same, and a shift by 8 bit positions transports the bit pattern by 1 byte position in the following way:

a left shift by 8 positions increases the byte address by 1 

a right shift by 8 positions decreases the byte address by 1 

a left shift by 8 positions decreases the byte address by 1 

a right shift by 8 positions increases the byte address by 1 
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 (the MSB in two's complement) is shifted in on the left, thus preserving the sign of the operand.
This example uses an 8bit register:
00010111 (decimal +23) LEFTSHIFT = 00101110 (decimal +46)
10010111 (decimal −23) RIGHTSHIFT = 11001011 (decimal −11)
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 1 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:
00010111 (decimal +23) LEFTSHIFTBYTWO = 01011100 (decimal +92)
A left arithmetic shift by n is equivalent to multiplying by 2^{n} (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is equivalent to dividing by 2^{n} and rounding toward negative infinity. If the binary number is treated as ones' complement, then the same rightshift operation results in division by 2^{n} and rounding toward zero.
In a logical shift, zeros are shifted in to replace the discarded bits. Therefore, the logical and arithmetic leftshifts are exactly the same.
However, as the logical rightshift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic rightshift is ideal for signed two's complement binary numbers.
Another form of shift is the circular shift, bitwise rotation or bit rotation.
In this operation, sometimes called rotate no carry, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted into the right during a leftshift is whatever value was shifted out on the left, and vice versa for a rightshift operation. This is useful if it is necessary to retain all the existing bits, and is frequently used in digital cryptography.
Rotate through carry is a variant of the rotate operation, where the bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag.
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 RIGHTROTATETHROUGHCARRYBYONE
is a logical rightshift, and if the carry flag contains a copy of the sign bit, then x RIGHTROTATETHROUGHCARRYBYONE
is an arithmetic rightshift. For this reason, some microcontrollers such as low end 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 one end of the first register must come in at the other end of the second. With rotatethroughcarry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
In Cfamily languages, the logical shift operators are "<<
" for left shift and ">>
" for right shift. The number of places to shift is given as the second argument to the operator. For example,
x = y << 2;
assigns x
the result of shifting y
to the left by two bits, which is equivalent to a multiplication by four.
Shifts can result in implementationdefined behavior or undefined behavior, so care must be taken when using them. The result of shifting by a bit count greater than or equal to the word's size is undefined behavior in C and C++.^{[2]}^{[3]} Rightshifting a negative value is implementationdefined and not recommended by good coding practice;^{[4]} the result of leftshifting a signed value is undefined if the result cannot be represented in the result type.^{[2]}
In C#, the rightshift is an arithmetic shift when the first operand is an int or long. If the first operand is of type uint or ulong, the rightshift is a logical shift.^{[5]}
The Cfamily of languages lack a rotate operator, but one can be synthesized from the shift operators. Care must be taken to ensure the statement is well formed to avoid undefined behavior and timing attacks in software with security requirements.^{[6]} For example, a naive implementation that left rotates a 32bit unsigned value x
by n
positions is simply:
uint32_t x = ..., n = ...;
uint32_t y = (x << n)  (x >> (32  n));
However, a shift by 0
bits results in undefined behavior in the right hand expression (x >> (32  n))
because 32  0
is 32
, and 32
is outside the range [0  31]
inclusive. A second try might result in:
uint32_t x = ..., n = ...;
uint32_t y = n ? (x << n)  (x >> (32  n)) : x;
where the shift amount is tested to ensure it does not introduce undefined behavior. However, the branch adds an additional code path and presents an opportunity for timing analysis and attack, which is often not acceptable in high integrity software.^{[6]} In addition, the code compiles to multiple machine instructions, which is often less efficient than the processor's native instruction.
To avoid the undefined behavior and branches under GCC and Clang, the following is recommended. The pattern is recognized by many compilers, and the compiler will emit a single rotate instruction:^{[7]}^{[8]}^{[9]}
uint32_t x = ..., n = ...;
uint32_t y = (x << n)  (x >> (n & 31));
There are also compilerspecific intrinsics implementing circular shifts, like _rotl8, _rotl16, _rotr8, _rotr16 in Microsoft Visual C++. Clang provides some rotate intrinsics for Microsoft compatibility that suffers the problems above.^{[9]} GCC does not offer rotate intrinsics. Intel also provides x86 Intrinsics.
In Java, all integer types are signed, so the "<<
" and ">>
" operators perform arithmetic shifts. Java adds the operator ">>>
" to perform logical right shifts, but since the logical and arithmetic leftshift operations are identical for signed integer, there is no "<<<
" operator in Java.
More details of Java shift operators:^{[10]}
<<
(left shift), >>
(signed right shift), and >>>
(unsigned right shift) are called the shift operators.aByte >>> 2
is equivalent to ((int) aByte) >>> 2
.n >>> s
is n rightshifted s bit positions with zeroextension.byte
is implicitly converted to int
. If the byte value is negative, the highest bit is one, then ones are used to fill up the extra bytes in the int. So byte b1 = 5; int i = b1  0x0200;
will result in i == 5
.JavaScript uses bitwise operations to evaluate each of two or more units place to 1 or 0.^{[12]}
In Pascal, as well as in all its dialects (such as Object Pascal and Standard Pascal), the logical left and right shift operators are "shl
" and "shr
", respectively. Even for signed integers, shr
behaves like a logical shift, and does not copy the sign bit. The number of places to shift is given as the second argument. For example, the following assigns x the result of shifting y to the left by two bits:
x := y shl 2;
Bitwise operations are necessary particularly in lowerlevel programming such as device drivers, lowlevel graphics, communications protocol packet assembly, and decoding.
Although machines often have efficient builtin instructions for performing arithmetic and logical operations, all these operations can be performed by combining the bitwise operators and zerotesting in various ways.^{[13]} For example, here is a pseudocode implementation of ancient Egyptian multiplication showing how to multiply two arbitrary integers a
and b
(a
greater than b
) using only bitshifts and addition:
c ← 0
while b ≠ 0
if (b and 1) ≠ 0
c ← c + a
left shift a by 1
right shift b by 1
return c
Another example is a pseudocode implementation of addition, showing how to calculate a sum of two integers a
and b
using bitwise operators and zerotesting:
while a ≠ 0
c ← b and a
b ← b xor a
left shift c by 1
a ← c
return b