Chapter 9 Arithmetic(1)

这一章是很难的,不过我打的就是精锐。😎

中英对照表

章节 内容(英语) 内容(中文)
Integer Representation Representation of integers in binary, including unsigned and signed formats. 整数的表示方法,包括无符号数和有符号数的二进制表示。
Addition and Subtraction of Signed Numbers Operations for adding and subtracting signed integers in binary representation. 有符号整数的加法与减法运算,基于二进制表示方法。
Ripple Carry Adder A method of binary addition where carry bits ripple through from one bit to the next. 进位链加法器,一种按位进行二进制加法的方法,进位逐位传递。
Design of Fast Adders Techniques for creating high-speed adders, such as carry-lookahead adders. 快速加法器的设计技术,如进位预测加法器。
Multiplication of Unsigned Numbers Binary multiplication of unsigned integers. 无符号整数的二进制乘法。
Multiplication of Signed Numbers Binary multiplication of signed integers, often using two's complement. 有符号整数的二进制乘法,通常使用二进制补码表示。
Integer Division Division of integers using binary methods, including restoring and non-restoring division. 整数的二进制除法,包括恢复型和非恢复型除法方法。
Floating-Point Numbers Representation Representation of real numbers in binary, following IEEE 754 standard. 浮点数的二进制表示,遵循 IEEE 754 标准。
Floating-Point Numbers Operation Operations on floating-point numbers, including addition, subtraction, multiplication, and division. 浮点数的运算,包括加法、减法、乘法和除法操作。
章节 内容(英语) 内容(中文)
Signed-Magnitude Representation A binary representation of signed numbers where the most significant bit (MSB) indicates the sign. 符号-数值表示法:用二进制表示有符号数,最高有效位(MSB)表示符号。
Signed One’s Complement Representation A binary representation where the negative of a number is formed by inverting all bits. 符号一补码表示法:通过将所有位取反来表示负数的二进制方法。
Signed Two’s Complement Representation A binary representation where the negative of a number is formed by inverting all bits and adding 1. 符号二补码表示法:通过将所有位取反并加 1 来表示负数的二进制方法。

整数表示法 (Integers)

1. 无符号整数表示

  • n位整数
    • \(B = b_{n-1}…b_1b_0,其中b_i = 0 或 1,0 ≤ i ≤ n-1\)
    • 该二进制数表示一个无符号整数值$ 0 ~ 2^n - 1$。
    • 对于二进制数 B,其数值 V(B) 由下式表示: $ V(B) = b_{n-1} ^{n-1} + b_{n-2} ^{n-2} + + b_1 ^1 + b_0 ^0 $
    • 该表示方法用于表示非负整数。

2. 有符号整数表示

为了表示正数和负数,计算机使用不同的二进制表示方法。常见的有三种:符号-数值表示法、符号一补码表示法和符号二补码表示法。

Signed-Magnitude Representation (带符号幅度表示法)

1. Signed-Magnitude Representation (1)

  • 定义:对于一个n位整数:
    • 符号部分是1位,用0表示正数,1表示负数。
    • 幅度部分是n-1位,用来表示数值的绝对值。

2. Signed-Magnitude Representation (2)

  • 表示法:对于一个n位整数 $ B = b_{n-1} b_{n-2} b_1 b_0 $,
    • 如果 $ b_{n-1} = 0 $,则B为正数。
    • 如果 $ b_{n-1} = 1 $,则B为负数。
  • 例子
    • +18 (十进制) = 00010010 (二进制)
    • -18 (十进制) = 10010010 (二进制)
    • +0 = 00000000
    • -0 = 10000000

3. Signed-Magnitude Representation (3)

  • 表示范围
    • 如果一个n位二进制数 $ b_{n-1} b_{n-2} b_1 b_0 $ 被解读为带符号整数B,则它的值为: $ V(B) = \[\begin{cases} \sum_{i=0}^{n-1} b_i \cdot 2^i & \text{如果} \, b_{n-1} = 1 \\ -\sum_{i=0}^{n-1} b_i \cdot 2^i & \text{如果} \, b_{n-1} = 0 \end{cases}\] $
    • 范围: $ -(2^{n-1} - 1) V(B) ^{n-1} - 1 $

