Bitwise or bitlevel operations form the basis of embedded programming. A knowledge of Hexadecimal and Binary Numbering system is required along with conversion from binary to hex and vice-verse. the link http://www.electronics-tutorials.ws/binary/bin_3.html provides information regarding Hexadecimal Numbers.
Bit-wise Operators : Now getting armed with the knowledge of interconversion between Hexadecimal and Binary we can start with Bitwise(or bit level) operations in C. There are bascially 6 types of Bitwise operators. These are :
- 1. Bitwise OR operator denoted by ‘|‘
- 2. Bitwise AND operator denoted by ‘&‘
- 3. Bitwise Complement or Negation Operator denoted by ‘~‘
- 4. Bitwise Right Shift & Left Shift denoted by ‘>>‘ and ‘<<‘ respectively
- 5. Bitwise XOR operator denoted by ‘^‘
Bitwise OR ( | ): This is same as OR Operator in digital logic. if we operate OR 0 with any No n then result will be the No ie. n. itself and if we operate OR 1 with any No n then result will be 1.
Ex. if the no is N then N | 1 = 1 and N | 0 = N
with value – 1 | 8 = 9 and 0 | 8 = 8
If you are using PORTA and want to Set pin 17 without changing rest of bits than by using OR Operator: PORTA |= (1<<17) you can change that bit.
Bitwise AND ( & ) : Bitwise AND operator in C is denoted by ‘&’. When 2 numbers are operated as AND then each pair of ‘corresponding’ bits in both numbers are operated as AND. Consider two 4-bit binary numbers ANDed : 1010 & 1101 , here nth bit of both numbers are operated as AND to get the result. if A and B are two no AND with each other than if any bit value of A or B is 0 than resultant bit value is zero.
Ex. 1 & 8 = 0 and 1 & 9 = 1
(0b10001000) & (0b01111000) = 0b00001000
If you are using PORTA and want to Clear pin 17 without changing rest of bits than by using AND Operator: PORTA &= ~ (1<<17) you can change that bit.
Bitwise Negation ( ~) :
If we need to convert all 1 in a bit to 0 and all 0 to 1 than that will be done by using operator ~ . if 1001 is operated with ~ than the result will be 0110.
in Hex No if ~(0x0F0F00) than result is 0xF0F0FF
If you are using PORTA and want to toggle all bits of a port than
PORTA = ~ PORTA will be use.
XOR Operator( ^ ): XOR is short for eXclusive-OR. By definition of XOR , the result will be a ’1′ if both the input bits are different and result will be ’0′ if both are same (as seen in the table few paragraphs above). XOR can be used to check the bit difference between 2 numbers. if XOR is operated between two no A and B and if bit value of A and B are same than result will be 0 and if they are different than result will be 1.
(0b01101001) ^ ( 0b00001111) = 0b10010110
Setting a bit
Use the bitwise OR operator (|) to set a bit.
number |= 1 << x;
That will set bit x.
Clearing a bit
Use the bitwise AND operator (&) to clear a bit.
number &= ~(1 << x);
That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.
Toggling a bit
The XOR operator (^) can be used to toggle a bit.
number ^= 1 << x;
That will toggle bit x.
Checking a bit
To check a bit, AND it with the bit you want to check:
bit = number & (1 << x);