LearnFrenzy
Sign in Take a free test

Bitwise Operators · Exercise 3 of 4

Bitwise Operators Find the Error

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

Each program contains a bit-manipulation bug – undefined shifts, sign problems, or operators that look right but are not. Choose the option that names the real problem.

Question 1 Medium

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

unsigned low_mask(int bits) {        /* mask with the lowest `bits` bits set */
    return (1u << bits) - 1;
}

int main(void) {
    printf("%u\n", low_mask(8));
    printf("%u\n", low_mask(32));
    return 0;
}
A low_mask(8) is wrong
B The function must return int
C For bits == 32, 1u << 32 shifts by the full width of unsigned – undefined behaviour
D There is no bug; it prints 255 and 4294967295
Question page & discussion
Question 2 Medium

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int bit = 31;
    int mask = 1 << bit;
    printf("%d\n", mask != 0);
    return 0;
}
A bit must be unsigned
B mask != 0 is always false
C There is no bug
D 1 is a signed int, and 1 << 31 does not fit in a 32-bit int – undefined behaviour; write 1u << 31
Question page & discussion
Question 3 Medium

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int x = -3;
    printf("%d\n", x << 2);
    return 0;
}
A %d cannot print a shifted value
B The shift amount must be a variable
C x << 2 is undefined because x is negative; use x * 4
D There is no bug; it prints -12
Question page & discussion
Question 4 Easy

The user has only READ permission (flags == 0x1). What does this program print, and why?

main.c
#include <stdio.h>

#define READ  0x1u
#define WRITE 0x2u

int main(void) {
    unsigned flags = READ;
    puts((flags && WRITE) ? "can write" : "read-only");
    return 0;
}
A read-only – the test is correct
B Nothing – it does not compile
C can write – because READ and WRITE overlap
D can write – && only checks that both sides are non-zero; it should be (flags & WRITE)
Question page & discussion
Question 5 Medium

The aim is to clear bit 2 of flags (0b1111). What does the program print, and why?

main.c
#include <stdio.h>

int main(void) {
    unsigned flags = 15;
    flags &= !(1u << 2);
    printf("%u\n", flags);
    return 0;
}
A 0 – !(4) is 0, so the AND clears every bit; it should be ~(1u << 2)
B 11 – bit 2 was cleared
C 15 – nothing changed
D It does not compile
Question page & discussion
Question 6 Easy

Bit 2 of x is set. What does this program print, and why?

main.c
#include <stdio.h>

int main(void) {
    unsigned x = 4;
    if ((x & 4) == 1)
        puts("bit 2 is set");
    else
        puts("bit 2 is clear");
    return 0;
}
A bit 2 is set
B It does not compile
C bit 2 is clear – x & 4 is 4, not 1; compare with != 0 instead
D Nothing is printed
Question page & discussion
Question 7 Hard

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    char buf[2] = {(char) 0x80, 0x01};     /* big-endian 16-bit value 0x8001 */
    int v = (buf[0] << 8) | buf[1];
    printf("%d\n", v);
    return 0;
}
A buf[1] must come first
B The | should be +
C There is no bug; it prints 32769
D char is signed here, so buf[0] is -128; shifting a negative value left is undefined (and sign extension corrupts the result) – read bytes as unsigned char
Question page & discussion
Question 8 Easy

The program should print the area of a square with side 5. What does it print, and why?

main.c
#include <stdio.h>

int main(void) {
    int side = 5;
    int area = side ^ 2;
    printf("%d\n", area);
    return 0;
}
A 25 – ^ means power
B 10 – ^ means multiply
C It does not compile
D 7 – ^ is bitwise XOR, not exponentiation: 101 ^ 010 = 111
Question page & discussion
Up next · Exercise 4Bit Manipulation Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links