Bitwise Operators in C
We have the following bitwise operators in C Language.
Operator | Description |
& | Binary AND Operator. |
| | Binary OR Operator. |
^ | Binary XOR Operator. |
~ | Binary NOT Operator. |
<< | Binary Left Shift Operator. |
>> | Binary Right Shift Operator. |
In order to understand bitwise operators one should know:
Decimal to binary conversion
Binary to decimal conversion
Decimal Number System
• Decimal number system has only 10 digits. i.e. 0,1,2,3,4,5,6,7,8,9
• So the base of the decimal number system is 10
• Every number can be represented with the digits from 0 to 9
Binary Number System
• A binary number system has only two digits that are 0 and 1.
• Every value is represented with 0 and 1 in this number system.
• Therefore the base of the binary number system is 2.
Decimal number to Binary Number
• Steps :
• Divide the number by 2 and record the quotient and remainder
• Repeat the step one until the quotient becomes 0.
• Write all the remainder values moving from bottom to top .
Example : Convert the decimal number 45 into its binary equivalent.
Binary Number to Decimal Number
• Steps:
• Start at the right most digit.
• Take the digit and multiply by powers of 2 according to the position of the digit in the number.
• Add the numbers
Example: convert the binary number 10101 to its decimal equivalent
25 | 24 | 23 | 22 | 21 | 20 |
1 | 0 | 1 | 1 | 0 | 1 |
= 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
= 1 x 32 + 0 x 16 + 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1
= 32 + 0 + 8 + 4 + 0 + 1
= (45) 10