ProgrammingHardWipro · Cognizant pattern

Assume 32-bit int. What is the value of n that checks whether the k-th bit (0-indexed from the LSB) of x is set, for x=44 and k=3, and what does the expression evaluate to?

Assume 32-bit int. What is the value of `n` that checks whether the k-th bit (0-indexed from the LSB) of x is set, for x=44 and k=3, and what does the expression evaluate to?
int x = 44, k = 3;
int set = (x >> k) & 1;
printf("%d", set);
  1. A. 1
  2. B. 0
  3. C. 4
  4. D. 8

Correct answer: A. 1

44 in binary is 101100. Shifting right by 3 gives 101 (=5); masking with & 1 isolates just the lowest bit of that shifted value, which is 1 — so bit 3 of 44 (counting from 0) is indeed set (44 = 32+8+4, and bit 3 is the '8' bit, which is set since 8 is part of 44's sum).

More C Programming questions

Exams that ask this topic