0% found this document useful (0 votes)
7 views

Slp Week02 e

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Slp Week02 e

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

System Level Programming

Software College of SCU

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

• Refernce: SLP-2.1~2.3 & CSAPP Chapter 2


2.0 Intro(1/6)

• Motto
2.0 Intro(2/6)

• Let’s consider ways of representing numbers...


– Roman Numerals
• Basic units
–I V X L C D M
– 1 5 10 50 100 500 1000
• Additive combination of units
– II III VI XVI XXXIII MDCLXVI MMXVI
–2 3 6 16 33 1666 2016
• Subtractive combination of units
– IV IX XL XC CD CM MCMLXXI
–4 9 40 90 400 900 1971
2.0 Intro(3/6)

• Let’s consider ways of representing numbers...


– Arabic Numerals
• Developed in India and Arabic world during the
European Dark Age
• Decisive step: invention of zero by Brahmagupta( 婆罗摩笈
多 ) in AD 628
• Basic units
–0123456789
• Positional system( 进位制系统 )
– 1 10 100 1000 10000 100000 1000000
2.0 Intro(4/6)

• Why Base 10?


2.0 Intro(5/6)

• Base 2

• Computer hardware is based on digital logic


– where digital voltages (high and low) represent 1 and 0
2.0 Intro(6/6)
Unit 2. Representation of Data

• 2.0 Intro
• 2.1 Bits and Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations

• Refernce: SLP-2.1~2.3 & CSAPP Chapter 2


Unit 2. Representation of Data

2.1 Bits and Bit Manipulation


– 2.1.1 Bits
– 2.1.2 Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations
2.1.1 Bits(1/6)

• Grouping Bits into Words


– On all modern computers data is accessed in chunks
of 8 bits: 1 byte
– Larger chunks of data (“words”) are formed from
multiple bytes:
• 2 bytes = 16 bits
• 4 bytes = 32 bits
• 8 bytes = 64 bits
– Modern CPUs have instructions for doing operations
on word-sized data values
2.1.1 Bits(2/6)

• Buses in computer system are typically designed to


transfer fixed-sized chunks of bytes known as words.
– The “primitive” C data types typically map onto
machine word sizes
– In C, an int usually corresponds to a word.
– . . . but unfortunately, not in a way that’s completely
consistent across different machines and compilers
2.1.1 Bits(3/6)

• Conceptually, memory (RAM) is a sequence of byte-


sized storage locations
• Each byte storage location has an integer address
– 0 is the lowest address
– Highest address determined by number of address
bits processor uses:
• 32-bit processors ⇒ addresses have 32 bits
• 64-bit processors ⇒ addresses have 64 bits
2.1.1 Bits(4/6)

• 32 bit vs. 64 bit addresses


– 1 GB = 230, 1 TB = 240
– A 32-bit system can directly address 232 bytes (4 GB)
– A 64-bit system can (in theory) directly access 2 64 =
17,179,869,184 GB = 16,777,216 TB
• This is a huge address space
• Note that actual systems don’t support that much
physical memory
• However, tens or hundreds of GB of physical memory is
not uncommon.
2.1.1 Bits(5/6)

• Addresses refer to byte


#include <stdio.h>
void main() { %x output integer in hexadecimal
int i = 0x9a0477f3; format,32 bits total with sign extension
char *ptr;
ptr = (char *)&i; //type casting
for (int j = 0; j <4; j++)
printf("The address is %x , value of byte %d is %x\n",
ptr + j, j, *(ptr +j) );
}
2.1.1 Bits(6/6)
Byte Order:
Little Endian: windows,Linux
F3 77 04 9A
Low address-->high address

Big Endian : MAC OS


9A 04 77 F3
Low address-->high address

Memory location in
reverse order
Unit 2. Representation of Data

• From : SSD6-2.1~2.3 & CSAPP Chapter 2


• 2.1 Bits and Bit Manipulation
– 2.1.1 Bits
– 2.1.2 Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations
2.1.2 Bit Manipulation(1/8)

• There are 6 operator


– NOT ( ~ ),
– AND ( & ),
– OR ( | ),
– EXCLUSIVE OR ( ^ ),
– SHIFT ( << and >> ),

• Except for shifts, bit operations are performed


independently on each bit position.
2.1.2 Bit Manipulation(2/8)

• ~(not).
– bitwise NOT( 按位取非 ), or complement( 补 )
• An unary( 一元 ) operator performs logical negation on
each bit. 0 becomes 1, and vice versa.

~ 0111 (decimal 7) int x=0x0111;


If(!x)
= 1000 (decimal 8) printf (“Oh,no!”);
2.1.2 Bit Manipulation(3/8)

• &(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)

• >> operation in c language Logic or Arithmetic shift?


#include <stdio.h>
void main() {
int x = 0x80000001;
int y;
y = x>>1;
printf("%x,%d",y,y);
}
2.1.2 Bit Manipulation(8/8)

• “>>” and “&” whose priority is higher?


