TECHNOLOGY 

Published on
<

KembaraXtra – Computer Science – Unsigned Numbers

1. The Need for Unsigned Numbers

Signed vs. Unsigned:

  • Signed integers (using two's complement) represent both positive and negative numbers.
  • Unsigned integers represent only non-negative numbers (positive and zero).

Why Use Unsigned?

  • When negative values are unnecessary, using signed integers wastes half the representable range.
  • Unsigned integers allow representing larger positive values with the same number of bits.

2. Understanding Unsigned Representation

Interpretation Matters: A binary sequence can represent different values depending on whether it's interpreted as signed or unsigned.

Example (4-bit):

Binary Signed Decimal Unsigned Decimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15

Generalization (n-bit unsigned number):

  • Maximum value: (2n) − 1
  • Minimum value: 0
  • Count of unique values: 2n

3. Operations and Interpretation

Adder Circuit's Perspective: The adder circuit performs the same binary addition regardless of whether the numbers are signed or unsigned.

Interpretation is Key: After calculation, the program decides how to interpret the result (signed or unsigned).

Example: Adding 1011 and 0010 results in 1101.

  • Signed: −5 + 2 = −3
  • Unsigned: 11 + 2 = 13

4. Integer Overflow

Carry-out Bit (Unsigned): A carry-out of 1 indicates an integer overflow. The result is too large to be represented with the given number of bits.

Carry-in/Carry-out (Signed):

  • Overflow: Most significant carry-in bit not equal to most significant carry-out bit.
  • No Overflow: Most significant carry-in bit equal to most significant carry-out bit (carry-out can be ignored).

Importance of Overflow Detection: Overflows can lead to incorrect results and unexpected program behavior if not handled properly.

Real-world Example: Pac-Man level 256 glitch is caused by an integer overflow in the level counter.

Picture
0 Comments