Bitwise Operators
Bitwise Operators
Bitwise Operators
html
remove this part
A : 01000001
27 128 26 64 25 32 24 16 23 8 22 4 21 2 20 1
0 1 0 0 0 0 0 1
5:
0 0 0 0 0 1 0 1
27 128 26 64 25 32 24 16 23 8 22 4 21 2 20 1
a=5
b=7
c=a&b
0 0 0 0 0 1 0 1
a =
& & & & & & & &
b = 0 0 0 0 0 1 1 1
c = 0 0 0 0 0 1 0 1
print ( c ) - 5
c=a|b
a=5 0 0 0 0 0 1 0 1
Bitwise or Operation | | | | | | | |
b=7 0 0 0 0 0 1 1 1
c7 0 0 0 0 0 1 1 1
c = a ^ b ^ caret : Shift 6
a=5 0 0 0 0 0 1 0 1
Bitwise or Operation ^ ^ ^ ^ ^ ^ ^ ^
b=7 0 0 0 0 0 1 1 1
c2 0 0 0 0 0 0 1 0
a = 60 0 0 1 1 1 1 0 0
right shift divides by 2
0 0 1 1 1 1 0 0
shift each bit one position to their 0 0 0 1 1 1 1 0 right
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Bitwise operators
In C, we can perform operations in bit level.
Other operators which we have discussed earlier are byte level operators.
Using bitwise operators, we can manipulate each and every bit of memory.
Since, we are doing it in bit level the operation will be much faster and efficient.
Ai Bi A i | Bi
0 0 0
0 1 1
1 0 1
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Ai Bi A i | Bi
1 1 1
Example
Let’s take two integers say A = 10 and B = 12,
Then A | B will be,
Pictorial Explanation
Pictorial Explanation
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Whenever the sensor wants to update any status, it will use the above four bits and set the
status. Like, if the sensor sets first bit as 1 which indicates that the car should turn to the right
direction.
Here, we need to enable bits according to sensor input.
OR MASK should have only one 1 in some position where we need to enable the bit status.
Ai Bi A i& Bi
0 0 0
0 1 0
1 0 0
1 1 1
Example
Let’s take two integers,
A = 5 and B = 9.
A = (0101) 2
B= (1001) 2
Now, apply the bitwise AND logic in each pair of corresponding bits.
Like,
Pictorial Explanation
In order to trun the car to the specified direction, we need to check which direction bit is enabled.
Let's assume that sensor updates the right direction bit as ON.
Then the sensor status will be 0001. (backward 0, forward 0, left 0, right 1).
How will you check whether the first bit (right direction) is
ON or OFF?
Using bitwise AND operator, we can check the particular bit has enabled(set to 1) or disabled(set to 0).
AND MASK should have only one 1 in some position where we supposed to check the bit status.
Example
Here, we are going to check the first bit status.
So, MASK will be 0001 (1 in first bit and remaining 3 bits are zero).
Like,
The answer is 1 which indicates that the car should change it's direction to the right.
Similarly, let's check for left direction status which is second bit.
Bitwise XOR (^) operator will take two equal length binary sequence and perform bitwise XOR operation
on each pair of bit sequence.
Ai Bi A i ^ Bi
0 0 0
0 1 1
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Ai Bi A i ^ Bi
1 0 1
1 1 0
Example
A = (0101) 2
B= (1001) 2
Now, apply the bitwise XOR logic in each pair of corresponding bits.
Like,
Pictorial Explanation
The interesting fact here is, if we again perform XOR on the above result with 9 we will get the 5 back.
Pictorial Explanation
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Because of this property, Bitwise XOR operator was used in cryptography initially.
But if we send message 5 directly without any modification then the secrete will be a question mark.
To avoid that, the two person will share the secrete key which is only known by those two.
Before sending the message the sender will XOR the message with secrete key. Then the encrypted
message will be sent.
After receiver got the message, he will decrypt the using the same key and get the original message.
Example
Sender,
Encryption
Secret key = 9
Message = 5
Encrypted message = 5 XOR 9 = 12
12 will be sent to the receiver rather than actual message 5.
Receiver,
Decryption
Secret key = 9
Message received = 12
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
In general, if we shift a number by n position to left, the output will be number * (2n).
Example
Example
12 << 3
= 12 * (23)
= 12 * 8
= 96.
Example
#include<stdio.h>
int main()
{
int var = 2;
return 0;
}
Example
Let’s take a number 14.
Binary representation of 14 is 00001110 (for the sake of clarity let’s write it using 8 bit)
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
14 = (00001110) 2
Then 14 >> 1 will shift the binary sequence by 1 position to the right side.
Like,
Pictorial Explanation
= 128 >> 5
= 128 / (25)
=128/32
=4.
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
#include<stdio.h>
int main()
{
int var = 128;
return 0;
}
Example
Let’s take a number 4.
Then ~4 will be,
4 = (00000100) 2
~4 = (11111011) 2.It will convert all 1 to 0 and 0 to 1.
Pictorial Explanation
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Example 2
Let’s take number 2,
2 = (00000010) 2
1’s complement of 2 = (11111101) 2
Add 1 to get the 2’s complement,
~2 = (11111101) 2
Result of ~2 is equal to the representation of -3 in binary, hence ~2 is -3.
#include<stdio.h>
int main()
{
int var = 3;
printf("value = %d \n",~var); // which is equal to -4
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
return 0;
}