Integer Representation Bits, binary numbers, and bytes Fixed-width - - PowerPoint PPT Presentation

integer representation
SMART_READER_LITE
LIVE PREVIEW

Integer Representation Bits, binary numbers, and bytes Fixed-width - - PowerPoint PPT Presentation

Wellesley CS 240 Integer Representation Bits, binary numbers, and bytes Fixed-width representation of integers: unsigned and signed Modular arithmetic and overflow 1 positional number representation 2 4 0 = 2 x 10 2 + 4 x 10 1 + 0 x 10 0


slide-1
SLIDE 1

Integer Representation

Bits, binary numbers, and bytes Fixed-width representation of integers: unsigned and signed Modular arithmetic and overflow

1

Wellesley CS 240

slide-2
SLIDE 2

positional number representation

  • Base determines:

– –

  • Each position holds a digit.
  • Represented value =

3

2 4

100 10 1 102 101 100 2 1

= 2 x 102 + 4 x 101 + 0 x 100

position weight

slide-3
SLIDE 3

binary = base 2

When ambiguous, subscript with base:

10110 Dalmatians (movie) 101ten 1012-Second Rule (folk wisdom for food safety) 101two

4

1 1 1

8 4 2 1 23 22 21 20 3 2 1

= 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 position weight

irony

slide-4
SLIDE 4

Powers of 2: memorize up to ≥ 210 (in base ten) ex

slide-5
SLIDE 5

conversion and arithmetic

1910 = ?2 10012 = ?10 24010 = ?2 110100112 = ?10 1012 + 10112 = ?2 10010112 x 210 = ?2

7

Show powers, strategies.

ex

slide-6
SLIDE 6

numbers and wires

One wire carries one bit. How many wires to represent a given number? What if I want to build a computer (and not change the hardware later)?

1 0 0 1 1 0 0 0 1 0 0 1

slide-7
SLIDE 7

byte = 8 bits

Smallest unit of data

used by a typical modern computer

Binary 000000002 -- 111111112 Decimal 00010 -- 25510 Hexadecimal 0016 -- FF16

Programmer’s hex notation (C, etc.): 0xB4 = B416 = B4hex

Octal (base 8) also useful.

Why do 240 students often confuse Halloween and Christmas?

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

9

What do you call 4 bits? a.k.a. octet

slide-8
SLIDE 8

word |wərd|, n.

Natural (fixed size) unit of data used by processor.

– Word size determines:

10 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

MSB: most significant bit LSB: least significant bit

slide-9
SLIDE 9

(size in bytes)

Java Data Type C Data Type

32-bit word 64-bit word

boolean 1 1 byte char 1 1 char 2 2 short short int 2 2 int int 4 4 float float 4 4 long int 4 8 double double 8 8 long long long 8 8 long double 8 16

fixed-size data representations

11

Depends on word size

slide-10
SLIDE 10

char: representing characters

A C-style string is represented by a series of bytes (chars).

— One-byte ASCII codes for each character. — ASCII = American Standard Code for Information Interchange

