ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
#define SET_BIT(x, n) ((x) | (1 << (n)))
#define CLEAR_BIT(x, n) ((x) & ~(1 << (n)))
#define TOGGLE_BIT(x, n) ((x) ^ (1 << (n)))
int main(void) {
int flags = 0b0101;
flags = SET_BIT(flags, 1);
flags = CLEAR_BIT(flags, 0);
flags = TOGGLE_BIT(flags, 3);
printf("%d", flags);
}- A. 14
- B. 7
- C. 4
- D. 6
Correct answer: A. 14
Start 0101 (5). SET_BIT(_,1): 0101|0010=0111 (7). CLEAR_BIT(_,0): 0111 & ~0001 = 0111 & 1110 = 0110 (6). TOGGLE_BIT(_,3): 0110 ^ 1000 = 1110 (14). Final printed value is 14.
More C Programming questions
- 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…
- What is printed? c #define CONCAT(a, b) a##b int xy = 42; printf("%d", CONCAT(x, y));
- What is printed? c #if defined(STDC) && STDCVERSION >= 199901L int x = 10; #else int x = 20; #endif printf("%d", x);
- What is printed? c #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define CLAMP(x, lo, hi) MIN(MAX(x, lo), hi) #define MAX(a, b) ((a) > (b) ?…