- Write out variables x, y and z in binary code
- int8_t x = 67; int8_t y = -7; int8_t z = y - x;
x = 01000011
y = 11111001
z = 10110110
- int8_t x = 0xd3;
x数值溢出
- uint8_t = 0xd3;
x = 11010011
- int8_t x = 127; int8_t y = -7; int8_t z = y – x;
x = 01111111
y = 11111001
z数值溢出
- float x = 1.125;
x = 1.001000
- float x = 23.0;
x = 10111.000000
- float x = 0.45;
x = 0.011100 (存在精度误差)
- 概念解释
1.Method of complements:
In mathematics and computing, the method of complements is a technique used to subtract one number from another using only addition of positive numbers.
2.Byte:
The byte is a unit of digital information that most commonly consists of eight bits, representing a binary number.
3.Integer (computer science):
In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers.
4.Floating point:
In computing, floating-point arithmetic (FP) is arithmetic using formulaic representation of real numbers as an approximation so as to support a trade-off between range and precision.
- 仔细阅读” Method of complements”的内容,你将注意到nines‘ complement in the decimal 和 ones’ complement in binary 等概念. 1)请证明:二进制的负数(two‘s complement of X)等于 X 的 ones’ complement + 1(即,X每位求反加1) 2)Int8_t x = - 017; 请用8进制描述变量 x。在c中017即(017)8
1)证明:
+1的原因就是为了有符号数多出一个数的编码,并且消除了+0与-0的表示。 如果不+1,8bits的编码只能表示-127 ~ 127,而出现+0与-0的编码,即00000000与10000000。 如果+1后,8bits的编码能够表示-128 ~ 127,并且0只有00000000一种表示,而10000000则表示-128。
2)x = 357
- 阅读维基百科” Two‘s complement”的内容,特别是Sign extension小节内容。
1)C程序:int8_t x = -0x1f; int y = x; 请用16进制描述变量 x 和 y,并说明 int y = x 的计算过程。
2)请用数学证明,为什么可以这么计算。
1)x = (-0x1f)16 = (-31)10 = (11100001)2 = (0xE1)16
y = (0xE1)16
2)证明: