BSEH SS Computer Science study materials
BSEH SS Computer Science study resources

Signed Sensor Data and a Wrong Temperature

BSEH SS Computer Science · Put your knowledge to work in context.

All case studies

Signed Sensor Data and a Wrong Temperature

A school weather station uses a 12-bit ADC connected to a sensor rated from −40 °C to +60 °C. Firmware stores temperature in tenths of a degree Celsius as a 12-bit two’s complement integer. Two readings are packed into 3 bytes for transmission, and a display routine prints each 12-bit value as three hexadecimal digits. On a winter morning the station measures −5.0 °C, but the display shows 7CE and the logger records 199.8 °C. A nearby sensor reads −5.2 °C. The programmer suspects the sign bit is being cleared before conversion. Separately, the daily maximum is stored in an 8-bit signed field, and on a hot day the true maximum is 60.0 °C but the stored value becomes 8.8 °C. The station also sends packets containing two samples, and the receiver currently reads only the first sample correctly. The project must log negative temperatures and display values without changing the sensor.

1. Compute the correct 12-bit representation of −5.0 °C and explain the 7CE / 199.8 symptom. Give a code fix.

Read the worked answer

−5.0 °C = −50 tenths. For 12-bit two’s complement, 2^12 = 4096, so −50 is 4096 − 50 = 4046 = 0xFCE = binary 1111 1100 1110. Bit 11 (the sign bit) is 1.

If the display routine masks with 0x7FF, it clears the sign bit: 0xFCE & 0x7FF = 0x7CE = 1998. Interpreting 1998 as tenths gives 199.8 °C. That exactly explains the log.

Fix: sign-extend the 12-bit value instead of masking:

v = raw & 0xFFF
if v & 0x800:
    v -= 0x1000
temp = v / 10.0

Test with −50 (0xFCE) and with +25.0 (250 = 0x0FA). The latter must not be sign-extended because bit 11 is 0.

2. Show why the true daily maximum of 60.0 °C becomes 8.8 °C in an 8-bit signed field, and propose two storage alternatives.

Read the worked answer

60.0 °C = 600 tenths. An 8-bit signed field holds −128 to +127, so 600 cannot fit. Storing only the low 8 bits gives 600 mod 256 = 88, which is read as +88 tenths = 8.8 °C. The value is not just rounded; it has wrapped, and without a range check the error is silent.

Options: (a) use a 16-bit signed field, which safely covers −3276.8 to +3276.7 tenths; (b) if tenths are not required for the daily maximum, store whole degrees in an 8-bit signed field, since +60 fits; (c) add an explicit overflow flag/clamp at +127. Option (a) is safest because it preserves the resolution used elsewhere.

3. For samples 0xFCE and 0x0C8, show the packed 3 bytes and the extraction logic. State one byte-order risk.

Read the worked answer

Pack as: packed = (first << 12) | second = (0xFCE << 12) | 0x0C8 = 0xFCE0C8. The three bytes, big-endian, are FC E0 C8.

Extraction:

first  = (packed >> 12) & 0xFFF   # 0xFCE
second = packed & 0xFFF            # 0x0C8

Then sign-extend first if bit 11 is set, because 0xFCE is −50 tenths, not +4046. second is 0x0C8 = 200 = +20.0 °C, so no sign extension is needed. If the receiver uses little-endian bytes, it must reverse C8 E0 FC before applying the same shifts; otherwise the two temperatures are swapped or corrupted.