<< is higher than &
#include <stdio.h>
void main() {
int i; >1234
short a;
0001001000110100

scanf("%x", &a);
for ( i = 15; i >= 0; i--)
printf ( "%1d", a & 1 << i ? 1 : 0 );
}
Unit 2. Representation of Data

• From : SSD6-2.1~2.3 & CSAPP Chapter 2


• 2.1 Bits and Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations
2.2 Integers

• 2.2.1 Common integer data types


• 2.2.2 Integer representation
• 2.2.3 Overflow
2.2.1 Common integer data types(1/1)

• Integers come in x86_64 i686


different sizes in char 1 1
computer: char* 8 4
– C: char , short , int , s_int 2 2
long in 32-bit
int 4 4
processors
u_int 4 4
– _int_64 is a type in
Visual C++ long 8 8
– 64-bit processors LL 8 8
UL 8 4
– sizeof(int)
2.2 Integers

• 2.2.1 Common integer data types


• 2.2.2 Integer representation
• 2.2.3 Overflow
2.2.2 Integer representation(1/7)

• How is the integer stored in computer ?


– Unsigned integer
• E.g.: 100101102 = 15010

– Signed integer: ???


• About half of encoding space used for negative values
• Each represented integer has a unique encoding as bit
string
• Straightforward way
2.2.2 Integer representation(2/7)

• Sign magnitude representation


– Let most significant bit be a sign bit: 0→positive,
1→negative

– Downsides: two representations of 0, arithmetic


complicated by sign bit
2.2.2 Integer representation(3/7)
• Ones’ complement ( 反码 )
– to represent -x, invert all of the bits of x

– Downsides: two representations of 0, slightly complicated


arithmetic
2.2.2 Integer representation(4/7)
• Two’s complement( 补码 )
– in w-bit word, the most significant bit represents −2w−1

– w-bit two’s complement representation


• Bit w-1 is the sign bit, 0→positive, 1→negative
• If sign bit is 0, usual unsigned interpretation
• If sign bit is 1, bits w − 2 . . 0 indicate the “offset” from −2 w−1
*
2.2.2 Integer representation(5/7)
• Two’s complement negation and subtraction
– Negation:
• if x is a two-complement integer value, −x can be
computed by inverting bits of x, then adding 1

– 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)

Represent -18 in 2’s complement


1. 1 001 0010 //original data (true form 原码 )

2. 1110 1101 //reverse 1 to 0, 0 to 1

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)

• Integers Conversion Between Different Size


– Small to large
• signed: sign extension
• unsigned: fill in 0s

– 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

• 2.2.1 Common integer data types


• 2.2.2 Integer representation
• 2.2.3 Overflow
2.2.3 Overflow(1/2)

• When integers are too big to fit into a word


– Example of Overflow: 16-bit unsigned integer
• If you add 65535 + 1 (dec), it produces 0

1111 1111 1111 1111 (binary)


+ 0000 0000 0000 0001
-----------------------------------------------
1 0000 0000 0000 0000 (overflow)

• Integer overflow is not detected or reported in C, so


the programmer must choose integer sizes carefully
2.2.3 Overflow(2/2)

• Sometimes, it is necessary to doing the following


code:
/* If overflow occurred, sum will be smaller than
either x or y. Otherwise, sum will be greater than
either x or y. */

unsigned long x, y, sum;


sum = x + y;
if (sum < x)
handle_overflow();
Unit 2. Representation of Data

• From : SSD6-2.1~2.3 & CSAPP Chapter 2


• 2.1 Bits and Bit Manipulation
• 2.2 Integers
• 2.3 Non-integral numbers Representations
2.3 Non-integral numbers Representations

• 2.3.1 Fixed Point Notation


• 2.3.2 BCD (Binary-Coded Decimal)
• 2.3.3 IEEE Floating Point
2.3.1 Fixed Point Notation(1/3)

• Splits numbers using a point “.”


– Integer section
– Fractional section

INTEGER . FRACTION

Value Representation

5.75 (3/4) 0101.11002

1/3 0000.01010101[01]... 2
2.3.1 Fixed Point Notation(2/3)

• Binary fixed-point numbers


– Bits to right of “binary point” represent fractional
powers of 2 i
– Represents rational number( 有理数 ):  bk 2 k
k  j
2i
2i–1

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)

• Limitation: How to improve ?


– Can only exactly represent numbers of the form x/2 k
– Other numbers have repeating bit representations

• Degree of precision is decided by position of ‘point’


2.3 Non-integral numbers Representations

• 2.3.1 Fixed Point Notation


• 2.3.2 BCD (Binary-Coded Decimal)
• 2.3.3 IEEE Floating Point
2.3.2 BCD(1/1)

• Binary Coded Decimal


– each decimal digit is stored in a four-bit nibble.

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

• 2.3.1 Fixed Point Notation ( 定点数形式 )


• 2.3.2 BCD (Binary-Coded Decimal)
• 2.3.3 IEEE Floating Point
2.3.3 IEEE Floating Point

• 2.3.3.1 IEEE Standard 754


