Bitwise Operators in C

CProgrammingServer Side Programming

Bitwise operators are used to perform bit-level operations on two variables. Here is the table of bitwise operators in C language,

OperatorsName of Operators
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Shift left
>>Shift right

Here is an example of bitwise operators in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   int x = 10;
   int y = 28;
   int i = 0;
   printf("Bitwise AND : %d
", x&y);    printf("Bitwise OR : %d
", x|y);    printf("Bitwise XOR : %d
", x^y);    printf("Bitwise Complement : %d,%d
", ~x,~-y);    for(i;i<2;i++)    printf("Right shift by %d: %d
", i, x>>i);    for(i;i<=3;++i)    printf("Left shift by %d: %d
", i, y<<i);    return 0; }

Output

Bitwise AND : 8
Bitwise OR : 30
Bitwise XOR : 22
Bitwise Complement : -11,27
Right shift by 0: 10
Right shift by 1: 5
Left shift by 2: 112
Left shift by 3: 224
raja
Updated on 24-Jun-2020 11:01:54

Advertisements