What is the output of the following C program?
#include <stdio.h>
int main(void) {
int a = 6, b = 3;
printf("%d %d %d\n", a & b, a | b, a ^ b);
return 0;
}
110 & 011 = 010 (2); 110 | 011 = 111 (7); 110 ^ 011 = 101 (5).
Bitwise Operators · Exercise 2 of 4
Read each program and choose what it prints. Every program was compiled and run with GCC/Clang behaviour on a 64-bit system, so the answers are the real output.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int a = 6, b = 3;
printf("%d %d %d\n", a & b, a | b, a ^ b);
return 0;
}
110 & 011 = 010 (2); 110 | 011 = 111 (7); 110 ^ 011 = 101 (5).
What is the output of the following C program?
#include <stdio.h>
int main(void) {
printf("%d %d %d\n", 1 << 4, 100 >> 3, 7 << 1 >> 1);
return 0;
}
1 << 4 = 16. 100 >> 3 is 100 / 8 rounded down = 12. Shifts group left to right: (7 << 1) >> 1 = 14 >> 1 = 7.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int x = 5;
printf("%d %d\n", ~x, ~x + 1);
return 0;
}
In two's complement (required by C23 and used everywhere) ~x equals -x - 1, so ~5 = −6. Adding 1 gives −5: "flip the bits and add one" is how negation works.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int x = 4;
if (x & 1 == 0)
puts("even");
else
puts("odd?!");
return 0;
}
1 == 0 is evaluated first (0), then x & 0 is 0, so the condition is false even though 4 is even. Compilers warn about it. Write (x & 1) == 0.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int a = 7, b = 12;
a ^= b;
b ^= a;
a ^= b;
printf("%d %d\n", a, b);
return 0;
}
After the first line a holds a ^ b. Then b ^= a gives b ^ (a ^ b) = a, and a ^= b gives (a ^ b) ^ a = b. The values are swapped. (It breaks if both names refer to the same variable.)
What is the output of the following C program?
#include <stdio.h>
int bits(unsigned x) {
int n = 0;
for (; x; x &= x - 1)
n++;
return n;
}
int main(void) {
printf("%d\n", bits(182));
return 0;
}
182 is 10110110 in binary, which has five 1 bits. Each x &= x - 1 removes one of them, so the loop runs 5 times.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
unsigned char c = 0x0F;
printf("%d %d\n", ~c, (unsigned char) ~c);
return 0;
}
c is promoted to the int 15 before ~, so all 32 bits flip: 0xFFFFFFF0 = −16. Casting the result back to unsigned char keeps the low 8 bits, 0xF0 = 240.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
unsigned x = 0xABCD;
printf("%X %X %X\n", (x >> 12) & 0xF, (x >> 4) & 0xFF, x & 0xF);
return 0;
}
Each hex digit is 4 bits. Shift right by 12 and keep 4 bits: A. Shift right by 4 and keep 8 bits: BC. Keep the lowest 4 bits: D.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
printf("%d %d\n", (16 & 15) == 0, (18 & 17) == 0);
return 0;
}
16 = 10000 and 15 = 01111 share no bits, so the AND is 0: a power of two. 18 = 10010 and 17 = 10001 share the top bit (16), so 18 is not a power of two.
What is the output of the following C program?
#include <stdio.h>
int main(void) {
char s[] = "LeArN";
for (char *p = s; *p; p++)
*p ^= 32;
puts(s);
return 0;
}
In ASCII, upper- and lowercase letters differ only in bit 5 (value 32): 'A' is 65, 'a' is 97. XOR with 32 flips that bit, swapping the case of every letter. It would corrupt digits and spaces, so real code checks isalpha() first.
Timed online tests with instant scores and explanations.