https://www.log2base2.com/C/basic/purpose-of-programming.
html
remove this part
1 Byte : 8 bits int 16 bits/ 32 bits
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
left shift multiplies by 2
shift each bit one position to their 0 1 1 1 1 0 0 0 left
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.
Various Bitwise operators
Bitwise or operator
Bitwise and operator
Bitwise xor operator
Bitwise left shift operator
Bitwise right shift operator
Bitwise ones complement operator
Bitwise OR (|) operator
Bitwise OR (|) operator will take two equal length binary sequence and perform bitwise OR
operation on each pair of bit sequence.
Bitwise OR will return 1, if any of the bit in the pair is 1.
If both bits are 0, then the outcome will be 0.
Truth table of OR(|),
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
What is the use of bitwise OR (|) operator ?
Let’s take a scenario where we are going to design a robot car which uses a sensor to sense the
environment and update it's direction accordingly.
Directions = {left, right, forward, backward}.
We are going to use 4 bits, each one for each direction.
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.
How will you enable specific bit programmatically?
Using bitwise OR operator, we can enable a specific bit.
Example
Let’s assume the sensor senses something and it wants to indicate the car to take the right direction.So,
we need enable first bit as 1. Because first bit is reserved for right direction.
To update the bit status, we need to create a binary OR MASK.
OR MASK should have only one 1 in some position where we need to enable the bit status.
And it should have the remaining bits as 0.
In our case, we need to enable first bit,
So OR MASK = 0001 (right 1, remaining directions are 0)
Now take Sensor status OR MASK,
Sensor status = 0000
MASK = 0001
Sensor status | MASK = 0001
Right direction bit has set as 1.
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Similarly if we want to enable left direction bit,
MASK will be 0010 (left 1, remaining 0)
Now take sensor status | MASK,
Sensor status = 0000
MASK = 0010
Sensor status | MASK = 0010
Left direction bit has set as 1.
Bitwise AND (&) operator
Bitwise AND (&) operator will take two equal length binary sequence and perform
the bitwise AND operation on each pair of bit sequence.
AND operator will return 1, if both bits are 1.
Otherwise it will return 0.
The truth table,
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.
Then what will be the result of A & B?
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Let’s represent each number in binary format.
A = (0101) 2
B= (1001) 2
Now, apply the bitwise AND logic in each pair of corresponding bits.
Like,
Pictorial Explanation
So, A&B will be (0001)2, which is 1.
What is the use of bitwise AND (&) operator ?
Let’s continue the previously discussed example where the sensor will update direction status in car
robot.
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).
To check the bit status, we need to create a binary AND MASK.
AND MASK should have only one 1 in some position where we supposed to check the bit status.
And it should have the remaining bits as 0.
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
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).
Now calculate Sensor status & MASK
Like,
Sensor status = 0001
MASK = 0001
Sensor status & MASK = 0001
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.
MASK will be 0010 (left 1, remaining are zero)
Now, calculate Sensor status & MASK
Sensor status = 0001
MASK = 0010
Sensor status & MASK = 0000
Now the answer is 0 which indicates don’t trun the car to the left direction.
Bitwise XOR operator
Bitwise XOR (^) operator will take two equal length binary sequence and perform bitwise XOR operation
on each pair of bit sequence.
XOR operator will return 1, if both bits are different.
If bits are same, it will return 0.
The truth table,
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
Let’s take two integers say A = 5 and B = 9.
Then what will be the result of A ^ B?
Let’s represent each number in binary format.
A = (0101) 2
B= (1001) 2
Now, apply the bitwise XOR logic in each pair of corresponding bits.
Like,
Pictorial Explanation
So, A ^ B will be (1100)2, which is 12.
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.
XOR operator in cryptography
Assume,
5 is the value that we want to send to another person confidentially.
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.
Let’s take secrete key as 9.
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
Original message = 12 XOR 9 = 5
This is one of the weakest cryptography methods.
Bitwise Left shift << operator
Bitwise Left shift operator is used to shift the binary sequence to the left side by
specified position.
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)
14 = (00001110) 2
Then 14 << 1 will shift the binary sequence 1 position to the left side.
Like,
Pictorial Explanation
Application of Bitwise Left Shift Operator
In the above diagram, you can notice that whenever we shift the number one position to left, the output
value will be exactly number * 2.
If we shift 14 by 1 position to the left, output will be 14 * 2 = 28.
If we shift 14 by 2 position to the left, output will be 14 * 4 = 56.
In general, if we shift a number by n position to left, the output will be number * (2n).
Example
Let’s assume the number as 12
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
If we shift the number 3 position to the left.
Then the output will be,
Example
12 << 3
= 12 * (23)
= 12 * 8
= 96.
Program using Bitwise Left shift Operator
Example
#include<stdio.h>
int main()
{
int var = 2;
printf("var * 2 = %d \n",var<<1); //1 position to the left
printf("var * 4 = %d \n",var<<2); //2 position to the left
printf("var * 8 = %d \n",var<<3); //3 position to the left
printf("var * 16 = %d \n",var<<4); //4 position to the left
printf("var * 32 = %d \n",var<<5); //5 position to the left
return 0;
}
Bitwise Right shift >> operator
Bitwise Right shift operator >> is used to shift the binary sequence to right side by
specified position.
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
Application of Bitwise Right Shift Operator
In the above diagram, you can notice that whenever we shift the number one position to
right, the output value will be exactly number / 2.
If I shift 14 by 1 position to the right, output will be 14 / 2 = 7. i.e 14/2 = 7
If I shift 14 by 2 position to the right, output will be 14 / 4 = 3. i.e 14/4 =3.5 since it’s an
integer, fractional part will not be considered.
In general, if we shift a number by n times to right, the output will be number / (2n) .
Example
Let’s assume number as 128.
If we shift the number 5 position to the right, the output will be
= 128 >> 5
= 128 / (25)
=128/32
=4.
https://www.log2base2.com/C/basic/purpose-of-programming.html
remove this part
Program using bitwise right shift operator
Example
#include<stdio.h>
int main()
{
int var = 128;
printf("var/2 =%d \n",var>>1); //1 position to right
printf("var/4 =%d \n",var>>2); //2 position to right
printf("var/8 =%d \n",var>>3); //3 position to right
printf("var/16 =%d \n",var>>4); //4 position to right
printf("var/32 =%d \n",var>>5); //5 position to right
return 0;
}
Bitwise one's complement operator (~)
Bitwise one's compliment operator will invert the binary bits.
If a bit is 1, it will change it to 0.
If the bit is 0, it will change it to 1.
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
If we print the value of ~4 using printf, it will print -5. How?
Let's take a look on how -5 will be stored in computer?
As we discussed earlier negative numbers are represented using 2’s
complement form.
To know how to calculate 2’s complement of a number, kindly refer the link.How
integers are stored in memory
So, -5 will be stored like below,
5 = (00000101) 2
1’s complement of 5 = (11111010) 2
Add 1 to get the 2’s complement,
-5 = (11111011) 2
Result of ~4 is equal to the representation of -5 in binary, hence ~4 is -5.
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.
Program using bitwise ones complement operator
Example
#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;
}