LearnFrenzy
Sign in Take a free test

Bitwise Operators · Predict the Output · Question 8

Extracting nibbles and bytes with shifts and masks Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    unsigned x = 0xABCD;
    printf("%X %X %X\n", (x >> 12) & 0xF, (x >> 4) & 0xFF, x & 0xF);
    return 0;
}
  • A A BC D Correct
  • B A AB D
  • C D BC A
  • D AB CD D
Correct Answer: Option A

Each hex digit is 4 bits. Shift right by 12 and keep 4 bits: A. Shift right by 4 and keep 8 bits: BC. Keep the lowest 4 bits: D.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Popular