Negate a byte in two's complement with an inverter and an add of one, and flag the one value with no negative.
Hardware context: A signed sensor sample widened into a calibration adder, and a byte negator for a motor driver. Build the two's-complement negator and flag the most negative byte.
A motor driver takes a speed as a signed byte. To reverse the motor, the controller needs the negative of that speed. The standard cell library has inverters and an adder, and no "minus" block.
This is an Implement problem. Write negate8. The output y is the negative of x in two's complement. Build it the way the hardware does: invert every bit, then add one.
One input has no correct answer in eight bits. For that input, y repeats the input pattern and the flag no_twin is 1. For every other input the flag is 0.
The check applies eight values of x, one per nanosecond, and records y and no_twin after each.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 64 | input of each check |
Full specification
module negate8 (
input logic [7:0] x, // a two's-complement number
output logic [7:0] y, // the negative of x, in two's complement
output logic no_twin // 1 when the negative of x does not fit in 8 bits
);
// your design
endmodule
| Output | Rule |
|---|---|
| y | The two's-complement negative of x. Zero stays zero. |
| y for the input with no twin | The same eight bits as x. |
| no_twin | 1 only when the true negative of x does not fit a signed byte. 0 for every other x, zero included. |
The result fields are named y_step0 and no_twin_step0 for the first step, up to y_step7 and no_twin_step7. Each y is shown as a signed decimal number.
Examples
Example 1
Small Positive and Negative Values- Input
x = 1; x = -1; x = 5; x = -5; x = 2; x = -2; x = 100; x = -100
- Output
y_step0 = -1, no_twin_step0 = no, y_step1 = 1, no_twin_step1 = no, y_step2 = -5, no_twin_step2 = no, y_step3 = 5, no_twin_step3 = no, y_step4 = -2, no_twin_step4 = no, y_step5 = 2, no_twin_step5 = no, y_step6 = -100, no_twin_step6 = no, y_step7 = 100, no_twin_step7 = no
Each value comes out with the opposite sign: 1 gives -1 and -1 gives 1. The flag stays 0 for all of them.
Example 2
Zero Between Other Values- Input
x = 0; x = 3; x = 0; x = -3; x = 0; x = 64; x = 0; x = -64
- Output
y_step0 = 0, no_twin_step0 = no, y_step1 = -3, no_twin_step1 = no, y_step2 = 0, no_twin_step2 = no, y_step3 = 3, no_twin_step3 = no, y_step4 = 0, no_twin_step4 = no, y_step5 = -64, no_twin_step5 = no, y_step6 = 0, no_twin_step6 = no, y_step7 = 64, no_twin_step7 = no
Zero negates to zero. The flag stays 0, because zero does fit.
Example 3
The Most Negative Value and Its Neighbours- Input
x = -128; x = -127; x = 127; x = -128; x = 126; x = -126; x = -128; x = 0
- Output
y_step0 = -128, no_twin_step0 = yes, y_step1 = 127, no_twin_step1 = no, y_step2 = -127, no_twin_step2 = no, y_step3 = -128, no_twin_step3 = yes, y_step4 = -126, no_twin_step4 = no, y_step5 = 126, no_twin_step5 = no, y_step6 = -128, no_twin_step6 = yes, y_step7 = 0, no_twin_step7 = no
The flag is 1 in the three steps where x is -128, and y repeats the input pattern there. The neighbours -127 and 127 negate normally.
Constraints and Contract
- Inputs are known 0/1 values. Each value is held for one nanosecond and the outputs are sampled at its end.
- Keep the module name and the ports. The design is combinational: no clock and no stored value.
- The minus operator is refused before the design runs: build the negative from ~ and + 1.
Hints
Hint 1
The bitwise NOT operator inverts every bit of x. After that step the value is one short of the negative.
Hint 2
Add the constant one as an 8-bit literal. The carry runs up from bit 0 on its own.
Hint 3
A signed byte runs from -128 to 127. Find the one value of x whose negative is outside that range, and compare x with it.
What the grader checks: Six directed and eight seeded cases of eight steps each run on the block. Both outputs are compared with a model written in JavaScript after every step. Before that, a source rule refuses any minus sign outside comments.
14 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 3 steps and the final capstone