computer number systems notes... · 2013. 5. 2. · based number systems: 1. base defines set of...

25
SYSC3006 1 Computer Number Systems Thorne, Edition 2 : Section 1.3, Appendix I (Irvine, Edition VI : Section 1.3)

Upload: others

Post on 24-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 1

Computer Number Systems

Thorne, Edition 2 : Section 1.3, Appendix I(Irvine, Edition VI : Section 1.3)

Page 2: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 2

Starting from What We Already Know … Decimal Numbers

Based Number Systems :1. Base defines set of symbols 2. Number = string of symbols3. Interpretation rules

– Digits numbered right to left, start at 0 eg. 4-digit number d3d2d1d0– Value of digit depends on symbol and position within number– Value of number is sum of value of digits

Decimal Numbers : Base-10 with symbols = { 0, 1, 2, 3, … , 9} 4-digit example: d3d2d1d0 = 1436 = d3 × 103 + d2 × 102 + d1 × 101 + d0 × 100

= 1 × 103 + 4 × 102 + 3 × 101 + 6 × 100

Note :1. Addition/Subtraction : We carry and borrow 10’s2. Multiplication/Division by 10 : Shift right/left

Page 3: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 3

Applying Same Methods to Binary Numbers

Binary Numbers : Base-2 with symbols = { 0, 1} 4-digit example: d3d2d1d0 = 1010b= d3 × 23 + d2 × 22 + d1 × 21 + d0 × 20

= 1 × 23 + 0 × 22 + 1 × 21 + 0 × 20

= 1 × 8 + 0 × 4 + 1 × 2 + 0 × 1= 8 + 2= 10d

Note :1. Addition/Subtraction : We carry and borrow 2’s2. Multiplication/Division by 2 : Shift right/left

Page 4: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 4

Applying Same Methods to Binary Numbers

Addition Subtraction

111b 1010b 101b 1001b+ 10b + 111b - 10b - 111b_____ _____ _____ _____

Multiplication by Powers-of-2 Division by Powers-of-2

By 21 : 100b * 10b = By 21 : 100b / 10b =By 22 : 11b * 100b = By 22 : 1100b / 100b = By 23 : 100b * 1000b = By 23 : 100000b / 1000b =

Page 5: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 5

4-bit Binary Numbers you should know

Binary Decimal Binary Decimal

0000 0 1000 80001 1 1001 90010 2 1010 100011 3 1011 110100 4 1100 120101 5 1101 130110 6 1110 140111 7 1111 15

Page 6: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 6

Range of Representation

Within a computer, all numbers are represented in a finite-width– Registers are finite width, depending on processor– Memory cells are 8 or 16 or 32 or 64-width

Range :n-bit values can represent (at most) 2n different counting numbers

· If start representation at 0, then largest value represented is 2n – 1· Previous page : 4-bit numbers

Maximum number of values : 24 = 16Range : 0 .. 15 (24 –1)

Observation : Binary representation often requires lots of bitsΕxample: 60110 = 10010110012 (10 bits!)Converting binary decimal is not so convenient

Page 7: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 7

Hexadecimal Numbers : Base 16

Why Base-16 ? 16 = 24

– Will provide us with a shortcut for working with large binary number analogous to acronyms (eg. CU, UBC, MIT)

Hexadecimal Numbers : Base-16 with symbols = { 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} 4-digit example: d3d2d1d0 = 12AFh= d3 × 163 + d2 × 162 + d1 × 161 + d0 × 160

= 1 × 163 + 2 × 162 + A × 161 + F × 160

= 1 × 4096 + 2 × 256 + 10 × 16 + 15 × 1= 4096 + 512 + 160 + 15= 4783d

Addition/Subtraction : We carry and borrow 16’s· Multiplication/Division by 16 : Shift right/left

Page 8: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 8

Applying Same Methods to Hexadecimal Numbers

Addition Subtraction

181h 1510h 1E1h 1001h+ 89h + E11h - 1Fh - 111h_____ _____ _____ _____

Multiplication by Powers-of-16 Division by Powers-of-16

By 161 : 20h * 10h = By 161 : 105h / 10h =By 162 : 3 * 100h = By 162 : 10E0h / 100h =

Page 9: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 9

Hexadecimal and Binary Numbers : 24 = 16

Converting Binary to Hexadecimal16-bit: 10001010011011102 (= 35,43810)

group in 4’s 1000 1010 0110 1110replace with hex 8 A 6 E16

10-bit: 10010110012 (= 60110)group in 4’s 0010 0101 1001replace with hex 2 5 9 16

Converting Hex Binary : Replace each hex digit by 4-digit binary rep.Example : 134h = 0001 0011 0100b

Page 10: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 10

Common Pitfalls for Beginners

1. What do I mean when I say “ Ten hex” ?

2. Are these two the same thing ?1010 0010b and A2h

3. What are the widths of the following numbers ?

1001 0010b 2h22h 1010h

1010b

4. Memory is always composed of bytesOne byte = 8 bitsContents of a byte = 8 binary digits 0000 0000b … 1111 1111b

= 2 hex digits = (2*4 bits) 00h … FFh= 0 … 255d

Page 11: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 11

Representing Negative Numbers

For unsigned numbers (counting, whole) : – All n bits are used for the magnitude of the number– Example : 4-bit number : 0000b …. 1111b

For signed numbers (negative) :– One bit must be used for the sign :

• Most significant bit = sign bit 0 = positive, 1 = negative– n-1 bits are used for the magnitude of the number.

Encoding the magnitude portion : 2 typical approaches1. Signed magnitude encoding :

• A natural progression from unsigned numbers, but …• No good for math

2. Two’s complement encoding• Hard to understand, but• Math works

Page 12: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 12

