csapp lab1:Data Lab

一、前置知识

1.补码

Two's complement is a mathematical operation to reversibly convert a positive binary number into a negative binary number with equivalent (but negative) value, using the binary digit with the greatest place value (the leftmost bit in big-endian numbers, rightmost bit in little-endian numbers) to indicate whether the binary number is positive or negative (the sign). It is used in computer science as the most common method of representing signed (positive, negative, and zero) integers on computers,[1] and more generally, fixed point binary values. When the most significant bit is a one, the number is signed as negative. (see Converting from two's complement representation, below).

Two's complement is executed by 1) inverting (i.e. flipping) all bits, then 2) adding a place value of 1 to the inverted number. For example, say the number +6 is of interest. +6 in binary is 0110 (the leftmost MSB is needed for the sign; positive 6 is not 110 because it would be interpreted as -2). Step one is to flip all bits, yielding 1001. Step two is to add the place value one to the flipped number, which yields 1010. To verify that 1010 indeed has a value of -6, remember that two's complement makes the most significant bit represent a negative place value, then add the place values: 1010 = 1(-23)+0(22)+1(21)+0(20) = 1(-8) + 0 + 1(2) + 0 = -6.

二进制补码是一种数学运算,可将正二进制数可逆地转换为具有等效(但为负)值的负二进制数,使用具有最大位值的二进制数(大端数中的最左边位,小端数中的最右边位- endian numbers)来指示二进制数是正数还是负数(符号)。它在计算机科学中用作在计算机上表示有符号(正、负和零)整数的最常用方法,[1] 以及更普遍的定点二进制值。当最高有效位为 1 时,该数字被标记为负数。 (请参阅下面的从二进制补码表示转换)。

二进制补码的执行方式是 1) 反转(即翻转)所有位,然后 2) 将位值 1 添加到反转后的数字。例如,假设数字 +6 是有趣的。二进制中的 +6 是 0110(符号需要最左边的 MSB;正 6 不是 110,因为它会被解释为 -2)。第一步是翻转所有位,得到 1001。第二步是将位值 1 与翻转后的数字相加,得到 1010。要验证 1010 确实具有值 -6,请记住二进制补码是最高有效位表示负位值,然后添加位值:1010 = 1*(-23)+0*(22)+1*(21)+0*(20) = 1(-8) + 0 + 1(2 ) + 0 = -6。

二、计算机数字编码方式

大多数机器对整数使用补码编码,而对浮点数使用IEEE标准754编码 ----csapp-2.5