Bit Manipulation in embedded
admin1 CommentHi Guy’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 viceverse. 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.
Decimal  Binary  Hex 
0  0000  0 
1  0001  1 
2  0010  2 
3  0011  3 
4  0100  4 
5  0101  5 
6  0110  6 
7  0111  7 
8  1000  8 
9  1001  9 
10  1010  A 
11  1011  B 
12  1100  C 
13  1101  D 
14  1110  E 
15  1111  F 
To convert C2AB to binary:
C = 1100, 1 = 0010, A = 1010, 8 = 1000
When we put the pieces together, we get: C2A8_{16} = 1100001010101000_{2}
Converting Binary to Hexadecimal:
To convert 10101110101111111010_{2} 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 AE3FA_{16}
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:
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.
Apart from a few days routine immediately after going back household, My spouse and i included a different: looked at your webpage within the search for brandnew entries. I really like your website, each access I study having bated breath of air!