Skip to main content
Logo image

Dive Into Systems: Exercises

Section 4.6 Bitwise Operators

Checkpoint 4.6.1. Bit Shifting.

    Which of the following expressions best represents the result of shifting a binary number (x) left by 1 place?
  • \(x * 2\)
  • Correct!
  • \(x + 2\)
  • Incorrect.
  • \(2 / x\)
  • Incorrect.
  • \(x / 2\)
  • Incorrect.
  • \(x^2\)
  • Incorrect.
  • \(2^x\)
  • Incorrect.

Checkpoint 4.6.2. Bit Shifting.

    Which of the following expressions best represents the result of shifting a binary number (x) left by n places?
  • \(x * 2^n\)
  • Correct!
  • \(x * n\)
  • Incorrect.
  • \(x * n^2\)
  • Incorrect.
  • \(x * 2*n\)
  • Incorrect.
  • \(x * 2\)
  • Incorrect.
  • None of these options
  • Incorrect.

Checkpoint 4.6.3. AND Operations.

    For any negative 8-bit two’s complement number (var), which of the following expressions will always produce a non-zero result?
  • var & 0xFF
  • Correct!
  • var && 0x10
  • Correct!
  • var & 0x10
  • Incorrect.
  • var & 0x01
  • Incorrect.
Hint.
&& is logical AND and & is bitwise AND

Checkpoint 4.6.4. Left Shift.

    Given an unsigned binary value x, under which conditions is it possible that shifting x to the left by 1 (x << 1) produces a result that is smaller than x?
  • If the high-order bit of x is 1
  • Correct!
  • If the high-order bit of x is 0
  • Incorrect.
  • If the low-order bit of x is 1
  • Incorrect.
  • If the low-order bit of x is 0
  • Incorrect.
  • This can never happen
  • Incorrect.

Checkpoint 4.6.5. Right Shift.

    How is it determined whether a right shift will be logical or arithmetic in C?
  • It depends on whether the variable is declared as a signed or unsigned type.
  • Correct!
  • There are separate C operators for the two types of right shift.
  • Incorrect.
  • It depends on the size of the variable (e.g. int vs char).
  • Incorrect.
  • None of these options.
  • Incorrect.

Checkpoint 4.6.6. Changing Signs.

    Given X, an N-bit signed binary number, which of the following operations might result in X switching signs (e.g. negative to positive or vice versa)?
  • x = x << 1
  • Correct!
  • x = x >> 1
  • Incorrect.
  • x = ~x
  • Correct!
  • x = &x
  • Incorrect.
  • x = x|x
  • Incorrect.
  • x = x^x
  • Correct!

Checkpoint 4.6.7. Bit Shifting.

What is the output of the following C program? Note that an unsigned short is 2 bytes, and the format %04x prints a 4-digit hexadecimal value.
int main(int argc, char **argv) {
   unsigned short u_val;

   u_val = 0x080F;
   printf("%04x\n", u_val << 4);
   printf("%04x\n", u_val << 1);

   return 0;
}

Checkpoint 4.6.8. Bitwise Operations.

Given the following binary values of x and y:
x = 0b01111010
y = 0b00001111
What is the result of each of the following operations?
x & y =
x | y =
x ^ y =
~x =
Hint.
See ch4.6.1- 4.6.4 for the definitions of those bitwise operators.

Checkpoint 4.6.9. Bitwise Operators.

Hint.
Toggle means to switch the state (if it is off, it turns it on, and vice versa)

Checkpoint 4.6.10. Bit-Encoding Colors.

Checkpoint 4.6.11. Bitwise and Logical NOT.

Predict the output of the following C code.
#include <stdio.h>

int main() {
  char x;
  x = 0x12;
  printf("%x\n", !x);
  printf("%x\n", ~x);

  return 0;
}
!x =
~x =