Representing Numbers: Signed, Unsigned, Fixed- and Floating-Point

From MediaWiki
Jump to navigation Jump to search

Representing Numbers: Signed, Unsigned, Fixed- and Floating-Point

This page introduces how numerical values are represented in computers. It explains unsigned and signed integers, overflow, fixed-point and floating-point numbers, and why different representations exist.

Positive and negative numbers

So far, we have considered only positive integers. Example: 15₁₀ = 0x0F (8-bit) or 0x0000000F (32-bit).

But how do we represent negative values such as −15? We need a rule to decide which bit patterns represent positive numbers and which represent negative numbers.

  • Numbers that can only be positive are called *unsigned*.
  • Numbers that can be both positive or negative are called *signed*.

Integer overflow

When counting with limited bits, the value wraps around once the maximum value is reached. For an 8-bit number:

11111111₂ + 00000001₂ = 00000000₂ (overflow)

This behavior is called integer overflow. It is cyclic, like an odometer rolling over from 999999 to 000000.

Representations of signed numbers

There are several methods to represent signed integers:

1. **Sign bit**

  The most significant bit (MSB) represents the sign.  
  * 0 → positive  
  * 1 → negative  
  Example:  
  +15 = 00001111₂  
  −15 = 10001111₂
  Problem: Arithmetic does not work correctly across signs.  
  Example: 15 + (−15) ≠ 0.  

2. **One’s complement**

  Negative numbers are formed by inverting all bits of the positive number.  
  Example:  
  +15 = 00001111₂  
  −15 = 11110000₂  
  Problems:  
  * Two representations of zero: 00000000 and 11111111.  
  * Addition with mixed signs gives wrong results without correction.

3. **Two’s complement**

  The modern standard for representing signed integers.  
  −X = 2ⁿ − X, where n is the bit width.  
  In practice:
  * Invert all bits.
  * Add 1.
  Example (8-bit):  
  15 = 00001111₂  
  −15 = invert → 11110000₂, add 1 → 11110001₂  
  Check arithmetic:  
  00001111₂ + 11111011₂ = 00001010₂ (15 + (−5) = 10).  
  Arithmetic works for both positive and negative numbers.

Negation method (two’s complement)

To find the negative of a number X: 1. Invert all bits. 2. Add 1.

Example: −20 20 = 00000000 00010100₂ Invert: 11111111 11101011₂ Add 1: 11111111 11101100₂ = 0xFFEC.

To return to positive, repeat the same process.

Value ranges

The value range depends on the number of bits (n):

          Type            |          Range
--------------------------|--------------------------
                 Unsigned | 0 to 2ⁿ − 1
Signed (two’s complement) | −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1

Examples:

  • 8-bit unsigned: 0 to 255
  • 8-bit signed: −128 to +127
  • 16-bit signed: −32 768 to +32 767

Interpretation of bit patterns

The same bit pattern can mean different values depending on whether it’s interpreted as signed or unsigned. The *data type* tells the CPU how to interpret the bits.

Example: 11111111₂

  • As unsigned: 255
  • As signed (two’s complement): −1

Unified arithmetic

Addition and subtraction work the same for signed and unsigned integers. The CPU performs the same binary operation; only interpretation differs. We simply move along the circular number line.

Why multiple representations exist

  • Unsigned integers are simpler and ideal for counts, indices, and addresses.
  • Signed integers support negative values for arithmetic operations.
  • Fixed-point and floating-point extend the idea to fractions and real numbers.