ProgrammingVery hardTCS NQT · Infosys
Which expression tests whether bit k of an unsigned x is set?
- A. x & k != 0
- B. (x >> k) & 1
- C. x >> k == 2
- D. x & (1 << k) == 1
Correct answer: B. (x >> k) & 1
Shifting right moves bit k to position 0, and masking with 1 isolates it. The x & (1 << k) == 1 form is wrong because == binds tighter than &, so it compares 1 << k with 1.
More C Programming questions
- int x = 0x1F; What does printf("%d", 07 + 0x1F) print?
- uint32t x = 0xFFFFFFFFu; How many times does while (x > 0) x >>= 1; execute its body, counting from the start?
- int cnt[3] = {0}; int p; for (p = cnt; p < cnt + 3; p++) p = p - cnt; What is the sum of cnt[0] + cnt[1] + cnt[2]?
- A loop while (!(x & 1)) { x >>= 1; c++; } counts the trailing zero bits of a nonzero uint32t x = 0b101000. What is c?
- uint64t mask = (1ULL << 40) - 1; What is mask?
- int a[] = {3, 1, 2}; qsort(a, 3, sizeof(int), cmp) sorts ascending, where cmp compares with (x > y) - (x < y). Then What is a[0]100 +…