I need help with the following problem. I just need to assign values to the part
ID: 671437 • Letter: I
Question
I need help with the following problem. I just need to assign values to the part in the code where it has the question marks. The program is supposed tp test if the 2nd bit is 1 and the 5th bit is 0 in a given word of 16 bits. If this is the case, it'll print true. Any help would be appreciated. Thanks!
#include <stdio.h>
int main() {
unsigned short int i, j, mask1, mask2;
printf("input a 16-bits word in hex ");
scanf("%x",&i); // Use “x” for inputting a hexadecimal value
mask1 = 0x0012;
mask2 = ?????;
j = i ??bit-op?? mask1;
j = j ??bit-op?? mask2;
/* j is 0 if there is a match of the bits in the same position, */
/* j is 1 if there is a mismatch of bits in the same position. */
if (j == 0)
printf("true ");
else printf("false ");
}
Explanation / Answer
#include <stdio.h>
int main() {
unsigned short int i, j, mask1, mask2;
printf("input a 16-bits word in hex ");
scanf("%x",&i); // Use “x” for inputting a hexadecimal value
mask1 = 0x0012;
mask2 = 0x0002;
j = i & mask1;
j = j ^ mask2;
/* j is 0 if there is a match of the bits in the same position, */
/* j is 1 if there is a mismatch of bits in the same position. */
if (j == 0)
printf("true ");
else printf("false ");
}