ProgrammingHardHCL · Tech Mahindra pattern
Assume 32-bit int. What does this snippet do, and what is printed for x=10?
Assume 32-bit int. What does this snippet do, and what is printed for x=10?
int x = 10;
int y = x & (x - 1);
printf("%d", y);- A. 10
- B. 8
- C. 2
- D. 0
Correct answer: B. 8
x & (x-1) is the classic trick to clear the lowest set bit of x. 10 is 1010; 9 is 1001; ANDing them gives 1000 = 8, which is 10 with its lowest set bit (the '2' bit) turned off.
More C Programming questions
- 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);
- Assume 32-bit int. What is printed? c int a = -1; unsigned int b = a; printf("%u", b);