Read sign-magnitude and ones'-complement bytes, convert them to and from two's complement, and add with an end-around carry.
Hardware context: A signed sensor sample widened into a calibration adder, and a byte negator for a motor driver. Read sign-magnitude and ones'-complement logs from old sensors by hand.
A new controller must read logs from two old sensors. One wrote its readings in sign-magnitude and the other in ones' complement. Before the converter is built, the team wants the values worked out by hand.
This is an Implement problem with no logic to write. Work out each answer and write it into the answer module as one literal. The first three constants are worked examples.
The check compares every constant with the right value and names the first one that is wrong.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
question | 8 | input of each check |
Full specification
| Constant | What to write |
|---|---|
| SM_85 | The value of the sign-magnitude byte 1000_0101 (sign 1, size 5). |
| OC_FA | The value of the ones'-complement byte 1111_1010 (invert it: 0000_0101). |
| SM_MIN | The most negative value of an 8-bit sign-magnitude number (1111_1111). |
| SM_D1 | The value of the sign-magnitude byte 1101_0001. |
| SM_7F | The value of the sign-magnitude byte 0111_1111. |
| OC_80 | The value of the ones'-complement byte 1000_0000. |
| OC_E3 | The value of the ones'-complement byte 1110_0011. |
| TC_OF_SM_83 | The two's-complement byte with the same value as the sign-magnitude byte 1000_0011. |
| SM_OF_TC_F6 | The sign-magnitude byte with the same value as the two's-complement byte 1111_0110. |
| OC_SUM | The 4-bit ones'-complement sum of 0110 and 1100, end-around carry included. |
| ZEROS | How many 8-bit sign-magnitude patterns mean zero. |
SM_85 = -16'sd5; // a value: a signed decimal number
TC_OF_SM_83 = 8'b0000_0000; // a pattern: binary, as many bits as asked
The values are signed decimal numbers. TC_OF_SM_83, SM_OF_TC_F6 and OC_SUM are binary patterns. Write each answer as one literal: the check refuses operators other than a leading minus sign, and function calls.
Examples
Example 1
Worked Example: Sign-Magnitude- Input
the value of the sign-magnitude byte 1000_0101 (sign 1, size 5)
- Output
sm_85 = -5
The top bit 1 is the sign. The other seven bits, 000_0101, are the size 5. The value is -5. This constant is already filled in.
Example 2
Worked Example: ones' Complement- Input
the value of the ones'-complement byte 1111_1010 (invert it: 0000_0101)
- Output
oc_fa = -5
The top bit is 1, so the value is negative. Inverting every bit gives 0000_0101, so the value is -5. This constant is already filled in.
Example 3
Worked Example: The Range- Input
the most negative value of an 8-bit sign-magnitude number (1111_1111)
- Output
sm_min = -127
The largest size in seven bits is 127, so the most negative sign-magnitude byte is -127. This constant is already filled in.
Constraints and Contract
- Keep the module name answer and the names of the constants.
- Every value fits in a signed 16-bit number.
Hints
Hint 1
Sign-magnitude: the top bit is the sign and the other seven bits are the size, read as an unsigned number.
Hint 2
Ones' complement: a pattern with the top bit set is negative. Invert every bit to read its size.
Hint 3
To add in ones' complement, add as unsigned numbers. A carry out of the top bit is added back in at bit 0.
What the grader checks: The check reads eleven constants and compares each with a value computed in JavaScript. Before that, a source rule refuses any answer that is not one literal.
11 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