22 Apr 2017

Bit Manipulation in embedded


HiGuy’s this is another important tutorial on Bitwise Operators. Knowledge of Hexadecimal and Binary Numbering system is required along with conversion from binary to hex and vice-verse. We are going to discuss about nibble, bit, byte, word, bitwise operators etc.
There are basically 6 types of Bitwise operators, as follow:
  • 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 ‘^

Everything we do with computers is the ones and zero’s. Which is called bits, four bits together is called Nibble, eight bits together is called byte, 16 bit together is called word and 32 bit together is called Dword. For further please check the image below:
Truth Tables for different bitwise operators as follows:
AND

OR
A
B
A.B
A
B
A+B
0
0
0
0
0
0
0
1
0
1
0
1
1
0
0
0
1
1
1
1
1
1
1
0
Hexadecimal to binary and vice versa conversion:
Converting Hexadecimal to Binary:
Remember hex uses groups of four bits, so we can use the table below for conversions.
DecimalBinaryHex
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
To convert C2AB to binary:
C = 1100, 1 = 0010, A = 1010, 8 = 1000
When we put the pieces together, we get: C2A816 = 11000010101010002
Converting Binary to Hexadecimal:
To convert 101011101011111110102 to base 16, we first break up the number into groups of four as shown below:
1010 1110 1011 1111 1010
Now assign each group its corresponding hex value
1010 = A, 1110 = E, 0011 = 3, 1111 = F, 1010 = A
When put together, we get AE3FA16
Bitwise operator used to manipulate the bits
  • Bitwise AND is used to clear a particular bit, without affecting the rest
  • Bitwise OR is used to set a particular bit, without affecting the rest
  • Bitwise XOR is used to toggle a particular bit
  • The shift operators are used along with bitwise AND, OR and XOR to manipulate bits, or to break up multiplication/division.
  • One’s complement is also used with the other operators to manipulate bits
Bit Fields
Another interesting thing that can be done using bits is to have a bit field. Bit Fields allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium. Typical examples:
?
1
2
3
4
5
6
7
8
9
struct date_struct {
 
BYTE day   : 5,   // 1 to 31
 
month : 4,   // 1 to 12
 
year  : 14;  // 0 to 9999
 
} date;

In this example, the day field takes up the lowest 5 bits, month the next four, and year the next 14 bits. So we can store the date structure in twenty three bits, which is contained in three BYTEs. The twenty fourth bit is ignored. If I had declared it using an integer for each field, the structure would have taken up 12 BYTEs.
Now lets pick this declaration apart to see what we are doing.
First we will look at the data type we are using for the bit field structure. In this case we used a BYTE. A BYTE is 8 bits, and by using it, the compiler will allocate one BYTE for storage. If however, we use more than 8 bits in our structure, the compiler will allocate another BYTE, as many BYTEs as it takes to hold our structure. If we had used a WORD or DWORD, the compiler would have allocated a total of 32 bits to hold our structure.
Now lets look at how the various fields are declared. First we have the variable (day, month, and year), followed by a colon that separates the variable from the number of bits that it contains. Each bit field is separated by a comma, and the list is ended with a semicolon.
Now we get to the struct declaration. We put the bit fields into a struct like this so that we can use convention structure accessing notation to get at the structure members. Also, since we can not get the addresses of bit fields, we can now use the address of the structure.

No comments:

Post a Comment

commnet here