The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
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.
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, &, combines corresponding bits in its tow operands such that if both bits are 1, the result is 1 otherwise the result is 0.
Input | Output | |
---|---|---|
X | Y | Z |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Let us write a C program to demonstrate Bitwise AND operator
#include <stdio.h> int main() { int a = 10, b = 2; printf("a & b = %d ", a & b); return 0; }
Binary representation is given below.
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.
Input | Output | |
---|---|---|
X | Y | Z |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Let us write a C program to demonstrate Bitwise OR operator
#include <stdio.h> int main() { int a = 10, b = 2; printf("a | b = %d ", a | b); return 0; }
Binary representation is given below.
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.
Input | Output | |
---|---|---|
X | Y | Z |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Let us write a C program to demonstrate Bitwise XOR operator
#include <stdio.h> int main() { int a = 10, b = 2; printf("a ^ b = %d ", a ^ b); return 0; }
Binary representation is given below.
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.
Let us write a C program to demonstrate Right Shift operator
#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; }
Binary representation is given below.
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.
#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; }
Binary representation is given below.
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.
Input | Output |
---|---|
X | Y |
0 | 1 |
1 | 0 |
#include <stdio.h> int main() { unsigned short int a = 10; a = ~a; printf("After One's Complement a = %u", a); return 0; }
If a variable 'a' is declared under normal int data type some garbage value will be displayed say -11. Binary representation is given below.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019