4. Signed-Magnitude Representation (4)

  • 缺点
    • 加法和减法操作需要考虑两个数的符号以及它们的幅度,操作较为复杂。
    • 存在两个0的表示:
      • +0 = 00000000
      • -0 = 10000000

Signed One’s Complement Representation (带符号一补码表示法)

1. Signed One’s Complement Representation (1)

  • 定义

    • 符号部分:1位,用0表示正数,1表示负数。
    • 幅度部分:n-1位,用来表示数值的绝对值。
  • 正数表示:对于正数,带符号一补码的表示与带符号幅度表示相同,符号位为0,幅度部分与该数的绝对值的二进制表示相同。

  • 负数表示:对于负数,符号位为1,幅度部分是该数绝对值的二进制表示的一补码,即将每一位的0变成1,1变成0。

2. Signed One’s Complement Representation (2)

  • 一补码的定义
    • 一补码是通过对数值的每一位进行位反转来获得的。也就是说,将每个1变成0,每个0变成1。
  • 例子
    • +18(十进制) = 00010010(二进制)
      • 该数是正数,符号位为0,幅度部分为18的二进制表示 00010010。
    • -18(十进制) = 11101101(二进制)
      • 该数是负数,符号位为1,幅度部分为18的绝对值 18 = 00010010,取其一补码(反转每一位),得到 11101101。
    • +0(十进制) = 00000000
      • 该数是正零,符号位为0,幅度部分为0。
    • -0(十进制) = 11111111
      • 该数是负零,符号位为1,幅度部分为0的一补码,得到 11111111。

Signed Two’s Complement Representation (带符号二补码表示法)

1. Signed Two’s Complement Representation (1)

  • 定义

    • 符号部分:1位,用0表示正数,1表示负数。
    • 幅度部分:n-1位,用来表示数值的绝对值。
  • 正数表示:对于正数,带符号二补码的表示与带符号幅度表示法相同,符号位为0,幅度部分为该数的二进制表示。

  • 负数表示:对于负数,符号位为1,幅度部分表示负数的二补码。负数的二补码是通过对该数的绝对值进行一补码(每一位取反)之后,再加1得到。

2. Signed Two’s Complement Representation (2)

  • 二补码的计算过程
    • 对于负数的表示,首先计算该数的一补码(反转每一位),然后再加1
  • 例子
    • +18(十进制) = 00010010(二进制)
      • 该数是正数,符号位为0,幅度部分为18的二进制表示 00010010。
    • -18(十进制) = 11101110(二进制)
      • 该数是负数,符号位为1,幅度部分为18的绝对值 18 = 00010010,先取其一补码 11101101,再加1得到 11101110。
    • +0(十进制) = 00000000
      • 该数是正零,符号位为0,幅度部分为0。
    • -0(十进制) = 00000000
      • 该数是负零,在二补码表示法中,负零和正零是相同的,都是 00000000。

3. Representation Range (表示范围)

  • 表示范围:对于一个n位的二进制数 $ B = b_{n-1} b_{n-2} b_1 b_0 $,其值 $ V(B) $ 的范围是:
    • 正数部分:$ 0 V(B) ^{n-1} - 1 $

    • 负数部分:$ -2^{n-1} V(B) $

    • 总范围:$ -2^{n-1} V(B) ^{n-1} - 1 $

    • 例如,对于一个8位的带符号二补码数(7位表示幅度):

      • 正数的范围是 $ 0 $ 到 $ 2^7 - 1 = 127 $
      • 负数的范围是 $ -2^7 = -128 $ 到 $ -1 $
      • 所以表示范围是 $ -128 V(B) $

4. Signed Two’s Complement Representation (3)

  • 计算方法
    • 对于正数,二补码就是普通的二进制表示,符号位为0,幅度部分直接表示该数的绝对值。
    • 对于负数,首先取该数绝对值的二进制表示,反转每一位(得到一补码),然后加1,得到二补码。
  • 示例
    • 假设我们有一个8位数n = 8:
      • +18:二进制为 00010010。
      • -18:先计算18的二进制表示:00010010,反转得到11101101,再加1得到11101110。

总结

image-20241122111951018

1. Common Characteristics of All Three Systems (三种表示法的共同特点)

  1. 符号位:在所有三种表示法中,符号位(最左边的位)为0表示正数,为1表示负数。
  2. 正数表示:正数在三种表示法中具有相同的表示方式。
  3. 负数表示:负数的表示方式在三种表示法中不同。
  4. 一补码与二补码关系:一个数的二补码表示是通过对其一补码进行加1得到的。
  5. 二补码表示范围:n位二补码整数的范围是 $ -2^{n-1} $ 到 $ 2^{n-1} - 1 $,这是因为0只有唯一的表示(没有负零)。

2. Signed-Magnitude System (带符号幅度表示法)

  • 优点:带符号幅度表示法在表示正数时非常直观,符号位单独表示正负,容易理解。
  • 缺点:对于加法和减法运算,需要单独考虑符号位和数值部分,计算过程相对复杂,因此在运算时效率较低。

3. Two’s-Complement System (带符号二补码表示法)

  • 优点:二补码表示法是执行加法和减法运算的最有效方式,因为它避免了两个零的表示,简化了运算过程。在二补码系统中,负数和正数的处理是统一的,不需要额外的符号判断。
  • 缺点:对于某些操作,二补码系统可能不直观,负零的表示方式与正零相同,可能导致一些理解上的混淆。

Converting between Different Bit Lengths (不同位长度之间的转换)

1. Signed-Magnitude Numbers (带符号幅度表示法)

  • 转换方法
    • 将符号位移到新的最左边的位置,并用零填充其余的位。
  • 例子
    • +18(8位) = 00010010
    • +18(16位) = 0000000000010010
    • -18(8位) = 10010010
    • -18(16位) = 1000000000010010

2. Signed Two’s Complement Numbers (带符号二补码表示法)

  • 转换方法
    • 对于二补码的转换,可以直接将符号位移到新位置,并对负数填充符号位的值(1或0),而对于正数,填充零。
  • 例子
    • +18(8位) = 00010010
    • +18(16位) = 0000000000010010
    • -18(8位) = 11101110
    • -32658(8位) = 1000000001101110

3. Sign Extension (符号扩展)

  • 定义

    • 符号扩展是将符号位扩展到新的位长度,并用符号位的值填充扩展部分。对于正数,填充零;对于负数,填充一。
  • 例子

    • -18(8位) = 11101110
    • -18(16位) = 1111111111101110

    其中,负数的符号位为1,因此扩展时用1填充。

总结

  • 带符号幅度表示法:在扩展位长度时,符号位位置不变,其他位用零填充。
  • 带符号二补码表示法:需要根据符号位进行扩展,正数填充零,负数填充一。

Quiz (测验)

1. The most efficient method for performing addition and subtraction operations is ______.

  • Answer:
    C. Signed 2’s Complement Representation

  • Explanation:

    • Signed 2’s Complement Representation is the most efficient method for performing addition and subtraction because it eliminates the need to separately handle positive and negative numbers. It allows for straightforward addition and subtraction operations without special rules for carrying or borrowing based on the sign of the numbers.

2. The range of an 8-bit signed 2’s complement integer is ______.

  • Answer:
    D. [-128, +127]

  • Explanation:

    • In an 8-bit signed 2’s complement representation, the range of values is from $ -2^{7} $ to $ 2^{7} - 1 $, which is -128 to +127. This is because the first bit represents the sign (1 for negative and 0 for positive), and the remaining 7 bits represent the magnitude.

1. Given the 8-bit binary number: 10011101. What decimal number does this represent if the computer uses signed-magnitude representation?

  • Answer:
    B. -29

  • Explanation:

    • Signed-Magnitude Representation uses the leftmost bit as the sign bit. In this case, the leftmost bit is 1, indicating that the number is negative.
    • The remaining bits 0011101 (in binary) represent the magnitude, which is 29 in decimal.
    • Therefore, the decimal value is -29.

2. Given the 8-bit binary number: 10011101. What decimal number does this represent if the computer uses signed two's complement representation?

  • Answer:
    B. -99

  • Explanation:

    • Signed Two’s Complement Representation also uses the leftmost bit as the sign bit. In this case, the leftmost bit is 1, indicating the number is negative.
    • To find the magnitude of the negative number, we first find the two’s complement (invert the bits and add 1):
      • Invert the bits of 10011101: 01100010
      • Add 1 to 01100010: 01100011, which is 99 in decimal.
    • Therefore, the decimal value is -99.