• 2.3.3.2 Floating Point Operations
2.3.3.1 IEEE Standard 754(1/15)

• IEEE Standard 754


– Established in 1985 as uniform standard for floating
point arithmetic
– Supported by all major CPUs
– Driven by Numerical Concerns
• Nice standards for rounding, overflow( 上溢 ),
underflow( 下溢 )
2.3.3.1 IEEE Standard 754(2/15)

• 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

• MSB is sign bit


• Exponent field encodes E as biased value
• Fraction field encodes M in true form
2.3.3.1 IEEE Standard 754(4/15)

• float: Single precision

• double: Double precision

• long double: Quadruple precision


2.3.3.1 IEEE Standard 754(5/15)

• 1. Exponent : unsigned value


– Exponent coded as biased value
Exponent( 机器表示 ( 移码 )) =E ( 真值 )+ Bias( 偏移量 )

-126 1 127 254


– Bias : bias value
• Single precision: 127 (Exp: 1…254, E: -126…127)
• Double precision: 1023 (Exp: 1…2046, E: -1022…1023)
• in general: Bias = 2e-1 - 1, where e is number of
exponent bits
2.3.3.1 IEEE Standard 754(6/15)

• 2. Fraction coded with implied leading 1

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)

• How would 23.7510 be stored?


– 1. Converting 23 to binary
2 23 (1
2 11 (1 10111
2 5 (1
2 2 (0
2 1 (1
0

(23)10=(10111)2
2.3.3.1 IEEE Standard 754(8/15)

• How would 23.7510 be stored?


– 2. Converting 0.75 to binary

0.75 × 2 = 1.5 (0.75)10 = (0.11)2


0.5 × 2 = 1
2.3.3.1 IEEE Standard 754(9/15)
• Float F = 23.75;
– 23.7510 = 10111.11 2 = 1.0111112 x 24
• Coefficient
– M = 1. 0111112
– fraction = 0111 1100 0000 0000 0000 0002
• Exponent
– E = 4
– Bias = 127
– Exponent = 131 = 100000112

Floating Point Representation:


Hex: 4 1 B E 0 0 0 0
Binary: 0100 0001 1011 1110 0000 0000 0000
0000
100 0001 1
1011 1110 0000 0000 0000 0000
2.3.3.1 IEEE Standard 754(10/15)
• How would -23.75 be stored?
– Just change the sign bit: C1 BE 00 00.
– Do not take the two’s complement!

Floating Point Representation:


Hex: C 1 B E 0 0 0 0
Binary: 1100 0001 1011 1110 0000 0000 0000 0000
100 0001 1
011 1110 0000 0000 0000 0000
2.3.3.1 IEEE Standard 754(11/15)
• 1 bit sign bit, 8 bit exponent, and 23 bit coefficient
s exp frac
• Determine the values of
1011 1101 0100 0000 0000 0000 0000 0000 2
– Sign = 1, negative
– Exponent = 011 1101 02 = 12210
– E = 12210 –12710 = -510
– Fraction = 100 0000 0000 0000 0000 0000 2
– M = 1.100 0000 0000 0000 0000 0000 2 = 1.510
• Result: (-1)1 x 1.5 x 2(-5) = -0.04687510
2.3.3.1 IEEE Standard 754(12/15)

• Summary of Floating Point Real Number Encodings


“Normalized” Numeric Values:
Exp  000…0 and Exp  111…1

 +
-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

• 2.3.3.1 IEEE Standard 754


• 2.3.3.2 Floating Point Operations
2.3.3.2 Floating Point Operations(1/9)

• Floating point arithmetic on a computer


– In mathematics, all numbers are accurate.
– On computer : numbers are approximations.
– 1.2

– All calculations are performed with limited precision.


2.3.3.2 Floating Point Operations(2/9)

#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)

The output is:


FALSE! The two
1.2, 1.20000004768372 values are not equal!
1.2F == 1.2: 0
2.3.3.2 Floating Point Operations(3/9)

float x = 1.2F;

float x = 1.2;//1.2 here is const double


warning C4305: 'initializing' : truncation from 'const
double' to 'float'
2.3.3.2 Floating Point Operations(4/9)

• A multiple choice question:

Which of the following numerical operations is most


likely to lead to loss of precision?
– a. Floating-point multiplication
– b. Floating-point addition
– c. Integer addition
– d. Integer multiplication
2.3.3.2 Floating Point Operations(5/9)

• Losing Precision - Addition


– To add two floating point numbers, the exponents must be
equal. If they are not already equal, then they must be adjusted
by shifting the coefficient of the number with the smaller
exponent.

• E.g., 10.375 + 6.34375 = 16.71875


1.0100110 × 23
+ 1.1001101 × 22
-----------------------------------------

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)

• Floating-point numbers underflow


– Underflow occurs when the required exponent is too
small (negative) and cannot be represented.
Summary

• 1. representation and overflow


– Integer and non-integral number

• 2. representation and precision


– Compare float-point numbers
– Conversion between different data type

You might also like