Number System

Binary Number System

这一节的内容可以用这个例子来总结
To express (1001.1101)2 in decimal number,

1 0 0 1 . 1 1 0 1
1×23=8 0×22=0 0×21=0 1×20=1 1×2−1=0.5 1×2−2=0.25 0×2−3=0 1×2−4=0.0625

Sum them up to get the decimal number of (1001.1101)2=8+1+0.5+0.25+0.0625=9.8125

Octal and Hexadecimal Numbers

换算为十进制跟上面二进制换算一样,在此不述

Conversion between Binary, Octal and Hexadecimal Numbers

规则为

Bytes and Nibbles

Unit No. of Bits Represent values per unit Range
Byte 8 28=256 [0,255]
Nibble 4 24=16 [0,15]
Number Digit No. of bit No. of byte No. of nibble
Binary Digit 1 18 14
Octal Digit 3 38 34
Hex 4 12 1

Signed Numbers

Sign / Magnitude Numbers

Two's Complement Numbers

  1. Convert to one's complement: 0 和 1 互换
  2. Add 1

Example: Calculate two's complement of −3

  1. Binary number of 3: 0011
  2. One's complement: 1100
  3. Add 1: 1101
    Hence, the two's complement of −3 is 1101
Tip

只有当 sign bit 一致的情况下才有可能会出现 overflow error

Example

Divide 01100100 by 00011001

  1. Convert 00011001 to 2's complement (negation 变成负数)
  2. 00011001→11100111
  3. 不断做 subtraction
  4. 01100100+11100111=01001011
  5. 01001011+11100111=00110010
  6. 00110010+11100111=00011001
  7. 00011001+11100111=00000000
  8. Quotient = 4=(0100)2

Fixed-Point Number

定点数 fixed-point number 就是固定小数点的位置

Unsigned Fixed Point Formats

Ua.b

Example

Convert 6.75 into binary

6=(110)20.75=(0.11)2

Represent 6.75 in different format:

  • U4.4=0110⏟61100⏟0.75
  • U3.5=110⏟611000⏟0.75
  • U6.2=000110⏟611⏟0.75

Signed Fixed Point Format

Qa.b: signed 2's complement number

Example

Write -6.75 in Q4.4

  1. 6.75=(01101100)2
  2. 1's complement: 10010011
  3. Add 1 LSB: 10010100

Saturating Arithmetic

定点溢出 Fixed point overflow 通常很糟糕,会产生难看的伪影(artifacts):

Example

In U4.4, 11000000+01111000=100111000
Overflow error occurs
Hence, use the largest value of U4.4 under saturating arithmetic, i.e. 11000000+01111000=11111111

Floating Point Numbers

类似科学计数法 (General form: ±M×BE)

Biased Exponent

Range of exponent (without biased): [0,28−1]=[0,255]
只能表示正数和 0,表示不了负数
但是科学计数法里的指数是有正有负,因此把 exponent 全部加 127,变成 positive number 储存

存进格子的值=真实指数+偏移量(bias)取出时:真实指数=格子里的值−偏移量
Example

Represent 228 using 32 bit floating point representation

  1. Convert to binary number: 228=(11100100)2
  2. Normalize (把小数点移到第二位): 11100100=1.1100100×27
  • 移动几位,指数就是几。这里移了 7 位 → 指数 E=7
  1. 计算 sign bit, exponent and mantissa
  2. Sign bit: 0
  3. Exponent: 7+127=134=(10000110)2
  4. Mantissa / Fraction: 由于第一位(即小数点前的那位)永远为 1,所以可以省略来减省空间
    1. 保存为 1100100
    2. 由于 fraction bit 是 23-bit 长,因此要填充 0,因此为 11001000000000000000000

因此,228=01000011011001000000000000000000

Example

Write -58.25 in floating point

  1. Convert to binary number: 58.25=(111010.01)2
  2. Normalize: 1.1101001×25
  3. Fill the fields
  4. Sign bit: 1
  5. Exponent bit: 5+127=132=(10000100)2
  6. Fraction bit: 110 1001 0000 0000 0000 0000

Special Cases

Number Sign Exponent Fraction
0 X 00000000 全 0
+∞ 0 11111111 全 0
−∞ 1 11111111 全 0
NaN(Not a Number) X 11111111 非 0

Non-positional / Non-weighted Number System

并非所有二进制串都是用来做算术的"数",有些只是编码(code)

Binary Coded Decimal (BCD)

把每一个十进制数字单独用一个 4 位二进制编码表示
例如 8421 code
BCD(8421 code).png

BCD Addition

Add two BCD numbers (跟二进制加法一致)

Note

4 bit 二进制的 range 是 [0,15],但 BCD 的 range 是 [0,9](到 1001 = 9 就该进位了)
加 6 正好把这段落差补回来,让进位时序重新和十进制对齐

Gray Code

Only a single bit change from one code word to the next in sequence
相邻的 Gray Code 只有 1 bit 的变化
Gray Code.png
Denote each digit of Gray Code by Gi and digit of binary number by Bi

Example

Convert the binary number 1100 0110 to Gray Code

  1. G1=B1=1
  2. Carry out XOR operation
G2=B2⊕B1=1⊕1=0G3=B3⊕B2=0⊕1=1G4=B4⊕B3=0⊕0=0G5=B5⊕B4=0⊕0=0G6=B6⊕B5=1⊕0=1G7=B7⊕B6=1⊕1=0G8=B8⊕B7=0⊕1=1

Hence, the Gray Code is 10100101.

Example

Convert Gray Code 10101111 to binary number.

B1=G1=1B2=B1⊕G2=1⊕0=1B3=B2⊕G3=1⊕1=0B4=B3⊕G4=0⊕0=0B5=B4⊕G5=0⊕1=1B6=B5⊕G6=1⊕1=0B7=B6⊕G7=0⊕1=1B8=B7⊕G8=1⊕1=0

Hence, the binary number is 11001010.

Error Codes

数据在传输或存储中可能出错(由 0 变 1 或反之)

Parity Bit for Error Detection

Cyclic Redundancy Check (CRC)

CRC 是能检测数据块中多个错误的检错方法:

Example

Given

  • Data: 10110010
  • Generator code: 1010
    Denote Data by D and Generator code by G

Send

  1. 在 data 后添加 (n−1)=3 个 0 (n 是 Generator code 的长度)
    D′=1011 0010 000
  2. Modulo-2 Division: D′G=110
  3. Append the remainder / checksum to D
    The transmitted CRC is 10110010110

Receive
Divide received data by generator code

  • Correct: Remainder = 0
  • Error: Remainder is non-zero
    CRC.png