Data Representation Sean Barker 1 Typical Data Sizes Data Type - - PDF document

data representation
SMART_READER_LITE
LIVE PREVIEW

Data Representation Sean Barker 1 Typical Data Sizes Data Type - - PDF document

Data Representation Sean Barker 1 Typical Data Sizes Data Type Bytes char 1 short 2 int 4 long 8 float 4 double 8 Sean Barker 2 Numbering Systems Decimal Binary Hex 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5


slide-1
SLIDE 1

Sean Barker

Data Representation

1 Sean Barker

Typical Data Sizes

2

Data Type Bytes char 1 short 2 int 4 long 8 float 4 double 8

slide-2
SLIDE 2

Sean Barker

Numbering Systems

3

0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Hex Decimal Binary

Sean Barker

Binary Arithmetic

4

slide-3
SLIDE 3

Sean Barker

Modular Arithmetic

5

128 (10000000) 64 192 255 (11111111) Addition 256.

Unsigned numbers!

Sean Barker

Signed Magnitude

6

  • 1
  • 127

A 1

  • 1 = 00000001,
  • 01, -1 = 10000001

127

= 01111111 10000001 = = 10000000

slide-4
SLIDE 4

Sean Barker

Two’s Complement

7

Two’s Complement

  • 127
  • 1

B 1 127

  • 128

Two’s Complement

  • 1 = 00000001,

From now on, unless we explicitly say otherwise, we’ll assume all integers are stored using two’s complement! This is the standard!

Two’s Complement

  • 1 = 11111111

From now on, unless we explicitly say otherwise, we’ll assume all integers are stored using two’s complement! This is the standard!

= 01111111 = 10000000 10000001 =

Sean Barker

Signed vs Unsigned

8

Signed& 1 2 3 4 5 6 7

  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

Unsigned& 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Bits& 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

=&

+/:&16&

slide-5
SLIDE 5

Sean Barker

Sign Extension

9

0101 00000101 1100 11111100 (5) (-4)

Sean Barker

Lab 1 Warmup

  • Given a signed 32-bit int X, return 1 if X is

positive, 0 if X is zero, and -1 if X is negative.

  • No loops or conditionals!
  • Allowed operators: ! ~ & ^ | + << >>

10

slide-6
SLIDE 6

Sean Barker

Overflow

11

  • 127
  • 1

Signed 1 127

  • 128

128 64 192 255 Unsigned Danger Zone Danger Zone

Sean Barker

“Can’t Sleep”

12

(credit: xkcd.com)

slide-7
SLIDE 7

Sean Barker

Unsigned Bugs...

13

float sum_elements(float a[], unsigned length) { int i; float result = 0; for (i = 0; i <= length - 1; i++) { result += a[i]; } return result; }

Sean Barker

IEEE Floating Point (IEEE 754)

14

value = (–1)s M 2E

s' exp' frac'

1 k = 8 or 11 23 or 52

  • Normalized
  • E = expU - bias
  • bias = 2k-1 - 1
  • M = 1.frac (binary)
  • Denormalized
  • when exp = 00...00
  • E = 1 - bias
  • M = 0.frac (binary)
  • Special Values
  • when exp = 11...11
  • Infinity (frac = 00...00)
  • NaN (frac ≠ 00...00)
slide-8
SLIDE 8

Sean Barker

Floating Point Visualization

15

+∞' −∞' F0' +Denorm' +Normalized' −Denorm' −Normalized' +0' NaN' NaN'

  • 15
  • 10
  • 5

5 10 15 Denormalized Normalized Infinity

6-bit values (3 exp, 2 frac)

  • 1
  • 0.5

0.5 1

Close-up (central values)

Sean Barker

Representing Chars

16