Further , I assume that the reader has a basic understanding of C programming language , Digital Logic , MCU(Registers,etc..).
Embedded Programming Basics : Bit level Operations in C:
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 ‘^‘
Below is the Truth table for OR , AND , XOR – each of them require 2 operands:
|Operand 1(OP1)||Operand 2(OP2)||OP1 | OP2 (OR)||OP1 & OP2 (AND)||OP1 ^ OP2 (XOR)|
Consider: int n = 0x7F2; Here its is implied that n will be 0x000007F2 since n is a 32bit number. Here number will be automatically padded with Zeros form right hand side as required.
Consider : int x = 0b1010; Here too zeros (28 0s) will be padded on the left to make x 32bit.
Consider: int n = (1<<3); Here the 3rd bit will be 1 and rest all will be made zeros by the compiler.
In short 0s are padded towards the left as required.
Consider there is a 32 bit register which is connected to 32 pins. Changing any bit to 1 will produce a HIGH (LOGIC 1) and making it ’0′ will produce a LOW i.e LOGIC 0 on the corresponding bit. If we assign bit number 19(from right) a ’1′ then pin 19 will produce a HIGH.
In C this can be done as :
REG = 0b10000000000000000000; //same as above since leading 0s will be automatically padded
As you can see using binary number directly is a headache – specially when there are like 32 bits to handle. Using Hexadecimal instead makes it a bit easier and using Left shift operator makes it super simple. Here ‘<<' is called the Left Shift Operator. Similar to this is ">>” viz. Right Shift operator.
“(1<<19)” Simply means ‘Shift 1 towards the LEFT by 19 Places‘. Other bits will be Zero by default.
We generally use Hexadecimal when we need to change bits in bluk and Left shift operator when only few bits need to be changed or extracted.
ORing in C:
This is the same exact thing as in Digital Logic i.e 1 ORed with ‘x’ is always 1 and 0 ORed with ‘x’ is always ‘x’ , where x is a bit. Lets take two 4-bit numbers and OR them. Consider two 4-bit numbers n1=0100 & n2=1001. Here the 1st bit of n1 will be ORed with 1st bit of n2 , 2nd bit of n1 will be ORed with 2nd bit of n2 and soo on. In this case n1 is decimal 4 and n2 is decimal 9.
|n1 (4) =>||0||1||0||0|
|n2 (9) =>||1||0||0||1|
|ORed Result (13) =>||1||1||0||1|
Now , If we want to make bit 19 and 12 as ’1′ we can use the binary OR operator which is represented by a pipe i.e ‘|’.
Now consider we want to making the first 21 bits (from right) to ’1′. This is can be done using hexadecimal notation instead of using shift operator since we will need to write 21 left shift operations for each of the 21 bits. In this case just consider we have a 32 bit number which has all first 19 bits ’1′. Then convert this number to hexadecimal and use it!
Hence using Hexadecimal or Binary operator depends on the situation.
Bitwise 1′s Complement / Negation in C :
Now lets say.. we need to convert all 0s to 1s and vice-verse. This can be done using the Bitwise negation operator denoted by ‘~’. The result of this operation is called 1′s Complement. Its also called a ‘NOT’ operation. ‘~’ is a unary operator since it requires only 1 operator while rest all are binary operators.
1st lets take a 4bit example to keep it simple.
Consider a binary number 1101. Its Negation will be ~(1101) => 0010.
Now lets get back to 32 bit numbers.. In C it can be done as follows:
REG = ~(x); // REG is assigned 0xF000FFF0;
ANDing in C:
Binary AND operator in C is denoted by ‘&’. When 2 numbers are ANDed each pair of ‘corresponding’ bits in both numbers are ANDed. Consider two 4-bit binary numbers ANDed : 1010 & 1101 , here nth bit of both numbers are ANDed to get the result. Here the same truth table(already shown above) is followed as in Therotical Digital Logic i.e 1 ANDed with x is always ‘x’ (x is a binary number.. a bit) and 0 ANDed with ‘x’ is always 0.
n1 = 0xF4; // binary n1 = 0b11110100;
n2 = 0x3A; // binary n2 = 0b00111010;
n3 = n1 & n2; // binary n3 = 0b00110000; i.e 0x30;
XORing in C:
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 or Registers.
n1 = 0b10011;
n2 = 0b11010;
n3 = n1^n2; // n2 = 0b01001;
Working with Read/Write Registers in C:
Generally its a bad Idea to assign a value directly to Registers since doing so may change the value of other bits which might be used to contol some other hardware.
Consider an 8 bit Register say REGT_8b is used to Start/Stop 8 different Timers. Bit 0 (from left) controls Timer 0 , Bit 1 Controls Timer 1 and so on… Writing a ’1′ to a bit will Start the timer and a ’0′ will Stop the Timer.
Now lets say Timers 7,6,5 are started and others are stopped. So the current value of REGT_8b will be ‘11100000‘. Now assume that we want to Start Timer 2. We can do this in a manner which doesnt affect the other bits as follows :
Now lets say we want to Stop Timer 6. This can be achieved as follows :
Here (1<<6) will be 01000000 (considering it 8bit.. for 32 bit 0s will be padding on left). Then ~(1<<6) will be ~(01000000) = 10111111 and finally when its ANDed with current value of REGT_8b 6th bit of REGT_8b will be set to ’0′ and others will remain as they were since ANDing any bit with 1 doesn’t change its value.
More Examples :
For below examples assume current value of REGT_8b as ’11100011′ which is 8 bit.
1) Stop Timers 0 and 5 :
=> ((1<<0) | (1<<5)) is = ((00000001) | (00100000)) = 00100001
=> ~(00100001) is = 11011110
=> Finally REGT_8b & 11011110 is = (11100011) & (11011110) = 11000010 => Timers 0 and 5 Stopped!
2) Start Timers 3 and 4:
=> ((1<<3) | (1<<4)) is = ((00001000) | (00010000)) = 00011000;
=> Now REGT_8b | (00001000) is = (11100011) | (00011000) = 11111011 => Timers 3 and 4 Started!
3) Stop Timer 7 and Start timer 3:
Above complex expression can be avioded by doing it in 2 steps as:
REGT_8b |= (1<<3); // Start Timer 3
Monitoring Specific bit change in Registers :
Many times we need to read certain Flags in a register that denotes change in Hardware state. Consider a 32 bit Register REGX in which the 12th bit denotes the arrival of data from UART Receive pin into buffer. This data may be a command for the MCU to start or stop or do something. So we need to read the command then interpret it and call appropriate function. In simplest approach we can continuously scan for change in 12th bit of REGX as follows :
Unless the 12th bit of REGX is ’1′ the result of (REGX & (1<<12)) will always be zero. When 12th bit is 1 then (REGX & (1<<12)) will be = (1<<12) which is obviously greater than 0 and hence is evaluated as TRUE condition and the code inside the while loop gets executed.
To monitor for the change in 12th bit from 1 to 0 we just Negate the condition inside while to :
Extracting/Testing Bit(s) from a Register:
To extract a bit from a register we can use a variable in which all other bit locations , except the one we are intereseted in , are forced to 0. This can be done using masks.
Lets assume we want to extract bit number 13. For this we first define a mask in which bit location 13 is 1 and rest are all zeros. Then we AND this mask with the register and save the result in a variable.
mask = (1<<13);
extract_n = REGX & mask; // bit 13 goes to extract_n
If the 13th bit of REGX was 1 then the 13th bit of extract_n will also be one. Similarly for 0. This can be easily extended to extract multiple bits.
To test whether bit 13 of REGX is 0 or 1 we can accomplish it as follows :
This is similar to what we did using mask and monitoring except that the result here is used as condition for ‘if’ construct. Here too ‘(REGX & (1<<13))‘ will evaluate to true if bit 13 is 1 and false if bit 13 is 0.
Filed Under: Embedded