Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that two bytes of a string variable are labeled D10 and D1 and that they

ID: 3796830 • Letter: A

Question


Assume that two bytes of a string variable are labeled D10 and D1 and that they contain the ASCII code for two digits. If the upper four bits of the ASCII code for a digit are forced to zero, what remains will be the binary value of the digit. Minimizing instructions, write an instruction sequence to form the "packed-BCD" version of this two-digit number into a 1-byte variable called D10_1. Leave the original string variable unchanged. For B'001 10101' (i.e., ASCII codes for 5) and D1 = B'00111001' (i.e., ASCII example, code for 9), then your code sequence should leave D10 and D1 unchanged while forming D10_1 = B'01011001' (i.e., the packed-BCD encoding of the decimal number 59). Minimizing instructions, write an instruction sequence that reverses the operation of the last problem. Note that the upper 4-bit "nibble" of the ASCII encoding of a digit is B'0011'.

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.