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

Mask Operations for bit manipulations in Registers Given for all questions .A RE

ID: 3914372 • Letter: M

Question

Mask Operations for bit manipulations in Registers Given for all questions .A REG is name of a 16-bit register in an MCU · Mask size needs to be same (16 bit) and should be specified in hex. For all questions, show your work. Only partial credit for just the answer. I. A_REG and mask have some value. Just write the specific lines of C code to do task. Do not need #include library or main(). Write C code to print "Alert" if at least one of the flag bits specified in mask is Set. a. b. Write C code to print "Alert" if none of the flag bits are Set (all flags clear). Write C code to Set all the flag bits specified in the mask (no change to other bits). c. Write C code to Clear all the flag bits specified in the mask (no change to other bits). d.

Explanation / Answer

For working with masks in C, you would want to use bitwise operations to perform boolean arithmetic.
To mask the register we simply need to "AND" A_REG and the mask.
//C Code
output = A_REG & mask;
a. If even a single flag bit is set the output is going to be non zero.
//C Code
if(output > 0)
printf("Alert ");

b. But if not a single flag bit is set the output is zero. So we simply check if output is zero or not.
//C Code
if(output == 0)
printf("Alert ");

c. If all the flag bits need to be set as specified by the mask, then simply setting the output as the "OR" of mask and A_REG will ensure that all the required bits are set to 1.
//C Code
output = A_REG ^ mask;

d. To set the flag bits off, we use negation of the mask. The negation of mask represents the bits we want to preserve. ~ (tilde) is used to perform bitwise negation.
//C Code
output = A_REG ^ (~mask);