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);- A. 1
- B. 0
- C. 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
- Assume 32-bit int. What does this snippet do, and what is printed for x=10? c int x = 10; int y = x & (x - 1); printf("%d", y);
- Assume 32-bit int. What is printed? c #define TOGGLE(flags, bit) ((flags) ^= (1 << (bit))) int flags = 0b0110; TOGGLE(flags, 1)…
- Assume 32-bit int. What is printed? c int count = 0; unsigned int x = 0xF0F0F0F0; while (x) { count += x & 1; x >>= 1; } printf("%d"…
- Assume 32-bit int. What is printed? c int x = 5; int y = x & -x; printf("%d", y);
- Assume 32-bit int. What is printed? c unsigned char a = 200; unsigned char b = 100; unsigned char c = a + b; printf("%u", c);
- Assume 32-bit int. What is printed? c int flags = 0; flags |= (1 << 2); flags |= (1 << 5); flags &= ~(1 << 2); printf("%d", flags);