C Bitwise Operators
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.
C Bitwise Operators allows manipulation of data at bit level. It should also be noted that these operators cannot be applied on floating point numbers.
Operators | Meaning of operators |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift right |
Bitwise AND Operator (&)
When two values are operated by bitwise & operator, the binary representation of both values are compared bit by bit. If each corresponding bit in the first value is 1 and also 1 in the second value, then it produces a 1 in corresponding bit position of result, anything else produces a 0.
Bit A | Bit B | Output Bit |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bit Operation of 12 and 25 00001100 & 00011001 ________ 00001000 = 8 (In decimal)
Example #1: Bitwise AND
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}
Output
Output = 8
Bitwise Inclusive OR Operator (|)
When two values are operated by bitwise inclusive or operator, then the binary representation of two values are compared bit by bit. If a particular bit is 1 in either of the two values, then it produces 1 in corresponding bit of result, otherwise 0 is produced.
Bit A | Bit B | Output Bit |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Let us suppose the bitwise OR operation of two integers 12 and 25.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal)
Example #2: Bitwise OR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a|b);
return 0;
}
Output
Output = 29
Bitwise Exclusive OR Operator (^)
This operator often called the XOR operator works as follows. For corresponding bits of the two operands, if either bit is a 1, but not both, the corresponding bit of the result is 1, otherwise it is a 0.
It should be noted that if any value is exclusive ORed with itself, it produces 0.
Bit A | Bit B | Output Bit |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Let us suppose the bitwise XOR operation of two integers 12 and 25.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise XOR Operation of 12 and 25 00001100 | 00011001 ________ 00010101 = 21 (In decimal)
Example #3: Bitwise XOR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a^b);
return 0;
}
Output
Output = 21
Bitwise complement operator ~
Ones Complement Operator (~)
Ones complement operator is a unary operator which simply reverse the bits of its operand. Each bit of the operand if is a 1 is changed to 0, but if the bit is 0, then it is changed to 1.
Bit A | Output Bit |
---|---|
0 | 1 |
1 | 0 |
35 = 00100011 (In Binary) Bitwise complement Operation of 35 ~ 00100011 ________ 11011100 = 220 (In decimal)
Twist in bitwise complement operator in C Programming
The bitwise complement of 35 (~35) is -36 instead of 220, but why?
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:
Decimal Binary 2's complement 0 00000000 -(11111111+1) = -00000000 = -0(decimal) 1 00000001 -(11111110+1) = -11111111 = -256(decimal) 12 00001100 -(11110011+1) = -11110100 = -244(decimal) 220 11011100 -(00100011+1) = -00100100 = -36(decimal) Note: Overflow is ignored while computing 2's complement.
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.
Bitwise complement of any number N is -(N+1). Here's how:
bitwise complement of N = ~N (represented in 2's complement form) 2'complement of ~N= -(~(~N)+1) = -(N+1)
Example #4: Bitwise complement
#include <stdio.h>
int main()
{
printf("complement = %d\n",~35);
printf("complement = %d\n",~-12);
return 0;
}
Output
complement = -36 Output = 11
Shift Operators in C programming
There are two shift operators in C programming:
- Right shift operator
- Left shift operator.
Left Shift Operator (<<)
Left shift operator shifts the bits of a value to the left. Bits that are shifted out through most significant bit of the data item are lost and 0s are always shifted in through least significant bit of the value.
Left shifting actually has the effect of multiplying the value that is shifted by two.
Left shifting n bits means the number x is multiplied by 2n.
y=x*2n
Above, x is the number and n is the number of bits to be shifted.
212 = 11010100 (In binary) 212<<1 = 110101000 (In binary) [Left shift by one bit] 212<<0 =11010100 (Shift by 0) 212<<4 = 110101000000 (In binary) =3392(In decimal)
Right Shift Operator (>>)
Right shift operator shifts the bits of a value to the right. Bits shifted out of least significant bit of value are lost. Right shifting an unsigned value always results in 0s being shifted on left.
If sign bit is 0 meaning value is positive, 0s are shifted in regardless of machine we are running.
However, if sign bit is 1, on the same machine 1s are shifted in known as arithmetic right shift and on others 0s are shifted in known as logical right shift.
Right shifting n bits means the number x is divided by 2n
y=x/2n
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)
MIND IT !
It should be noted that if an attempt is made to shift a value to the left or right by an amount which is either greater than or equal to number of bits in the data item, then the result produced is not defined in c.
Example #5: Shift Operators
#include <stdio.h>
int main()
{
int num=212, i;
for (i=0; i<=2; ++i)
printf("Right shift by %d: %d\n", i, num>>i);
printf("\n");
for (i=0; i<=2; ++i)
printf("Left shift by %d: %d\n", i, num<<i);
return 0;
}
Right Shift by 0: 212 Right Shift by 1: 106 Right Shift by 2: 53 Left Shift by 0: 212 Left Shift by 1: 424 Left Shift by 2: 848