跳转至

Lecture 1: Number Representation

Big Idea: Everything is Bits

All data in your computer is stored as bits, sequences of 0s and 1s.

High voltage wire = 1. Low voltage wire = 0.

Numbers in Different Bases

  • Binary System (Base 2)
  • Decimal System (Base 10)
  • Octal System (Base 8)
  • Hexadecimal System (Base 16)
We use letters if we need more digits in Hex

The base-16 digits: 0 1 2 3 4 5 6 7 8 9 A(10) B(11) C(12) D(13) E(14) F(15)

Converting from Base-2/8/16 to Base-10

To convert bases(-2/8/16...) to base-10: Write out the powers of that base (e.g. powers of 16).

Converting from Base-10 to Base-2/8/16

Use the "leftover algorithm".

Leftover Algorithm:

  • If you use a box, you have to fill it up.
  • Use as few boxes as possible.

alt text

alt text

alt text

alt text

alt text

alt text

alt text

alt text

Numbers in Different Bases

Commonly Used Bases

  • Base 10 (decimal):
    • Digits: 0 1 2 3 4 5 6 7 8 9 ○ Understandable by humans.
  • Base 2 (binary):
    • Digits: 0 1 ○ Converting numbers to base 2 lets us represent numbers as bits!
  • Base 16 (hexadecimal):
    • Digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F ○ A convenient shorthand for writing long sequences of bits.

Commonly Used Bases – Notation

Writing numbers is ambiguous when we have different bases.

1011 Is this base 2? Base 10? Base 16? Base 7?

alt text

Converting Between Commonly-Used Bases

alt text

alt text

How to padding with zeros?

alt text

Groups of bits have special names

  • 1 byte = 8 bits. 2 hex digits.
  • 1 nibble = 4 bits. 1 hex digit.

Representing Integers (Including Negatives)

Unsigned Integers: Number Line View

Overflow occurs when we exceed the largest value and wrap back around to 0.

  • Example in 8-bit binary: 11111111 + 00000001 = 00000000.

Negative overflow occurs when we try to go below 0 and wrap around to large values.

  • Example in 8-bit binary: 00000001 – 00000010 = 11111111.
  • Don't call it underflow. That's a different thing.
underflow

在计算机科学中,underflow 指的是在执行浮点数计算时,结果的绝对值小于计算机能够表示的最小正数,从而被近似为零的情况。这与整数的negative overflow不同。

Underflow 发生在浮点数计算中,具体来说,当浮点数计算的结果绝对值非常小,以至于计算机无法表示(因为它小于浮点数格式能表示的最小正数),结果就会被舍入为零。这是一种精度丢失现象。

简单来说,negative overflow 是在整数运算中出现的现象,而 underflow 则是浮点数运算中特有的情况。

Different Representations

Sign-Magnitude (Not Commonly Used)

idea

Use the left-most bit to indicate if the number is positive (0) or negative (1).

sign-magnitude representation

alt text

alt text

0b1000...000 and 0b0000...000 both represent zero!

One's Complement (Not Commonly Used)

idea

If the number is negative, flip(翻转) the bits.

alt text

alt text

  • There's an unavoidable overflow, just like in unsigned.
  • Most positive number + 1 = Most negative number.
  • 0b1111...111 and 0b0000...000 both represent zero!

Two's Complement (What we use today)

alt text

idea

If the number is negative, flip the bits, and add one (Because we shifted to avoid double-zero).

alt text

alt text

Two's Complement: Conversion Algorithm

From Binary to Decimal

To convert two's complement to a signed decimal number (base 10):

  • If left-most digit is 0: Positive number.
    • Just read it as unsigned.
  • If left-most digit is 1: Negative number.
    • Flip the bits, and add 1.
    • Convert to base-10, and stick a negative sign in front.

alt text

From Decimal to Binary

To convert a signed decimal number to two's complement:

  • If number is positive:
    • Just convert it to base-2.
  • If number is negative:
    • Pretend it's unsigned, and convert to base-2.
    • Flip the bits, and add 1.

Two's Complement: Deep Connection to Modular Arithmetic

alt text

Bias Notation

idea

Just like unsigned, but shifted on the number line

alt text

\(Standard Bias\) of –(\(2^{N–1}\)– 1)

Convert biased notation to decimal

  1. Read as unsigned decimal.
  2. Add the bias.

alt text

Convert decimal to biased notation

  1. Subtract the bias.
  2. Convert to unsigned binary.

alt text