C Bitwise Operators - AND, OR, XOR, Complement and Shift Operations
C Bitwise Operators - AND, OR, XOR, Complement and Shift Operations
In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done
in bit-level. To perform bit-level operations in C programming, bitwise operators are used.
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
Let us suppose the bitwise AND operation of two integers 12 and 25.
https://www.programiz.com/c-programming/bitwise-operators 1/7
7/15/2017 C Bitwise Operators: AND, OR, XOR, Complement and Shift Operations
input output
1 #include <stdio.h>
2 int main()
3 {
4 int a = 12, b = 25;
5 printf("Output = %d", a&b);
6 return 0;
7 }
Feedback (https://www.programiz.com/contact)
Output
Output = 8
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted
by |.
https://www.programiz.com/c-programming/bitwise-operators 2/7
7/15/2017 C Bitwise Operators: AND, OR, XOR, Complement and Shift Operations
run
input output
1 #include <stdio.h>
2 int main()
3 {
4 int a = 12, b = 25;
5 printf("Output = %d", a|b);
6 return 0;
7 }
Feedback (https://www.programiz.com/contact)
Output
Output = 29
https://www.programiz.com/c-programming/bitwise-operators 3/7
7/15/2017 C Bitwise Operators: AND, OR, XOR, Complement and Shift Operations
run
input output
1 #include <stdio.h>
2 int main()
3 {
4 int a = 12, b = 25;
5 printf("Output = %d", a^b);
6 return 0;
7 }
Feedback (https://www.programiz.com/contact)
Output
Output = 21
For any integer n , bitwise complement of n will be -(n+1) . To understand this, you should have the knowledge of 2's complement.
2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus
1. For example:
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.
https://www.programiz.com/c-programming/bitwise-operators 4/7
7/15/2017 C Bitwise Operators: AND, OR, XOR, Complement and Shift Operations
input output
1 #include <stdio.h>
2 int main()
3 {
4 printf("complement = %d\n",~35);
5 printf("complement = %d\n",~-12);
6 return 0;
7 }
Feedback (https://www.programiz.com/contact)
Output
complement = -36
Output = 11
input output
1 #include <stdio.h>
2 int main()
3 {
4 int num=212, i;
5 for (i=0; i<=2; ++i)
6 printf("Right shift by %d: %d\n", i, num>>i);
7
8 printf("\n");
9
10 for (i=0; i<=2; ++i)
11 printf("Left shift by %d: %d\n", i, num<<i);
12
13 return 0;
14 }
Feedback (https://www.programiz.com/contact)
C Programming
C Introduction
C Flow Control
C Functions
C Programming Arrays
C Programming Pointers
C Programming Strings
Structure And Union
C Programming Files
Additional Topics
https://www.programiz.com/c-programming/bitwise-operators 6/7
7/15/2017 C Bitwise Operators: AND, OR, XOR, Complement and Shift Operations
Subscribe
ABOUT
CONTACT
ADVERTISE
https://www.programiz.com/c-programming/bitwise-operators 7/7