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

Question 1 (10 pts): Binary operations, 2pts each Write a single line of C code

ID: 3907936 • Letter: Q

Question

Question 1 (10 pts): Binary operations, 2pts each Write a single line of C code to implement each of the following. Assume the following declarations unsigned char ?pr unsianed short n; Example: For ch, check if bit 0 is set to 1 a. Set bits 7, 2, 0 of?a to 1, and preserve the remaining bits. b. For ch, check if any of bits 5, 3, 1, 0 are Set to 1. c. For n, Set bits 15, 11 to 1, Clear bits 7, 3 to 0, Toggle bits 9, 2, and preserve the remaining bits. d. For n, check if all bits 14, 13 are Set to 1, all bits 11, 7, 3 are Cleared to 0, any of bits 6, 5, 4 are Set to 1, any of bits 15, 0 are Cleared to 0 e. For ch, rotate the bits to the right one position (bit 0 should end up at bit position 7), this is also known as a right-barrel-shift operation.

Explanation / Answer

a. To set the 0,2, and 7 th bit as 1 we have to perform logical OR operation of 10000101 with ch, as,

ch = ch|10000101;

b. For this we have to perform logical AND with 0000001, 00001000, 00100000.

if(ch&0000001 || ch&00001000 || ch&00100000)

printf(" yes ");

c. n is unsigned short, so it has 16 bits.

To set 15, 11 and 1 bits to 1, we perform logical OR with 1000100000000010. To clear bits 7 and 3, we perform logical and with 0000000010001000. and to toggle 9 and 2 we perform NOT with (n & 0000001000000100).

d. if (n&0110000000000000) && (n&0000100010001000) && (n&00000001000000) && (n&0000000000100000) && (n&0000000000010000) )

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote