Section 2 · 9 min read

Number representation and arithmetic

Base conversion, unsigned and signed integers (sign-magnitude, 1's and 2's complement), overflow, Booth multiplication, BCD, fixed point and IEEE 754 floating point, with GATE-style conversions worked step by step.

On this page
  1. Why this topic scores
  2. Positional number systems
  3. Unsigned integers
  4. Signed integers
  5. BCD and other codes
  6. Fixed-point numbers
  7. IEEE 754 floating point
  8. Worked examples
  9. Common traps
  10. Quick revision

Why this topic scores

Questions here are mechanical, so they are free marks if you are careful: decode a 2's complement or IEEE 754 pattern, encode a decimal into IEEE 754 hex, decide whether an addition overflows, convert between bases, or reason about the range and precision of a made-up floating-point format. Most errors come from off-by-one mistakes in the exponent or from forgetting the hidden bit.

Positional number systems

In base rr, the digits dn−1…d1d0.d−1d−2…d_{n-1}\ldots d_1d_0.d_{-1}d_{-2}\ldots mean ∑diri\sum d_i r^i. Digits must be less than rr, which is often the key constraint in "find the base" questions.

Conversions

  • Base rr to decimal: expand the sum.
  • Decimal integer to base rr: divide by rr repeatedly, collect remainders from last to first.
  • Decimal fraction to base rr: multiply the fraction by rr repeatedly, collect the integer parts from first to last.
  • Binary, octal, hexadecimal: group bits from the binary point outward, in threes for octal and fours for hex.

A decimal fraction has a finite binary expansion only if its denominator (in lowest terms) is a power of 2. So 0.750.75 and 0.68750.6875 are exact, but 0.10.1 is 0.000110011…20.000110011\ldots_2 forever.

Unsigned integers

nn bits represent 00 to 2n−12^n - 1. Unsigned addition overflows when there is a carry out of the MSB. Unsigned subtraction A−BA - B done as A+B‾+1A + \overline{B} + 1 produces carry out 1 when A≥BA \ge B and 0 when a borrow occurs.

Signed integers

Representation Range for nn bits Zeros Negate xx
Sign-magnitude −(2n−1−1)-(2^{n-1}-1) to 2n−1−12^{n-1}-1 two flip the sign bit
1's complement −(2n−1−1)-(2^{n-1}-1) to 2n−1−12^{n-1}-1 two invert all bits
2's complement −2n−1-2^{n-1} to 2n−1−12^{n-1}-1 one invert all bits, add 1
Excess-KK (biased) −K-K to 2n−1−K2^n - 1 - K one stored value is x+Kx + K

2's complement in detail

The MSB has weight −2n−1-2^{n-1} and every other bit has its usual positive weight:

x=−bn−12n−1+∑i=0n−2bi2ix = -b_{n-1}2^{n-1} + \sum_{i=0}^{n-2} b_i 2^i

That formula is the fastest way to decode. Example: 11011011=−128+64+16+8+2+1=−3711011011 = -128 + 64 + 16 + 8 + 2 + 1 = -37.

  • Sign extension: to widen a number, copy the sign bit into the new high positions. −37-37 in 16 bits is FFDB.
  • Negating the most negative number: −2n−1-2^{n-1} has no positive counterpart, and negating it returns the same pattern.
  • Shortcut for negation: copy bits from the right up to and including the first 1, then invert the rest.

Overflow

Adding two nn-bit 2's complement numbers overflows exactly when both operands have the same sign and the result has the other sign. Equivalent hardware test: overflow =Cn⊕Cn−1= C_n \oplus C_{n-1} (carry out of the MSB XOR carry into the MSB). Adding numbers of opposite signs never overflows. A carry out of the MSB by itself means nothing for signed arithmetic.

In 1's complement addition, a carry out of the MSB is added back into the LSB (end-around carry).

Booth's multiplication (radix 2)

Booth's algorithm scans the multiplier from the right with an extra bit Q−1=0Q_{-1} = 0. For each pair (Qi,Qi−1)(Q_i, Q_{i-1}):

Pair Action
0 0 or 1 1 shift only
1 0 subtract multiplicand (start of a run of 1s)
0 1 add multiplicand (end of a run of 1s)

The number of additions and subtractions equals the number of bit changes in the multiplier with the appended 0. Booth is efficient for multipliers with long runs of 1s and worst for alternating patterns such as 0101...01 with Q−1=0Q_{-1} = 0, where every pair needs an operation.

BCD and other codes

