Practical 3
Practical 3
Title: Create a C program to perform bitwise operations on integer variable and print the results.
In the 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.
Operators Meaning of operators
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
Shift left
Let us suppose the bitwise AND operation of two integers 12 and 25.
Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
For any integer n, bitwise complement of n will be -(n + 1). To understand this, you
2's Complement
Right shift operator shifts all bits towards right by certain number of specified bits. It
is denoted by >>.
212 = 11010100 (In binary)
212 >> 2 = 00110101 (In binary) [Right shift by two bits]
212 >> 7 = 00000001 (In binary)
212 >> 8 = 00000000
212 >> 0 = 11010100 (No Shift)
Left shift operator shifts all bits towards left by a certain number of specified bits. The
bit positions that have been vacated by the left shift operator are filled with 0. The