Prev Problem
Next Problem

43. Guard Division by Zero

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

Using ternary operators, each of the 3 outputs can be appropriately assigned values based on the if - then - else property of ternary operators. 

 

Code

module safe_div8 (
    input  [7:0] dividend,
    input  [7:0] divisor,
    output [7:0] q,
    output [7:0] r,
    output       div_by_zero
);
    // TODO: implement with ternary (?:)
    assign q = (~|divisor)? 8'd0 : dividend/divisor; 
    assign r = (~|divisor)? 8'd0 : dividend%divisor; 
    assign div_by_zero = (~|divisor)? 1'b1 : 1'b0; 

endmodule

 

Was this helpful?
Upvote
Downvote