BCD (8421) stores each decimal digit in 4 bits; codes 1010 to 1111 are unused. BCD addition: add the digits in binary; if the 4-bit result exceeds 9 or produces a carry, add 0110 and pass a carry to the next digit.

Excess-3 is BCD plus 3. It is self-complementing: inverting all bits gives the 9's complement of the digit.

Gray code: consecutive values differ in one bit. Binary to Gray is G=B⊕(B≫1)G = B \oplus (B \gg 1).

Fixed-point numbers

A fixed-point format puts the binary point at a fixed position. With mm integer bits and ff fraction bits in 2's complement, the stored integer is divided by 2f2^f. Range is −2m−1-2^{m-1} to 2m−1−2−f2^{m-1} - 2^{-f}, and every step is exactly 2−f2^{-f}. Example: in 4.4 format, 1101.01101101.0110 is the integer −42-42, so the value is −42/16=−2.625-42/16 = -2.625.

IEEE 754 floating point

A floating-point number is (−1)s×1.f×2e−bias(-1)^s \times 1.f \times 2^{e - \text{bias}} for normalized values.

Format Sign Exponent Fraction Bias
Single (32-bit) 1 8 23 127
Double (64-bit) 1 11 52 1023

Special exponent fields

Exponent field Fraction Meaning
all 0s 0 ±0\pm 0
all 0s nonzero subnormal: (−1)s×0.f×21−bias(-1)^s \times 0.f \times 2^{1 - \text{bias}}
1 to max −1-1 any normalized: (−1)s×1.f×2e−bias(-1)^s \times 1.f \times 2^{e - \text{bias}}
all 1s 0 ±∞\pm\infty
all 1s nonzero NaN

Note the subnormal exponent is 1−bias1 - \text{bias} (that is −126-126 for single), not 0−bias0 - \text{bias}. This makes the subnormals continue smoothly below the smallest normal number.

Key single-precision values

  • Largest finite: (2−2−23)×2127≈3.4×1038(2 - 2^{-23}) \times 2^{127} \approx 3.4 \times 10^{38}.
  • Smallest positive normalized: 2−126≈1.18×10−382^{-126} \approx 1.18 \times 10^{-38}.
  • Smallest positive subnormal: 2−23×2−126=2−149≈1.4×10−452^{-23} \times 2^{-126} = 2^{-149} \approx 1.4 \times 10^{-45}.
  • Gap between 1 and the next representable number: 2−232^{-23}.
  • Integers are exact up to 2242^{24}; 224+12^{24} + 1 is the first that cannot be stored.

+0 and -0 have different bit patterns but compare equal. NaN compares unequal to everything, including itself.

Encoding a decimal number

  1. Write the magnitude in binary.
  2. Normalize to 1.xxx×2E1.xxx \times 2^E.
  3. Biased exponent =E+bias= E + \text{bias}, in binary.
  4. Fraction = the bits after the leading 1, padded with zeros (or rounded to nearest even if longer).
  5. Concatenate sign, exponent and fraction; group into hex.

Custom formats

GATE often defines a small format, for example 1 sign bit, kk exponent bits with bias 2k−1−12^{k-1} - 1, and pp fraction bits. Use the same rules: the largest exponent field is reserved, the largest finite value is (2−2−p)×2(2k−2)−bias(2 - 2^{-p}) \times 2^{(2^k - 2) - \text{bias}}, and the smallest normal is 21−bias2^{1 - \text{bias}}. Read the question carefully: some custom formats do not reserve the all-ones exponent, and some have no hidden bit.

Worked examples

Worked example 1: Base conversion

Convert 243.25243.25 to hexadecimal and 11010.101211010.101_2 to decimal.

Solution. 243=15×16+3243 = 15 \times 16 + 3, so the integer part is F3. 0.25×16=4.00.25 \times 16 = 4.0, so the fraction is .4. Answer: F3.416\text{F3.4}_{16}.

11010.1012=16+8+2+0.5+0.125=26.62511010.101_2 = 16 + 8 + 2 + 0.5 + 0.125 = 26.625.

Worked example 2: Overflow check

In 8-bit 2's complement, compute 5A + 4B (hex) and decide whether it overflows.

Solution. 5A=905A = 90 and 4B=754B = 75, both positive. Binary sum: 01011010+01001011=1010010101011010 + 01001011 = 10100101 = A5. The result's MSB is 1, so it reads as −91-91. Two positives gave a negative: overflow. Carry into the MSB is 1 and carry out is 0, and 1⊕0=11 \oplus 0 = 1 agrees. The true sum 165 exceeds 127.

Worked example 3: Booth recoding

How many additions and subtractions does radix-2 Booth perform for the multiplier 0011110000111100?

Solution. Append 0: 00111100 000111100\,0. Scanning pairs from the right, the only changes are at bit 2 (pair (1,0)(1,0): subtract) and bit 6 (pair (0,1)(0,1): add). So 2 operations. The recoding is +26−22=64−4=60+2^6 - 2^2 = 64 - 4 = 60, which matches 00111100=6000111100 = 60. A plain shift-and-add multiplier would do 4 additions (one per 1 bit).

Worked example 4: Encode 12.375 in single precision

Solution. 12=1100212 = 1100_2 and 0.375=0.01120.375 = 0.011_2, so 12.375=1100.0112=1.1000112×2312.375 = 1100.011_2 = 1.100011_2 \times 2^3.

  • Sign: 0.
  • Exponent: 3+127=130=1000001023 + 127 = 130 = 10000010_2.
  • Fraction: 1000110000000000000000010001100000000000000000.

Bits: 0 10000010 100011000000000000000000\,10000010\,10001100000000000000000. Grouped in fours: 0100 0001 0100 0110 0000 0000 0000 00000100\,0001\,0100\,0110\,0000\,0000\,0000\,0000 = 41460000 (hex).

Worked example 5: Decode BE200000

Solution. Binary: 1011 1110 0010 0000…1011\,1110\,0010\,0000\ldots

  • Sign: 1.
  • Exponent: 01111100=12401111100 = 124, actual 124−127=−3124 - 127 = -3.
  • Fraction: 0100…0100\ldots, significand 1.012=1.251.01_2 = 1.25.

Value: −1.25×2−3=−0.15625-1.25 \times 2^{-3} = -0.15625.

Worked example 6: A small format

A format has 1 sign bit, 5 exponent bits (bias 15) and 4 fraction bits, with IEEE rules. Find the largest finite value and the smallest positive subnormal.

Solution. Largest usable exponent field is 11110=3011110 = 30, so the exponent is 30−15=1530 - 15 = 15. Largest significand 1.11112=2−2−4=1.93751.1111_2 = 2 - 2^{-4} = 1.9375. Largest value =1.9375×215=63488= 1.9375 \times 2^{15} = 63488.

Smallest subnormal: fraction 00010001, value 2−4×21−15=2−182^{-4} \times 2^{1-15} = 2^{-18}.

Common traps

  • Using the bias wrongly: actual exponent == field −- bias, not field ++ bias.
  • Forgetting the hidden 1 for normalized numbers, or adding it for subnormals.
  • Using −127-127 instead of −126-126 as the subnormal exponent in single precision.
  • Grouping hex digits from the left when the bit count is not a multiple of 4. Pad on the left of the integer part (and the right of the fraction part).
  • Treating carry out as signed overflow. Use the sign rule or Cn⊕Cn−1C_n \oplus C_{n-1}.
  • Writing the 2's complement range as symmetric. It is −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1.
  • In BCD addition, forgetting to correct a digit that produced a carry even though its 4-bit result looks valid (for example 9+9=1 00109 + 9 = 1\,0010 needs the +6+6 correction).
  • Assuming 0.1 is stored exactly in binary floating point.

Quick revision

  • 2's complement value: −bn−12n−1+∑bi2i-b_{n-1}2^{n-1} + \sum b_i2^i; range [−2n−1,2n−1−1][-2^{n-1}, 2^{n-1}-1].
  • 1's complement and sign-magnitude: range ±(2n−1−1)\pm(2^{n-1}-1), two zeros.
  • Signed overflow: same-sign operands, different-sign result; =Cn⊕Cn−1= C_n \oplus C_{n-1}.
  • Sign extension: copy the sign bit.
  • Booth: one operation per bit change in multiplier with appended 0.
  • BCD add: if digit sum > 9 or carries, add 0110.
  • IEEE single: 1/8/23, bias 127. Double: 1/11/52, bias 1023.
  • Normal: 1.f×2e−1271.f \times 2^{e-127}. Subnormal: 0.f×2−1260.f \times 2^{-126}.
  • Exponent all 1s: infinity (fraction 0) or NaN.
  • Single: max ≈3.4×1038\approx 3.4 \times 10^{38}, min normal 2−1262^{-126}, min subnormal 2−1492^{-149}.

Further reading

Test this section