G1.2.3 Read the Old Number Formats

EasyImplementIcarus · browser samples / server checksReview the topic

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

SignalWidthRole
question8input of each check
Full specification
ConstantWhat to write
SM_85The value of the sign-magnitude byte 1000_0101 (sign 1, size 5).
OC_FAThe value of the ones'-complement byte 1111_1010 (invert it: 0000_0101).
SM_MINThe most negative value of an 8-bit sign-magnitude number (1111_1111).
SM_D1The value of the sign-magnitude byte 1101_0001.
SM_7FThe value of the sign-magnitude byte 0111_1111.
OC_80The value of the ones'-complement byte 1000_0000.
OC_E3The value of the ones'-complement byte 1110_0011.
TC_OF_SM_83The two's-complement byte with the same value as the sign-magnitude byte 1000_0011.
SM_OF_TC_F6The sign-magnitude byte with the same value as the two's-complement byte 1111_0110.
OC_SUMThe 4-bit ones'-complement sum of 0110 and 1100, end-around carry included.
ZEROSHow many 8-bit sign-magnitude patterns mean zero.
How answers are written
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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Worked Example: Sign-Magnitude

Input
the value of the sign-magnitude byte 1000_0101 (sign 1, size 5)
Expected 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.

Run samples executes all 3 public checks. Check all tests runs 11 checks on the server.

Icarus · browser samples / server checks