LearnFrenzy
Sign in Take a free test

Bitwise Operators · Exercise 2 of 4

Bitwise Operators Predict the Output

  • 12 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 6, b = 3;
    printf("%d %d %d\n", a & b, a | b, a ^ b);
    return 0;
}
A 2 7 5
B 2 7 1
C 1 7 5
D 3 6 5
Question page & discussion
Question 2 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%d %d %d\n", 1 << 4, 100 >> 3, 7 << 1 >> 1);
    return 0;
}
A 16 12 7
B 16 12 14
C 8 12 7
D 16 13 7
Question page & discussion
Question 3 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 5;
    printf("%d %d\n", ~x, ~x + 1);
    return 0;
}
A -6 -5
B -5 -4
C 250 251
D -6 6
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 4;
    if (x & 1 == 0)
        puts("even");
    else
        puts("odd?!");
    return 0;
}
A even
B odd?!
C Compilation error
D Nothing
Question page & discussion
Question 5 Easy

What is the output of the following C program?

main.c
#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;
}
A 12 7
B 7 12
C 11 11
D 0 0
Question page & discussion
Question 6 Medium

What is the output of the following C program?

main.c
#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;
}
A 5
B 4
C 8
D 6
Question page & discussion
Question 7 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    unsigned char c = 0x0F;
    printf("%d %d\n", ~c, (unsigned char) ~c);
    return 0;
}
A -16 240
B 240 240
C -16 -16
D 240 -16
Question page & discussion
Question 8 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    unsigned x = 0xABCD;
    printf("%X %X %X\n", (x >> 12) & 0xF, (x >> 4) & 0xFF, x & 0xF);
    return 0;
}
A A BC D
B A AB D
C D BC A
D AB CD D
Question page & discussion
Question 9 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%d %d\n", (16 & 15) == 0, (18 & 17) == 0);
    return 0;
}
A 1 0
B 0 1
C 1 1
D 0 0
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    char s[] = "LeArN";
    for (char *p = s; *p; p++)
        *p ^= 32;
    puts(s);
    return 0;
}
A lEaRn
B LEARN
C learn
D LeArN
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links