Assume that two bytes of a string variable are labeled D10 and D1 and that they
ID: 3796830 • Letter: A
Question
Explanation / Answer
3-6 Bit manipulation
Step 1: Declare three string variables as unsigned int viz D10, D1 and D10_1
Step 2: Input D10 and D1 the desired values.
Step 3: Using Bitwise AND ( & ) operator we retain the LSB of the strings of D10 and D1.
Procedure:
Consider D10 = 32 (Binary Value = 0011 0010)
D5 = 39 (Binary Value = 0011 1001)
1)Perform And Operation between D10 and Hex character 'F' and assign it to variable A
2)1)Perform And Operation between D1 and Hex character 'F' and assign it to variable B
i.e
A = D10 & 'F' makes you mask only the first 4 bits of the string D10 ( In Binary language it computes as
0011 0101 & 0000 1111 = 0000 0101 = 5 = D10)
B = D1 & 'F' makes you mask only the first 4 bits of the string D1 ( In Binary language it computes as 0011 1001 & 0000 1111 = 0000 1001 = 9 = D1)
Step 4 :
In order to concatenate the numbers A and B to appear as forming D10_1. We use the folllwing set of operations to get it done. And here it invokes Bitwise OR (|) operator for the procedure.
Procedure:
1) Use Bit left shift operator (<<) to move one string's LSB values to its MSB values
i.e eg : if A = 5 = 0000 0101 as per the previous stated example and also is the expected inputs/values on which actually the operation to be carried out.
then A<<4 yields you to a value 50 = 0101 0000
Note : The above shift operation to be done only once where in we do it in order to make the D10 as a MSB of D10_1
And the updated value of A is bitwise OR(|) operated with the value of B.
i.e A = 50 , B = 9
A|B = 59
A|B = 0100 0000 | 0000 1001 = 0100 1001 = 59
2) Assign the value of A|B to D10_1.
D10_1 = 59
Hence the Result.