ProgrammingHardWipro · Cognizant pattern
What is printed? (Assume int is 32 bits, two's complement, arithmetic right shift for signed negatives — the common but implementation-defined behaviour.)
What is printed? (Assume int is 32 bits, two's complement, arithmetic right shift for signed negatives — the common but implementation-defined behaviour.)
int a = -8;
int b = a >> 1;
printf("%d", b);- A. 2147483644
- B. -4
- C. 4
- D. -16
Correct answer: B. -4
Right-shifting a negative signed int is implementation-defined, but virtually all mainstream compilers (GCC/Clang/MSVC) perform an ARITHMETIC shift, replicating the sign bit: -8 >> 1 = -4 (equivalent to dividing by 2, rounding toward negative infinity for this value).
More C Programming questions
- What is printed? (Check if a number is a power of two.) c int isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; } int main(void) {…
- What is printed? c int swaptrick(int a, int b) { a = a ^ b; b = a ^ b; a = a ^ b; printf("%d %d ", a, b); return a + b; } int main(void) {…
- What is printed? c #define SETBIT(x, n) ((x) | (1 << (n))) #define CLEARBIT(x, n) ((x) & ~(1 << (n))) #define TOGGLEBIT(x, n) ((x) ^ (1 <<…
- What is printed? c #define SQUARE(x) x x int main(void) { int a = 5; printf("%d", SQUARE(a + 1)); }
- What is printed? c #define TWICE(x) x + x printf("%d", 2 TWICE(3));
- What is printed? c #define LOG(msg) printf("[LOG] %sn", msg) #ifdef DEBUG #define TRACE(msg) LOG(msg) #else #define TRACE(msg) #endif int…