Signed Magnitude Encoding of Signed Numbers

Magnitude encoded in remaining n – 1 bits using binary number system (as for counting #s)

Example : 10000001b = -1dmagnitude = 000 0001b = 1sign bit = 1 ∴ negative number

Problems1. Two representations for 0 positive 0 (sign bit = 0)

negative 0 (sign bit = 1)2. Awkward arithmetic operations

0000 0010b 2+ 1000 0001b + -1

1000 0011 -3

Page 13: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 13

Two’s Complement Encoding of Signed Numbers

Magnitude of negative numbers encoded with “flip-and-add”Magnitude of positive numbers encoded as “normal” binary number

Definitions :complement of one bit: b = 1 – b

complement of 0 = 1 complement of 1 = 0· 1s’ complement of an n-bit value: complement each bit· 2’s complement of an n-bit value:

complement each bit, and then add 1 (flip-and-add)· Ignore any carry out of most significant bit

Page 14: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 14

Two’s Complement Encoding of Signed Numbers

Example : Find the 8-bit 2’s complement representation of –1.+110 0000 0001b

Complement 1111 1110bAdd 1 + 1

– 110 1111 1111b = FFh

Encoding of negative numbers is “weird” !

Example : Find the 8-bit 2’s complement representation of +1.+110 0000 0001b = 01h That’s it!

2’s complement operation

Page 15: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 15

Two’s Complement Encoding of Signed Numbers

Example : Find the decimal value of this 2’s complement number FEh(sign bit = 1) 1111 1110bComplement 0000 0001bAdd 1 + 1

0000 0010b = 2dFEh = -2d

Example : Find the decimal value of this 2’s complement number 79h(sign bit = 0) 0111 1001b = 64 + 32 + 16 + 8 + 1

= 121d

2’s complement operation

Page 16: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 16

Range of Signed Representations

Whether using signed magnitude or 2’s complementMSBit = sign bitmagnitude = n-1 remaining bits

Range :n-bit values still represent (at most) 2n different counting numbers, but…

· Use half of binary values for negative values (i.e. 2n–1 values) , the rest for positive (and 0)Range for negative and positive values : All negatives start with 1.

-2n – 1 …. 0 …. 2n – 1 -1 (Total 2n numbers)

Page 17: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 17

Why 2’Complement for Signed Numbers ?

1. Negating a negated number should give the original number– (– x) = x

– 110 1111 1111complement 0000 0000add 1 + 1+110 0000 0001 It works !

2. How many representations for 0?010 0000 0000complement 1111 1111add 1 + 1010 0000 0000 ignore carry out of

most significant bit

Page 18: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 18

One Special Case with 2’Complement for Signed Numbers

Consider 8-bit 2’s complement encodings: • 28 = 256, 27 = 128• range: – 128 . . + 127

(– 2n – 1) (2n – 1 – 1)

+12710 0111 1111complement 1000 0000add 1 + 1– 12710 1000 0001

Page 19: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 19

One Special Case with 2’Complement for Signed Numbers

How to get encoding for – 128 ?1. Using math – 127 – 1 = – 128

– 12710 1000 0001subtract 1 – 1– 12810 1000 0000

2. Using 2’s complement What if we negate – 128 ? [ special case !! ]–12810 1000 0000complement 0111 1111add 1 + 1– 12810 1000 0000

Page 20: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 20

Character Encoding

Representation of “displayable” characters:· characters: { A, B , . . . , Y, Z }· 26 × 2 = 52 (upper and lower case)· decimal (10) digits: {0, 1, . . . , 8, 9 }· punctuation: ! ″ ′ , . – ? / : ;· math symbols: + * = ( – and / above) · brackets: ( ) [ ] { } < >· others: @ # $ % ^ & \ | ~· blank space: “ ”· 90+ symbols (??)

Various encoding schemes have been used

Page 21: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 21

(7-bit) ASCII character encoding

ASCII = American Standard Code for Information Interchange7-Bit ASCII Encoding· 7 bits to encode each character (128 codes)· often extended to 8-bit (byte) values by making most significant bit (msb) = 0[in following: all codes are given as hex values]00 – 1F non-displayable control char’s· 00h NULL· 07h BELL· 08h backspace· 09h tab· 0Ah line feed· 0Ch form feed· 0Dh carriage return· others – often serve special purposes in communication applications

Page 22: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 22

ASCII Code TableDecimal

Page 23: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 23

(7-bit) ASCII character encoding

30h – 39h decimal digit char’s30h “0”

39h “9”

41h – 5Ah Upper Case Letter char’s41h “A”

5Ah “Z”

61h – 7Ah Lower Case Letter char’s61h “a”

7Ah “z”

character “0” ≠ number

Page 24: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 24

(7-bit) ASCII character encoding

• Example : “306 is FUN!”encoding: 33 30 36 20 69 73 20 46 55 4E 21

shorthand 00110000 01110011for binary!

Other Character Encoding Schemes:• IBM standardized an 8-bit scheme (256 char’s) as defacto standard

(PC’s !)• See Appendix I : overlaps with 7-bit ASCII for displayable char’s• Java: unicode – 16-bit scheme (65,536 char’s)multi-lingual character sets

Page 25: Computer Number Systems Notes... · 2013. 5. 2. · Based Number Systems: 1. Base defines set of symbols 2. Number = string of symbols 3. Interpretation rules – Digits numbered

SYSC3006 25

Programmers : Be Aware

Important Concept !!!!!!· fixed-width binary values are used to represent information in

computers· the computer works with the binary representations (NOT the

information!!)· the same binary value can be used to represent different information !

8-bit example: 1111 00002unsigned: 24010signed: – 16108-bit ASCII: “ ≡ ”other?