Challenge - 5 Problems
Verilog Ternary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of simple ternary operator
What is the output value of
result after this Verilog code runs?Verilog
module test; reg [3:0] a = 4'd7; reg [3:0] b = 4'd10; wire [3:0] result; assign result = (a > b) ? a : b; endmodule
Attempts:
2 left
💡 Hint
Check which value is greater between a and b.
✗ Incorrect
The ternary operator checks if a > b. Since 7 is not greater than 10, it returns b which is 10.
❓ Predict Output
intermediateNested ternary operator output
What is the value of
out after this code executes?Verilog
module test; reg [3:0] x = 4'd5; wire [3:0] out; assign out = (x < 3) ? 4'd1 : (x < 7) ? 4'd2 : 4'd3; endmodule
Attempts:
2 left
💡 Hint
Evaluate conditions from left to right.
✗ Incorrect
x=5 is not less than 3, so first condition is false. Then check if x < 7, which is true, so out = 2.
🔧 Debug
advancedIdentify the error in ternary operator usage
What error does this Verilog code produce?
Verilog
module test; reg [3:0] a = 4'd2; reg [3:0] b = 4'd3; wire [3:0] result; assign result = a > b ? a : b : 4'd0; endmodule
Attempts:
2 left
💡 Hint
Check the number of ':' in the ternary expression.
✗ Incorrect
The ternary operator syntax is condition ? true_expr : false_expr. Here there are two ':' which is invalid syntax.
🧠 Conceptual
advancedUnderstanding ternary operator precedence
Given the expression
a ? b : c ? d : e in Verilog, how is it evaluated?Attempts:
2 left
💡 Hint
Ternary operators associate from right to left.
✗ Incorrect
Ternary operators in Verilog associate right to left, so the expression is parsed as a ? b : (c ? d : e).
🚀 Application
expertUsing ternary operator for conditional bit assignment
What is the value of
out after this code runs?Verilog
module test; reg [1:0] sel = 2'b10; wire [3:0] out; assign out = (sel == 2'b00) ? 4'd1 : (sel == 2'b01) ? 4'd2 : (sel == 2'b10) ? 4'd4 : 4'd8; endmodule
Attempts:
2 left
💡 Hint
Match sel value with conditions in order.
✗ Incorrect
sel is 2'b10, so the third condition is true, assigning out = 4.
