Bitwise operator - C

  • One’s complement operator
  • Logical bitwise operator
  • Shift operator

Operators can be divided into 3 categories. One’s complement operator, logical bitwise operator and shift operators.
These operations can be performed only on integers (short, long) and character whether signed or unsigned.
We have considered byte as the smallest thing that can be operated upon. But each byte is further made up of bits.’C’ contains several operators that allow bitwise operations to be carried out easily. Using bitwise operations we can directly interact with the hardware. The collection of bits is nothing but binary representation of numbers.
Bitwise operator
When we are using bitwise operators, we are considering the binary representation of numbers

One’s complement operator
The ones complement operator, which is a unary operator, causes the bits of its operand to be inverted. i.e. each 1 is converted to 0 and each 0 is converted to 1. The operator precedes the operand.
~(5225) = 59286
Here ‘~’ being a bitwise operator we will consider binary equivalent of operand.
Binary equivalent of 5225 is 0001010001101001.
On inversion it will result into 1110101110010110 = 59286.

Decimal 1 is same as binary 0000000000000001. One’s complement of one is 1111111111111110. As it is seen one’s complement of a number gives an entirely different number. Hence it can be used to encrypt a file.

Logical bitwise operators

  • AND operator
  • OR operator
  • XOR operator

There are three types of logical bitwise operators AND operator (&), bitwise OR operator (|), bitwise XOR operator (^).
These operators use two operands. The operator has to be applied to each pair of bits (One from either operand), independent of other bits within the operand. The least significant bits within the two operands will be considered, then the next least significant bit and so on until operator has been applied to all bits. Both operands must be of the same type.

AND operator
Two bits on AND results into one only if both are one. The AND operation can be better understood by looking at the truth table.

AND operator
There is a difference between the logical AND operator (&&) and bitwise AND operator (&).
If x =1 and y = 2
Then x && y will consider x as one expression which on evaluation gives true (Since it has some value other than 0) and y as other expression which on evaluation gives true. Thus result is true Therefore (x && y) is 1.

For x & y, binary equivalent of x will be ANDed with binary equivalent of y, bit by bit.

For x
After AND (refer truth table)
Therefore (x & y) is 0.

The AND operation can be used to
• Check whether particular bits are ON or OFF.
• To turn of any bits.

In either case, the second operand has to be constructed according to the bits to be tested or to be turned off. It is called as mask

Check whether particular bits are ON or OFF
We have a bit pattern 1010 1101 and we want to check if 3rd bit is ON or OFF. We will construct a bit pattern (mask) such that 3rd bit in the pattern is 1. Mask is 0000 1000
AND these two patterns,

Check whether particular bits are ON or OFF

We see that the resulting bit pattern has third bit ON. When will the resulting pattern have third bit ON? It is when third bit of both operands are ON. We have constructed a mask such that third bit is ON. Thus third bit in the original bit pattern must be ON.

To turn of any bits.
If we require that fifth bit of given bit pattern 1010 1101 is turned OFF, we will construct a bit pattern such that its fifth bit is OFF and all other bits are ON. So that whatever is the fifth bit of given pattern, result will have fifth bit set to OFF, while all other remain unchanged.

To turn of any bits.

OR operator
Two bits on OR result into 1 if either bit is 1. The truth table for OR operation can be given as follows
OR operator
Bitwise OR operator is used to turn ON particular bit in a number. The bit to be turned ON in the original pattern should be made 1 in the OR mask. All other bits in the mask kept to 0 & remaining original bits remain unchanged.

Bitwise OR operator

XOR operator
It is called as bitwise exclusive OR operator. It excludes the last condition in the OR truth table. i.e. the bitwise exclusive OR operator sets 1 in each bit position where its operands have different bits and 0 where they are same.

XOR operator
XOR operation can be used toggle (invert) bits. If we want to toggle last 8 bit of a pattern,

XOR operation
When each of the rightmost 8 bit in ‘a’ is XOR with corresponding 1 in mask, the resulting bit is opposite of bit originally in ‘a’. On the other hand, when each of the leftmost 8 bit in ‘a’ is XORed with corresponding 0 in mask, resulting bit will be same as bit originally in ‘a’.

Shift operators

  • Right shift operator
  • Left shift operator

These operators take two operands. The first operand is the bit pattern to be shifted and the second is an unsigned integer, a number that indicates the number of places the bits are shifted.

Right shift operator
>> is right shift operator.
x>>3 shifts all bits in x, three places to the right.
If x contains the bit pattern 1101 0111, ch>>3 would give 0001 1010.

When bits are shifted to the right, the blanks created at the left are filled with zeros if the operand is unsigned. If the operand is *signed, blanks are filled with either zeroes or signbit. It depends on the machine.
If operand is a multiple of 2, then shifting the operand one bit to right causes division by 2.

64 >> 1 = (0100 0000) >> 1 = (0010 0000) which is the binary equivalent of 32.
64 >> 2 = (0100 0000) >> 2 = (0001 0000) which is the binary equivalent of 16.

* Signed quantity: Leftmost bit is used to indicate sign of a signed number. It is called the signbit. Leftmost bit is 1 if number is negative. Leftmost bit is 0 if number is positive.

Left shift operator
<< is the left shift operator.
y << 5 shifts all bits in y, five places to the left.

If y contains the bit pattern 0110 1001, y << 5 gives 0010 0000. For each bit shifted to left, a zero is added to the right of the number.
If operand is multiple of two then shifting operand one bit two left causes multiplication by 2.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics