Challenge - 5 Problems
Shift Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this left shift operation?
Consider the following Verilog code snippet. What is the value of
result after the shift operation?Verilog
module test; reg [7:0] a = 8'b0001_0011; // decimal 19 reg [7:0] result; initial begin result = a << 2; end endmodule
Attempts:
2 left
💡 Hint
Left shift moves bits to the left and fills with zeros on the right.
✗ Incorrect
The original value is 00010011 (decimal 19). Shifting left by 2 moves bits two places left: 01001100 (decimal 76).
❓ Predict Output
intermediateWhat is the output of this right shift operation?
Given the Verilog code below, what is the value of
result after the right shift?Verilog
module test; reg [7:0] a = 8'b1100_1000; // decimal 200 reg [7:0] result; initial begin result = a >> 3; end endmodule
Attempts:
2 left
💡 Hint
Right shift moves bits to the right and fills with zeros on the left for unsigned values.
✗ Incorrect
Original value 11001000 (200 decimal). Shifting right by 3 bits gives 00011001 (25 decimal).
❓ Predict Output
advancedWhat is the output of this arithmetic right shift on a signed value?
In Verilog, consider this signed 8-bit value and the arithmetic right shift operation. What is the value of
result?Verilog
module test; reg signed [7:0] a = -8'sd16; // binary 1111_0000 reg signed [7:0] result; initial begin result = a >>> 2; end endmodule
Attempts:
2 left
💡 Hint
Arithmetic right shift preserves the sign bit (leftmost bit).
✗ Incorrect
The value -16 in 8-bit signed is 11110000. Arithmetic right shift by 2 keeps sign bits: 11111100 (-4 decimal).
❓ Predict Output
advancedWhat is the result of this shift with a variable shift amount?
Given the Verilog code below, what is the value of
result after shifting?Verilog
module test; reg [7:0] a = 8'b0001_1111; // decimal 31 reg [2:0] shift_amt = 3'd4; reg [7:0] result; initial begin result = a << shift_amt; end endmodule
Attempts:
2 left
💡 Hint
Shifting left by 4 moves bits 4 places left, filling with zeros on the right.
✗ Incorrect
31 decimal is 00011111. Shift left by 4 gives 11110000 (decimal 240).
🧠 Conceptual
expertHow many bits are set to 1 after this combined shift operation?
Consider this Verilog code snippet. How many bits in
result are set to 1 after execution?Verilog
module test; reg [7:0] a = 8'b1010_1010; reg [7:0] result; initial begin result = (a << 1) >> 2; end endmodule
Attempts:
2 left
💡 Hint
Perform the shifts step-by-step and count the 1 bits in the final result.
✗ Incorrect
a = 10101010. Left shift by 1: 01010100. Right shift by 2: 00010101. Counting 1 bits: 3 bits set.
