- In this tutorial we will learn about bitwise operators in C. C supports OR, AND, NOT, XOR, Right shift and Left shift bitwise operators.

C is a middle level language, it support many operations which can be performed in assembly language like operations on bits. Bitwise operators performs bit-by-bit operations on operands.

There are six bitwise operators supported in C programming language.

Bitwise Operator | Syntax | Description |
---|---|---|

| | A | B | Bitwise OR Operator |

& | A & B | Bitwise AND Operator |

~ | ~A | NOT Operator(One’s Complement) |

^ | A ^ B | Bitwise Exclusive OR Operator |

>> | A >> B | Right Shift Operator |

<< | A << B | Left Shift Operator |

Bitwise Operators can only be applied on char and integer operands. We cannot use bitwise operators with float, double, long double, void and other user define complex data types.

Here is the truth table of &, |, ^ and ~ bitwise operators.

A | B | A | B | A & B | A ^ B | ~A |
---|---|---|---|---|---|

0 | 0 | 0 | 0 | 0 | 1 |

0 | 1 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 | 0 |

1 | 0 | 1 | 0 | 1 | 0 |

### C Program to show the use of AND, OR, XOR, NOT and Right/Left Shift Bitwise Operators

#include <stdio.h> #include <conio.h> int main(){ /* A = 29 = 00011101(in binary) B = 42 = 00101010(in binary) */ int A = 29, B = 42; /* Bitwise OR */ printf("A | B = %d\n", A | B); /* Bitwise AND */ printf("A & B = %d\n", A & B); /* Bitwise XOR */ printf("A ^ B = %d\n", A ^ B); /* Bitwise NOT */ printf("~A = %d\n", ~A); /* Bitwise Right Shift Operator */ printf("A >> 3 = %d\n", A >> 3); /* Bitwise Left Shift Operator */ printf("A << 3 = %d\n", A << 3); getch(); return 0; }

Output

A | B = 63 A & B = 8 A ^ B = 55 ~A = -30 A >> 3 = 3 A << 3 = 232