Slp Week02 e
Slp Week02 e
Instructor: Shu, Li
Week02
1
Unit 2. Representation of Data
• 2.0 intro
• 2.1 Bits and Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations
• Motto
2.0 Intro(2/6)
• Base 2
• 2.0 Intro
• 2.1 Bits and Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations
Memory location in
reverse order
Unit 2. Representation of Data
• ~(not).
– bitwise NOT( 按位取非 ), or complement( 补 )
• An unary( 一元 ) operator performs logical negation on
each bit. 0 becomes 1, and vice versa.
• &(ampersand) 位与 110x
– A bitwise AND. For example: AND 0 0 0 1
= 000x
– Use:
• used to select bits from a word or byte :利用 mask :对应位是 1
• Another use of “AND” is to clear a bit. :利用 mask :对应位是 0
• Set bit n of variable x to 0 : x &= ~(1 << n);
• Get just the lowest n bits of variable x : x & ~(~0U << n)
2.1.2 Bit Manipulation(4/8)
• |(pipe) 位或
– A bitwise OR. For example: 010X
OR 0001
= 0101
– Use:
• is used for setting a bit :利用 mask :对应位是 1
• Set bit n of variable x to 1
– x |= (1 << n);
2.1.2 Bit Manipulation(5/8)
• ^(caret) 位异或
– A bitwise EXclusive OR
– Result is 1 if either but not both of the two bits is 1
– For example
0010
XOR 1 0 1 0
= 1000
2.1.2 Bit Manipulation(6/8)
<< >>
Logic shift
Arithmetic shift
2.1.2 Bit Manipulation(7/8)
scanf("%x", &a);
for ( i = 15; i >= 0; i--)
printf ( "%1d", a & 1 << i ? 1 : 0 );
}
Unit 2. Representation of Data
– Subtraction:
• a − b = a + −b
• I.e., to compute a − b, compute −b, then add −b to a
2.2.2 Integer representation(6/7)
3. 1110 1101
+ One’s complement ( 反码 )
0000 0001 (add one to one’s complement)
---------------
1110 1110
Two’s complement( 补码)
2.2.2 Integer representation(7/7)
– Large to small :
• 1. when the original number is within the range of the
small sized one
– sign extension (negative number in 2’s complement)
• 2. otherwise truncated and cause wrong result
2.2 Integers
INTEGER . FRACTION
Value Representation
1/3 0000.01010101[01]... 2
2.3.1 Fixed Point Notation(2/3)
4
••• 2
1
bi bi–1 ••• b2 b1 b0 . b–1 b–2 b–3 ••• b–j
1/2
1/4
•••
1/8
2–j
2.3.1 Fixed Point Notation(3/3)
Decimal: 0 1 2 3 4 5 6 7 8 9
BCD: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Decimal: 127.33
BCD: 0001 0010 0111 .0011 0011
2.3 Non-integral numbers Representations
• Arithmetic formats
– Finite number can be expressed as normalized
scientific notation as (–1)s M 2E , where
• s = a sign ( zero or one)
• M = Coefficient, [1,2)
• E = Exponent
– Two infinities: +∞ and −∞.
– NaN
2.3.3.1 IEEE Standard 754(3/15)
• Encoding (–1)s M 2E
s Exponent Fraction
M = 1.xxx…x2
• xxx…x: bits of FRAC
– Minimum when 000…0 (M = 1.0)
– Maximum when 111…1 (M = 2.0 - )
• Get extra leading bit for “free”
2.3.3.1 IEEE Standard 754(7/15)
(23)10=(10111)2
2.3.3.1 IEEE Standard 754(8/15)
+
-Normalized-Denorm +Denorm+Normalized
3 1 2
NaN
NaN
0 +0
2.3.3.1 IEEE Standard 754(13/15)
• Denormalized Values
– Condition
• exp = 000…0
– Cases
• exp = 000…0, frac = 000…0
– Represents value 0
– Note that have distinct values +0 and -0 by sign bit
• exp = 000…0, frac 000…0
– Numbers very close to 0.0
– “Gradual underflow”
2.3.3.1 IEEE Standard 754(14/15)
• Special Values
– Condition
• exp = 111…1
– Cases
• exp = 111…1, frac = 000…0
– Represents value (infinity)
– Operation that overflows
– Both positive and negative
– E.g., 1.0/0.0 = -1.0/-0.0 = +, 1.0/-0.0 =-
• exp = 111…1, frac 000…0
– Not-a-Number (NaN)
– Represents case when no numeric value can be
determined
– E.g., sqrt(–1),
2.3.3.1 IEEE Standard 754(15/15)
• Encoding
2.3.3 IEEE Floating Point
#include "iostream.h"
#include “iomanip.h”//iomanip.h 是 I/O 流控制头文件
void main() {
float x = 1.2F;
double y = x;
cout << setprecision(20) << x << ", " << y << endl;
cout << "1.2F == 1.2: " << (1.2F == 1.2) << endl;
} //(x==y)
float x = 1.2F;
16.75
2.3.3.2 Floating Point Operations(6/9)
double x = 1.0E160;
// note: largest double is about 1.8E308
// so x * x will overflow x = x * x;
x = x*x;
cout << x << endl;
不同的编译器,
这个值存在差异
The output maybe different
because of different compiler.
2.3.3.2 Floating Point Operations(7/9)
• Floating-point numbers overflow
– The overflow is detectable
– #include <float.h>
– int _finite(double x) returns 1 (true) if x is an ordinary
number and 0 (false) if x is either infinite or not-a-
number (NaN).
– int _fpclass( double x ) returns the status word that
indicates the floating-point class of its argument x.
2.3.3.2 Floating Point Operations(8/9)
• The status word may have one of the following values, defined in
FLOAT.H.
– _FPCLASS_NINF Negative infinity ( –INF)
– _FPCLASS_NN Negative normalized non-zero
– _FPCLASS_ND Negative denormalized
– _FPCLASS_NZ Negative zero ( – 0)
– _FPCLASS_PZ Positive 0 (+0)
– _FPCLASS_PD Positive denormalized
– _FPCLASS_PN Positive normalized non-zero
– _FPCLASS_PINF Positive infinity (+INF)
– _FPCLASS_SNAN /* signaling NaN: like x/0; output -1.#INF 除 0 溢出 */
– _FPCLASS_QNAN /* quiet NaN,like : sqrt(-1); output -1.#IND 非法操作 */
2.3.3.2 Floating Point Operations(9/9)