In the C programming language, operations can be performed on a bit level using bitwise operators.
Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR, NOT operators. Instead of performing on individual bits, byte-level operators perform on strings of eight bits (known as bytes) at a time. The reason for this is that a byte is normally the smallest unit of addressable memory (i.e. data with a unique memory address).
This applies to bitwise operators as well, which means that even though they operate on only one bit at a time they cannot accept anything smaller than a byte as their input.
All of these operators are also available in C++, and many C-family languages.
C provides six operators for bit manipulation.
The bitwise AND operator is a single ampersand: <code>&</code>. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND performs logical conjunction (shown in the table above) of the bits in each position of a number in its binary form.
For instance, working with a byte (the char type): 11001000 & 10111000 -------- = 10001000
The most significant bit of the first number is 1 and that of the second number is also 1 so the most significant bit of the result is 1; in the second most significant bit, the bit of second number is zero, so we have the result as 0.
Similar to bitwise AND, bitwise OR performs logical disjunction at the bit level. Its result is a 1 if either of the bits is 1 and zero only when both bits are 0. Its symbol is <code>|</code> which can be called a pipe.
The bitwise XOR (exclusive or) performs an exclusive disjunction, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones. XOR can be used to toggle the bits between 1 and 0. Thus <code>i = i ^ 1</code> when used in a loop toggles its values between 1 and 0.
There are two bitwise shift operators. They are
The symbol of right shift operator is <code>>></code>. For its operation, it requires two operands. It shifts each bit in its left operand to the right. The number following the operator decides the number of places the bits are shifted (i.e. the right operand). Thus by doing <code>ch >> 3</code> all the bits will be shifted to the right by three places and so on.
However, do note that a shift operand value which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior. For example, when shifting a 32 bit unsigned integer, a shift amount of 32 or higher would be undefined.
Example:
Here blank spaces are generated simultaneously on the left when the bits are shifted to the right. When performed on an unsigned type or a non-negative value in a signed type, the operation performed is a logical shift, causing the blanks to be filled by <code>0</code>s (zeros). When performed on a negative value in a signed type, the result is technically implementation-defined (compiler dependent), however most compilers will perform an arithmetic shift, causing the blank to be filled with the set sign bit of the left operand.
Right shift can be used to divide a bit pattern by 2 as shown:
Typical usage of a right shift operator in C can be seen from the following code.
Example:
The output of the above program will be
The symbol of left shift operator is <code><<</code>. It shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. It works opposite to that of right shift operator. Thus by doing <code>ch << 1</code> in the above example (<code>11100101</code>) we have <code>11001010</code>. Blank spaces generated are filled up by zeroes as above.
However, do note that a shift operand value which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior. This is defined in the standard at ISO 9899:2011 6.5.7 Bit-wise shift operators. For example, when shifting a 32 bit unsigned integer, a shift amount of 32 or higher would be undefined.
Left shift can be used to multiply an integer by powers of 2 as in
The following program adds two operands using AND, XOR and left shift (<<).
C provides a compound assignment operator for each binary arithmetic and bitwise operation. Each operator accepts a left operand and a right operand, performs the appropriate binary operation on both and stores the result in the left operand.
The bitwise assignment operators are as follows.
Four of the bitwise operators have equivalent logical operators. They are equivalent in that they have the same truth tables. However, logical operators treat each operand as having only one value, either true or false, rather than treating each bit of an operand as an independent value. Logical operators consider zero false and any nonzero value true. Another difference is that logical operators perform short-circuit evaluation.
The table below matches equivalent operators and shows a and b as operands of the operators.
<code>!=</code> has the same truth table as <code>^</code> but unlike the true logical operators, by itself <code>!=</code> is not strictly speaking a logical operator. This is because a logical operator must treat any nonzero value the same. To be used as a logical operator <code>!=</code> requires that operands be normalized first. A logical not applied to both operands will not change the truth table that results but will ensure all nonzero values are converted to the same value before comparison. This works because <code>!</code> on a zero always results in a one and <code>!</code> on any nonzero value always results in a zero.
Example:
The output of the above program will be