CHSE Odisha Elective Computer Science study materials
CHSE Odisha Elective Computer Science study resources

Signed Temperature Readings on an 8-bit Microcontroller

CHSE Odisha Elective Computer Science · Put your knowledge to work in context.

All case studies

Signed Temperature Readings on an 8-bit Microcontroller

At a district science exhibition, a team stores temperature readings from a sensor in an 8-bit signed integer inside a microcontroller. The sensor’s specified range is −40 °C to +125 °C, and only the integer part is stored. The team displays readings in hexadecimal. At +100 °C the display shows 0x64, and at −40 °C it shows 0xD8. A visiting student claims the negative reading is wrong because “D8 is 216, not −40”. The team also plans to add a second sensor that can reach +130 °C and wants to know whether the same 8-bit signed variable is safe. They have an option to use a 16-bit signed variable instead. The teacher asks you to verify the representation, check the range, and recommend a storage type with justification.

1. Determine the range of an 8-bit signed two’s-complement integer, and verify both hex readings by showing the conversions.

Read the worked answer

An 8-bit two’s-complement signed integer has range −2^7 to 2^7−1, i.e., −128 to +127. So both −40 and +125 are in range and the readings are valid. For +100: 100 decimal = 0x64, because 6×16 + 4 = 100. For −40, take the 8-bit magnitude 40 = 0x28 = 0010 1000; invert = 1101 0111; add 1 = 1101 1000 = 0xD8. As unsigned, 0xD8 = 216, but in signed interpretation the leading bit is 1, so value = 216 − 256 = −40. Thus the student’s “216” is the unsigned reading; the signed reading is −40. Similarly, +125 = 0x7D, and +100 = 0x64.

2. What happens if the team stores +130 in the same 8-bit signed variable? Recommend a representation for the extended range and justify it.

Read the worked answer

+130 cannot be represented in 8-bit signed. The maximum is +127 (0x7F). Adding 1 to 0x7F gives 0x80, which is interpreted as −128: sign overflow or wraparound. The displayed value would jump from +127 to −128, a dangerous error. A 16-bit signed integer has range −32,768 to +32,767, so +130 and the full −40 to +130 range fit safely; use 16-bit signed. If memory were tight, an offset-binary 8-bit scheme could store temperature + 40, giving 0–255 for −40 to +215; that covers +130. But then every comparison and display must subtract the offset, which is easier to get wrong. For a science project, 16-bit signed is simpler and less error-prone. Also configure the ADC for the new range.

3. The team compares raw hex values, so 0xD8 appears greater than 0x64. Explain why this comparison is misleading for temperatures and how a signed comparison should be done in C.

Read the worked answer

The hex comparison 0xD8 > 0x64 is true only as unsigned, because 216 > 100. But the physical temperatures are −40 and +100, and signed order is −40 < +100. Comparing raw bit patterns as unsigned reverses the order of all negative numbers. The program must either declare the variable as int8_t or signed char and compare as signed, or convert the stored byte to signed before comparing. In C, signed char t = (signed char)0xD8; yields −40 on a two’s-complement machine. If the byte is held in an unsigned char, cast it to signed before using relational operators. Do not compare the hex strings directly.