The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Bitwise Operators

We knew that, all integer variables represented internally as binary numbers. A value of type int consists of 32 binary digits, known to us as bits. We can operate on the bits that make up integer values using the bitwise operators.

Bitwise Operator's Facts

  • A bitwise operator which operates on each bit of data.
  • Bitwise operators only operates on integer operands such as int, char, short int, long int.
C Bitwise Operators

All Bitwise Operators

C provides 6 bitwise operators to operate on individual bits in an integer quantity.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Right shift
<< Left shift
~ One's Complement

Bitwise AND Operator

Bitwise AND operator, &, combines corresponding bits in its tow operands such that if both bits are 1, the result is 1 otherwise the result is 0.

Bitwise AND Operator Table

Input Output
X Y Z
0 0 0
0 1 0
1 0 0
1 1 1

Bitwise AND Operator Program

Let us write a C program to demonstrate Bitwise AND operator

bitwiseand.c
#include <stdio.h>
int main()
{
int a = 10, b = 2;
printf("a & b = %d ", a & b);
return 0;
}
  • a & b = 2

Note:

Binary representation is given below.

BitwiseAND

Bitwise OR Operator

The bitwise OR operator, |, combines corresponding bits such that if either or both bits are 1, then the result is 1. Only if both bits are 0 is the result 0.

Bitwise OR Operator Table

Input Output
X Y Z
0 0 0
0 1 1
1 0 1
1 1 1

Bitwise OR Operator Program

Let us write a C program to demonstrate Bitwise OR operator

bitwiseor.c
#include <stdio.h>
int main()
{
int a = 10, b = 2;
printf("a | b = %d ", a | b);
return 0;
}
  • a | b = 10

Note:

Binary representation is given below.

BitwiseOR

Bitwise XOR Operator

Bitwise exclusive OR (XOR) operator, ^, combines corresponding bits such that if both bits are the same the result is 0; otherwise, the result is 1.

Bitwise XOR Table

Input Output
X Y Z
0 0 0
0 1 1
1 0 1
1 1 0

Bitwise XOR Program

Let us write a C program to demonstrate Bitwise XOR operator

bitwisexor.c
#include <stdio.h>
int main()
{
int a = 10, b = 2;
printf("a ^ b = %d ", a ^ b);
return 0;
}
  • a ^ b = 8

Note:

Binary representation is given below.

bitwiseEXOR

Right shift Operator (>>)

Right shift operator requires two operands. It takes Left hand side operand as bit sequence or bit Pattern to be shifted and right hand side operand as positive integer or unsigned integer that indicates the number of displacements or bit positions to the right.

C Program for Right Shift

Let us write a C program to demonstrate Right Shift operator

rightshift.c
#include <stdio.h>
int main()
{
int a = 8,b;
a>>= 1;
b = a;
printf("The Right shifted data of 8 by 1 = %d ",b);
return 0;
}
  • The Right shifted data of 8 by 1 = 4

Note:

Binary representation is given below.

RightShift

Left shift Operator (<<)

Left shift operator also requires two operands. It takes Left hand side operand as bit sequence or bit Pattern to be shifted and right hand side operand as positive integer or Unsigned integer that indicates the number of displacement or bit positions to the left.

C Program for Left Shift

leftshift.c
#include <stdio.h>
int main()
{
int a = 4, b;
a<<= 1;
b = a;
printf("The Left shifted data of 4 by 1 = %d ",b);
return 0;
}
  • The Left shifted data of 4 by 1 = 8

Note:

Binary representation is given below.

LeftShift

One's Complement

One's Complement operator is a unary operator, it always precedes to the variable or an operand. One's Complement operator inverts bits of its operand. So, 1s becomes 0s and 0s becomes 1s.

Table for One's Complement

Input Output
X Y
0 1
1 0

C Program for One's Complement

onescomplement.c
#include <stdio.h>
int main()
{
unsigned short int a = 10;
a = ~a;
printf("After One's Complement a = %u", a);
return 0;
}
  • After One's Complement a = 65525

Note:

If a variable 'a' is declared under normal int data type some garbage value will be displayed say -11. Binary representation is given below.

BinaryOnesComplement

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report