# C Bitwise Operators

C bitwise operators allows manipulation of data at bit level. It should also be noted that these operators cannot be applied on floating point numbers.

Available C bitwise operators are as follows:

## Bitwise AND Operator (&)

When two values are operated by bitwise & operator, the binary representation of both values are compared bit by bit. If each corresponding bit in the first value is 1 and also 1 in the second value, then it produces a 1 in corresponding bit position of result, anything else produces a 0.

Bit A | Bit B | Output Bit |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

## Bitwise Inclusive OR Operator (|)

When two values are operated by bitwise inclusive or operator, then the binary representation of two values are compared bit by bit. If a particular bit is 1 in either of the two values, then it produces 1 in corresponding bit of result, otherwise 0 is produced.

Bit A | Bit B | Output Bit |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

## Bitwise Exclusive OR Operator (^)

This operator often called the XOR operator works as follows. For corresponding bits of the two operands, if either bit is a 1, but not both, the corresponding bit of the result is 1, otherwise it is a 0.

It should be noted that if any value is exclusive ORed with itself, it produces 0.

Bit A | Bit B | Output Bit |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

## Ones Complement Operator (~)

Ones complement operator is a unary operator which simply reverse the bits of its operand. Each bit of the operand if is a 1 is changed to 0, but if the bit is 0, then it is changed to 1.

Bit A | Output Bit |
---|---|

0 | 1 |

1 | 0 |

## Left Shift Operator (<<)

Left shift operator shifts the bits of a value to the left. Bits that are shifted out through most significant bit of the data item are lost and 0s are always shifted in through least significant bit of the value.

Left shifting actually has the effect of multiplying the value that is shifted by two.

Left shifting n bits means the number x is multiplied by 2^{n}.

*y=x*2 ^{n}*

Above, x is the number and n is the number of bits to be shifted.

## Right Shift Operator (>>)

Right shift operator shifts the bits of a value to the right. Bits shifted out of least significant bit of value are lost. Right shifting an unsigned value always results in 0s being shifted on left.

If sign bit is 0 meaning value is positive, 0s are shifted in regardless of machine we are running.

However, if sign bit is 1, on the same machine 1s are shifted in known as arithmetic right shift and on others 0s are shifted in known as logical right shift.

Right shifting n bits means the number x is divided by 2^{n}.

*y=x/2 ^{n}*

It should be noted that if an attempt is made to shift a value to the left or right by an amount which is either greater than or equal to number of bits in the data item, then the result produced is not defined in c.

## C Bitwise Operators Example

/*C program to illustrate bitwise operators*/ #include<stdio.h> main() { unsigned int w1=0525u, w2=0707u,w3=0122u; printf("%on",w1&w2); printf("%on",w1|w2); printf("%on",w1^w2); return 0; getch(); }