Bitwise operators are used to perform bit operations. Decimal numbers are converted into binary numbers which are the sequence of bits and bitwise operators work on these bits.
Data in the memory (RAM) is organized as a sequence of bytes. Each byte is a group of eight consecutive bits. Bitwise operators are useful when we need to perform actions on bits of the data.
Bitwise operators work with integer type. They do not support float or real types.
There are six bitwise operators in C.
Operator | Description |
---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise complement |
<< | Bitwise left shift |
>> | Bitwise right shift
|
Bitwise AND (&) operator:
The bitwise AND (&) operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Operation | Result |
---|
0 & 0 | 0 |
1 & 0 | 0 |
0 & 1 | 0 |
1 & 1 | 1 |
Note: Both operands to the bitwise AND operator must be of integer types. Since, bitwise operators work with integer type. They do not support float or real types.
Let's understand with a simple example:
Suppose x and y are two integer variables with initial value
int x=10, y=11;
Let us re-write these integers in 8-bit binary representation and then perform bit operation x & y.
- x = 0000 1010 // Binary representation of 10
- y = 0000 1011 // Binary representation of 11
- x 0000 1010
- y 0000 1011
- ---------------------------------------
- x & y 0000 1010
The above expression x & y
will evaluate to 0000 1010 which is 10 in decimal.
Bitwise OR (|) operator:
The bitwise OR | operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 0, the corresponding result bit is set to 0. Otherwise, the corresponding result bit is set to 1.
Bitwise OR | operator is commonly used to set flag bit values.
Operation | Result |
---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
Let's understand with a simple example:
Suppose x and y are two integer variables with initial value
int x=3, y=14;
Let us re-write these integers in 8-bit binary representation and then perform bit operation x | y.
- x = 0000 0011 // Binary representation of 3
- y = 0000 1110 // Binary representation of 14
- x 0000 0011
- y 0000 1110
- ---------------------------------------
- x | y 0000 1111
The above expression x | y
will evaluate to 0000 1111 which is 15 in decimal.
Bitwise XOR (^) operator:
The bitwise XOR ^ operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits of the two operands are differ, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Operation | Result |
---|
0 ^ 0 | 0 |
1 ^ 0 | 1 |
0 ^ 1 | 1 |
1 ^ 1 | 0 |
Let's understand with a simple example:
Suppose x and y are two integer variables with initial value
int x=6, y=13;
Let us re-write these integers in 8-bit binary representation and then perform bit operation x ^ y.
- x = 0000 0110 // Binary representation of 6
- y = 0000 1101 // Binary representation of 13
- x 0000 0110
- y 0000 1101
- ---------------------------------------
- x ^ y 0000 1011
The above expression x ^ y
will evaluate to 0000 1011 which is 11 in decimal.
Bitwise complement (~) operator:
The bitwise complement ~ operator is also called one's complement operator since it always takes only one value or an operand. It is a unary operator. When we perform complement on any bits, all the one's become zero 0's and vice versa.
Let's understand with a simple example:
Suppose x is an integer variable with initial value int x=10;
Let us re-write this integer in 8-bit binary representation and then perform one's complement operation ~x.
- x = 0000 1010 // Binary representation of 10
- x 0000 1010 // Binary representation of 10
- ---------------------------------------
- ~x 1111 0101 // one's comlement.
The above expression ~ x
will evaluate to 1111 0101 which is --(Two's complement) in decimal.
Bitwise left shift (<<) operator:
The bitwise left shift << operator is a binary operator. Which is used to shift each bits to left n times.
Let's understand with a simple example:
Suppose x is an integer variable with initial value int x=15;
Let us re-write these integers in 8-bit binary representation and the perform left shifting x << n.
- x = 0000 1111 // Binary representation of 15
- x 0000 1111 // Binary representation of 15
- ---------------------------------------
- x << 1 0001 1110 // Sifting bits of x one times to the left. Which is = 30 in decimal
- x << 2 0011 1100 // Sifting bits of x two times to the left. Which is = 60 in decimal
- x << 3 0111 1000 // Sifting bits of x three times to the left. Which is = 120 in decimal
- x << 4 1111 0000 // Sifting bits of x four times to the left. Which is = 240 in decimal
The above expression x << 4
will evaluate to 1111 0000 which is 240 in decimal.
Remember: Shifting bits to left is also equivalent to multiplying value by 2. You can use bitwise left shift operator if you need to multiply a variable by a power of two. As you can see in the above example after each left shifting number becomes double.
Bitwise right shift (>>) operator:
The bitwise right shift >> operator is a binary operator. Which is used to shift each bits to right n times.
Let's understand with a simple example:
Suppose x is an integer variable with initial value int x=31;
Let us re-write these integers in 8-bit binary representation and the perform right shifting x >> n.
- x = 0001 1111 // Binary reresentation of 31.
- x 0001 1111 // Binary representation of 31
- ---------------------------------------
- x >> 1 0000 1111 // Sifting bits of x one times to the rght.
- x >> 2 0000 0111 // Sifting bits of x two times to the right.
- x >> 3 0000 0011 // Sifting bits of x three times to the right.
- x >> 4 0000 0001 // Sifting bits of x four times to the right.
The above expression x >> 4
will evaluate to 0000 0001 which is -- in decimal.
Remember: Shifting bits to right is also equivalent to dividing value by 2. You can use bitwise right shift operator if you need to divid a number (unsigned number) by a power of two.
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.