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.
Checkpoint4.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.
Checkpoint4.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
Checkpoint4.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.
Checkpoint4.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.
Checkpoint4.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!
Checkpoint4.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.
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.
Checkpoint4.6.9.Bitwise Operators.
Lights in a lecture hall are controlled electronically through a one-byte variable (unsigned char lights;) in a C program. The low-order bit, \(b_0\text{,}\) is used for podium lights and the remaining bits, \(b_1\) to \(b_7\text{,}\) are each used for a row of lights from rows 1 to 7.
Match each statement to the C code that implements its effect:
lights = lights | 0x4
turns on the lights in row 2
lights = lights & 0x1F
turns off lights in row 5,6,7
lights = lights & 0x00
turns off all the lights
lights = lights | 0x03
turns on podium and row 1 lights
lights = lights | 0x00
does not change the state of the lights
lights = lights ^ 0x01
toggles the value of the podium light
Hint.
Toggle means to switch the state (if it is off, it turns it on, and vice versa)
Checkpoint4.6.10.Bit-Encoding Colors.
RGB color is often encoded as a 24-bit value where 8 bits each are used to encode the amount of Red, Green, and Blue in a color value.
Given the following hex representation of an RGB value:
// RGB color: 0xRRGGBB
unsigned int color;
Match each statement to the C code that implements its effect:
(color >> 8) & 0x0FF
Retrieve the green component
(color >> 16) & 0x0FF
Retrieve the red component
color & 0x0FF
Retrieve the blue component
Checkpoint4.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;
}