Sub topic 16
GATE-style problem patterns and shortcuts
This chapter assumes you can already encode and decode 2's complement integers, fixed-point values and IEEE 754 fields by hand. Here we sort the questions GATE asks into a few recurring patterns, each with a fast method and a worked example.
Step 1: Classify the question
Most numeric-representation questions fall into one of five types. Name the type first, then apply its shortcut.
Pattern 1: Integer ranges and overflow
Memorize the table for n bits.
| Encoding | Smallest | Largest | Distinct values |
|---|---|---|---|
| Unsigned | 0 | 2^n − 1 | 2^n |
| Sign-magnitude | −(2^(n−1) − 1) | 2^(n−1) − 1 | 2^n − 1 (two zeros) |
| 1's complement | −(2^(n−1) − 1) | 2^(n−1) − 1 | 2^n − 1 (two zeros) |
| 2's complement | −2^(n−1) | 2^(n−1) − 1 | 2^n |
Overflow shortcuts for 2's complement addition:
- Adding two numbers of the same sign gives a result of the opposite sign: overflow.
- Adding numbers of different signs never overflows.
- In hardware terms, overflow = (carry into the MSB) XOR (carry out of the MSB).
- A carry out of the MSB alone does not mean overflow. It is the unsigned overflow flag.
Example: add 100 and 50 in 8 bits. The pattern 01100100 plus 00110010 gives 10010110. Both operands are positive and the result has sign bit 1, so this is overflow. The pattern reads as 150 − 256 = −106.
Reading a negative pattern quickly: if the MSB is 1, the value is (unsigned value) − 2^n. For 8 bits, 11111011 is 251 − 256 = −5.
Pattern 2: Fixed-point questions
A number with f fraction bits is the stored integer divided by 2^f. Resolution is 2^−f. For a 2's complement word of n bits, the range is −2^(n−f−1) to 2^(n−f−1) − 2^−f.
Example: a 16-bit 2's complement fixed-point format has 6 fraction bits. The resolution is 1/64. The smallest value is −2^9 = −512. The largest is 2^9 − 1/64 = 511.984375. Moving the binary point one place right doubles the range and halves the precision.
Pattern 3: Decode or encode an IEEE 754 word
Single precision is 1 + 8 + 23 bits with bias 127. Double precision is 1 + 11 + 52 bits with bias 1023.
Decoding 0xC1480000. Write it in binary as 1100 0001 0100 1000 followed by zeros.
- Sign is 1, so the number is negative.
- The next 8 bits are 10000010 = 130, so the true exponent is 130 − 127 = 3.
- The fraction bits are 1001, so the mantissa is 1.1001 in binary = 1.5625.
- The value is −1.5625 × 2^3 = −12.5.
Encoding 20.75. In binary, 20.75 = 10100.11 = 1.010011 × 2^4.
- The biased exponent is 4 + 127 = 131 = 10000011.
- Pad the fraction 010011 with zeros to 23 bits.
- The word is 0 10000011 0100110…0.
- Regroup into nibbles: 0100 0001 1010 0110 0000 … = 0x41A60000.
Shortcut: a leading hex digit of 3 or 4 with a 0 sign means the exponent is near 0 to 2^k. Compare quickly with 0x3F800000, which is 1.0. Anything from 0x3F800000 up to 0x40000000 lies in [1, 2).
Pattern 4: Custom floating-point formats
GATE often gives a format such as "1 sign bit, e exponent bits, m mantissa bits" and asks for a maximum, a minimum or a gap. Follow the same steps each time.
- Bias = 2^(e−1) − 1, unless the question states another bias.
- Check whether the question says IEEE-like conventions apply. If so, the all-ones exponent is reserved for infinity and NaN, and the all-zeros exponent is reserved for zero and denormals.
- Largest finite value = (2 − 2^−m) × 2^(2^e − 2 − bias).
- Smallest positive normalized value = 2^(1 − bias).
- Smallest positive denormal = 2^−m × 2^(1 − bias).
- The gap between neighbouring values with true exponent x is 2^(x − m). Gaps double each time the exponent rises by one.
For a 16-bit format with e = 5 and m = 10, the bias is 15. The largest finite value is (2 − 2^−10) × 2^15 = 65504. The smallest normal is 2^−14, and the smallest denormal is 2^−24.
You can check hand results with a small decoder.
import math
def decode(word, e_bits, m_bits):
bias = (1 << (e_bits - 1)) - 1
sign = -1.0 if word >> (e_bits + m_bits) else 1.0
exp = (word >> m_bits) & ((1 << e_bits) - 1)
frac = word & ((1 << m_bits) - 1)
if exp == (1 << e_bits) - 1:
return sign * math.inf if frac == 0 else math.nan
if exp == 0:
return sign * (frac / 2**m_bits) * 2.0 ** (1 - bias)
return sign * (1 + frac / 2**m_bits) * 2.0 ** (exp - bias)
print(decode(0x41A60000, 8, 23)) # 20.75
print(decode(0xC1480000, 8, 23)) # -12.5
print(decode(0x7BFF, 5, 10)) # 65504.0
print(decode(0x0001, 5, 10)) # 5.960464477539063e-08
# 8-bit format: 1 sign, 3 exponent, 4 mantissa bits
vals = [decode(w, 3, 4) for w in range(256)]
finite = [v for v in vals if math.isfinite(v)]
print(max(finite)) # 15.5
print(min(v for v in finite if v > 0)) # 0.015625Pattern 5: Counting representable values
For an IEEE-like format with sign 1 bit, e exponent bits and m mantissa bits, count by exponent class.
| Class | Count |
|---|---|
| Zeros | 2 |
| Denormals | 2 × (2^m − 1) |
| Normalized | 2 × (2^e − 2) × 2^m |
| Infinities | 2 |
| NaNs | 2 × (2^m − 1) |
For the 8-bit format with e = 3 and m = 4, the counts are 2, 30, 192, 2 and 30. They sum to 256, which confirms the classification. Always check that your counts add up to 2^(total bits).
The number of values in a range such as [1, 2) is simply 2^m, because every mantissa pattern with exponent 0 lands there. Questions that ask how many numbers lie between 2^k and 2^(k+1) use the same idea.
Pattern 6: Precision and equality traps
- Integers are exact in single precision only up to 2^24 = 16,777,216. Above that, consecutive integers cannot all be represented, since the gap becomes 2.
- Adding a small number to a very large one can leave the large one unchanged. Therefore (a + b) + c can differ from a + (b + c).
- Decimal fractions such as 0.1 have no finite binary expansion, so they are stored approximately.
- Machine epsilon is 2^−m, the gap between 1.0 and the next larger value: 2^−23 for single and 2^−52 for double.
- Comparing the bit patterns of two positive IEEE floats as unsigned integers gives the same order as comparing their values. This is why the exponent comes before the mantissa.
Time-saving checklist
- Write the format as S | E | F with the bit counts before doing any arithmetic.
- Convert hex to binary only for the fields you need.
- For negative 2's complement, use "unsigned − 2^n" instead of flipping bits when the pattern is given.
- Express everything as powers of two and leave decimal conversion to the end.
- Verify counts and ranges against 2^n total patterns.
Exercise
In 8-bit 2's complement, compute (−100) + (−50). What are the stored result bits, and is there overflow?
Show answer
−100 = 10011100 and −50 = 11001110. Their sum is 1 01101010, and the 8-bit result is 01101010 = +106. Both operands are negative and the result is positive, so overflow occurred. The true sum is −150, which is below −128.
Exercise
Encode 0.15625 in IEEE 754 single precision and give the hex word.
Show answer
0.15625 = 0.00101 in binary = 1.01 × 2^−3. The biased exponent is 124 = 01111100, and the fraction is 01 followed by zeros. The word is 0 01111100 0100…0, which is 0x3E200000.
Exercise
A custom format has 1 sign bit, 4 exponent bits and 5 mantissa bits, and follows IEEE conventions. What are the bias and the largest finite value?
Show answer
The bias is 2^3 − 1 = 7. The largest finite biased exponent is 2^4 − 2 = 14, so the true exponent is 7. The largest mantissa is 2 − 2^−5 = 1.96875. The largest finite value is 1.96875 × 2^7 = 252.