32 space 48 64 @ 80 P 96 ` 112 p 33 ! 49 1 65 A 81 Q 97 a 113 q 34 ” 50 2 66 B 82 R 98 b 114 r 35 # 51 3 67 C 83 S 99 c 115 s 36 $ 52 4 68 D 84 T 100 d 116 t 37 % 53 5 69 E 85 U 101 e 117 u 38 & 54 6 70 F 86 V 102 f 118 v 39 ’ 55 7 71 G 87 W 103 g 119 w 40 ( 56 8 72 H 88 X 104 h 120 x 41 ) 57 9 73 I 89 Y 105 I 121 y 42 * 58 : 74 J 90 Z 106 j 122 z 43 + 59 ; 75 K 91 [ 107 k 123 { 44 , 60 < 76 L 92 \ 108 l 124 | 45

  • 61

= 77 M 93 ] 109 m 125 } 46 . 62 > 78 N 94 ^ 110 n 126 ~ 47 / 63 ? 79 O 95 _ 111

  • 127

del

slide-11
SLIDE 11

Fixed-width integer encodings

Unsigned

non-negative integers only Signed ⊂ both negative and non-negative integers n bits offer only 2n distinct values. Terminology:

13

0110010110101001

“Most-significant” bit(s)

  • r “high-order” bit(s)

“Least-significant” bit(s)

  • r “low-order” bit(s)

MSB LSB

slide-12
SLIDE 12

(4-bit) unsigned integer representation

n-bit unsigned integers: minimum = maximum =

14

1 1 1

8 4 2 1 23 22 21 20 3 2 1

= 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 position weight

slide-13
SLIDE 13

modular arithmetic, overflow

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

unsigned overflow = "wrong" answer = wrap-around

= carry 1 out of MSB = math answer too big to fit

x+y in n-bit unsigned arithmetic is (x + y) mod 2N in math

4-bit unsigned integers

11 + 2 13 + 5

Unsigned addition overflows if and only if a carry bit is dropped.

1011 + 0010 1101 + 0101

slide-14
SLIDE 14

sign-magnitude

Most-significant bit (MSB) is sign bit

0 means non-negative 1 means negative

Remaining bits are an unsigned magnitude 8-bit sign-magnitude: Anything weird here?

00000000 represents _____ 01111111 represents _____ 10000101 represents _____ 10000000 represents _____

17

!!!

ex

00000100 +10000011 Arithmetic?

Example: 4 - 3 != 4 + (-3)

Zero?

slide-15
SLIDE 15

(4-bit) two's complement signed integer representation

4-bit two's complement integers:

minimum = maximum =

19

1 1 1

  • 23

22 21 20

= 1 x -23 + 0 x 22 + 1 x 21 + 1 x 20

slide-16
SLIDE 16

two’s complement vs. unsigned

20

_ _ …_ _ _

2n-1 2n-2 … 22 21 20

  • 2n-1 2n-2

… 22 21 20 two's complement places unsigned places

What's the difference?

n-bit minimum = n-bit maximum =

slide-17
SLIDE 17

8-bit representations

21

1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 1 1 1

n-bit two's complement numbers: minimum = maximum =

ex

slide-18
SLIDE 18

4-bit unsigned vs. 4-bit two’s complement

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

11

  • 5

difference = ___ = 2___

1 0 1 1

1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 1 x -23 + 0 x 22 + 1 x 21 + 1 x 20

4-bit unsigned 4-bit two's complement

slide-19
SLIDE 19

two’s complement addition

23 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1

2 + 3 0010 + 0011

Modular Arithmetic

  • 2

+ 3 1110 + 0011

  • 2

+ -3 1110 + 1101 2 + -3 0010 + 1101

slide-20
SLIDE 20

two’s complement overflow

24 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1

6 + 3 0110 + 0011 Modular Arithmetic

  • 1

+ 2 1111 + 0010

Addition overflows if and only if the arguments have the same sign but the result does not. if and only if the carry in and carry out of the sign bit differ.

Some CPUs/languages raise exceptions on overflow. C and Java cruise along silently... Feature? Oops?

slide-21
SLIDE 21

Ariane 5 Rocket, 1996

Exploded due to cast of 64-bit floating-point number to 16-bit signed number. Overflow.

"... a Model 787 airplane … can lose all alternating current (AC) electrical power … caused by a software counter internal to the GCUs that will overflow after 248 days of continuous power. We are issuing this AD to prevent loss of all AC electrical power, which could result in loss of control of the airplane."

  • -FAA, April 2015

Boeing 787, 2015

Reliability

slide-22
SLIDE 22

A few reasons two’s complement is awesome

Addition, subtraction, hardware Sign Negative one Complement rules

slide-23
SLIDE 23

Another derivation

How should we represent 8-bit negatives?

  • For all positive integers x, x and –x should sum to zero.
  • Use the standard addition algorithm.

00000001 00000010 00000011 + + + 00000000 00000000 00000000

  • Find a rule to represent –x where that works…

27

ex