Вы находитесь на странице: 1из 25

SYSC3006 1

Computer Number Systems

Thorne, Edition 2 : Section 1.3, Appendix I
(Irvine, Edition VI : Section 1.3)
SYSC3006 2
Starting from What We Already Know Decimal Numbers
Based Number Systems :
1. Base defines set of symbols
2. Number = string of symbols
3. Interpretation rules
Digits numbered right to left, start at 0 eg. 4-digit number d
3
d
2
d
1
d
0
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: d
3
d
2
d
1
d
0
= 1436
= d
3
10
3
+ d
2
10
2
+ d
1
10
1
+ d
0
10
0
= 1 10
3
+ 4 10
2
+ 3 10
1
+ 6 10
0
Note :
1. Addition/Subtraction : We carry and borrow 10s
2. Multiplication/Division by 10 : Shift right/left
SYSC3006 3
Applying Same Methods to Binary Numbers
Binary Numbers : Base-2with symbols = { 0, 1}
4-digit example: d
3
d
2
d
1
d
0
= 1010b
= d
3
2
3
+ d
2
2
2
+ d
1
2
1
+ d
0
2
0
= 1 2
3
+ 0 2
2
+ 1 2
1
+ 0 2
0
= 1 8 + 0 4 + 1 2 + 0 1
= 8 + 2
= 10d
Note :
1. Addition/Subtraction : We carry and borrow 2s
2. Multiplication/Division by 2: Shift right/left
SYSC3006 4
Applying Same Methods to Binary Numbers
111b 1010b 101b 1001b
+ 10b + 111b - 10b - 111b
_____ _____ _____ _____
Multiplication by Powers-of-2 Division by Powers-of-2
By 2
1
: 100b * 10b = By 2
1
: 100b / 10b =
By 2
2 :
11b * 100b = By 2
2 :
1100b / 100b =
By 2
3
: 100b * 1000b = By 2
3
: 100000b / 1000b =
SYSC3006 5
4-bit Binary Numbers you should know
Binary Decimal Binary Decimal
0000 0 1000 8
0001 1 1001 9
0010 2 1010 10
0011 3 1011 11
0100 4 1100 12
0101 5 1101 13
0110 6 1110 14
0111 7 1111 15
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) 2
n
different counting numbers
If start representation at 0, then largest value represented is 2
n
1
Previous page : 4-bit numbers
Maximum number of values : 2
4
= 16
Range : 0 .. 15 (2
4
1)
Observation : Binary representation often requires lots of bits
xample: 601
10
= 1001011001
2
(10 bits!)
Converting binary decimal is not so convenient
SYSC3006 7
Why Base-16 ? 16 = 2
4
Will provide us with a shortcut for working with large binary
number analogous to acronyms (eg. CU, UBC, MIT)
Base-16with symbols ={ 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
4-digit example: d
3
d
2
d
1
d
0
=12AFh
=d
3
16
3
+d
2
16
2
+d
1
16
1
+d
0
16
0
= 1 16
3
+2 16
2
+A 16
1
+F 16
0
= 1 4096 +2 256 +10 16 +15 1
= 4096 +512 +160 +15
=4783d
Addition/Subtraction : We carry and borrow 16s
Multiplication/Division by 16: Shift right/left
SYSC3006 8
Applying Same Methods to Hexadecimal Numbers
181h 1510h 1E1h 1001h
+ 89h + E11h - 1Fh - 111h
_____ _____ _____ _____
Multiplication by Powers-of-16 Division by Powers-of-16
By 16
1
: 20h * 10h = By 16
1
: 105h / 10h =
By 16
2 :
3 * 100h = By 16
2 :
10E0h / 100h =
SYSC3006 9
Hexadecimal and Binary Numbers : 2
4
= 16
16-bit: 1000101001101110
2
(= 35,438
10
)
group in 4s 1000101001101110
replace with hex 8 A 6 E
16
10-bit: 1001011001
2
(= 601
10
)
group in 4s 001001011001
replace with hex 2 5 9
16
Converting Hex Binary : Replace each hex digit by 4-digit binary rep.
Example : 134h = 0001 0011 0100b
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 2h
22h 1010h
1010b
4. Memory is always composed of bytes
One byte =8 bits
Contents of a byte =8 binary digits 0000 0000b 1111 1111b
=2 hex digits =(2*4 bits) 00h FFh
=0 255d
SYSC3006 11
Representing Negative Numbers
For unsignednumbers (counting, whole) :
All n bits are used for the magnitude of the number
Example : 4-bit number : 0000b . 1111b
For signednumbers (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 approaches
1. Signed magnitude encoding :
A natural progression from unsigned numbers, but
No good for math
2. Twos complement encoding
Hard to understand, but
Math works
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 = -1d
magnitude = 000 0001b = 1
sign bit = 1 negative number
Problems
1. 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
SYSC3006 13
Twos 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
2s complement of an n-bit value:
Ignore any carry out of most significant bit
SYSC3006 14
Twos Complement Encoding of Signed Numbers
Example : Find the 8-bit 2s complement representation of 1.
+1
10
0000 0001b
Complement 1111 1110b
1
10
1111 1111b = FFh
Encoding of negative numbers is weird !
Example : Find the 8-bit 2s complement representation of +1.
+1
10
0000 0001b = 01h Thats it!
2s complement
operation
SYSC3006 15
Twos Complement Encoding of Signed Numbers
Example : Find the decimal value of this 2s complement number FEh
(sign bit = 1) 1111 1110b
Complement 0000 0001b
0000 0010b = 2d
FEh = -2d
Example : Find the decimal value of this 2s complement number 79h
(sign bit = 0) 0111 1001b = 64 + 32 + 16 + 8 + 1
= 121d
2s complement
operation
SYSC3006 16
Range of SignedRepresentations
Whether using signed magnitude or 2s complement
MSBit = sign bit
magnitude = n-1 remaining bits
Range :
n-bit values still represent (at most) 2
n
different counting numbers,
but
Use half of binary values for negative values (i.e. 2
n1
values) , the rest
for positive (and 0)
Range for negative and positive values : All negatives start with 1.
-2
n 1
. 0 . 2
n 1
-1 (Total 2
n
numbers)
SYSC3006 17
Why 2Complement for Signed Numbers ?
1. Negating a negated number should give the original number
( x) = x
1
10
1111 1111
complement 0000 0000
+1
10
0000 0001 It works !
2. How many representations for 0?
0
10
0000 0000
complement 1111 1111
0
10
0000 0000
ignore carry out of
most significant bit
SYSC3006 18
One Special Case with 2Complement for Signed Numbers
Consider 8-bit 2s complement encodings:
2
8
= 256, 2
7
= 128
range: 128 . . + 127
( 2
n 1
) (2
n 1
1)
+127
10
0111 1111
complement 1000 0000
127
10
1000 0001
SYSC3006 19
One Special Case with 2Complement for Signed Numbers
How to get encoding for 128 ?
1. Using math
127 1 = 128
127
10
1000 0001
subtract 1 1
128
10
1000 0000
2. Using 2s complement
What if we negate 128 ? [ special case !! ]
128
10
1000 0000
complement 0111 1111
128
10
1000 0000
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
SYSC3006 21
(7-bit) ASCII character encoding
ASCII =American Standard Code for Information Interchange
7-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 chars
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
SYSC3006 22
ASCII Code Table
Decimal
SYSC3006 23
(7-bit) ASCII character encoding
30h 39h decimal digit chars
30h 0
39h 9
41h 5Ah Upper Case Letter chars
41h A
5Ah Z
61h 7Ah Lower Case Letter chars
61h a
7Ah z
character 0 number
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 01110011
for binary!
Other Character Encoding Schemes:
IBM standardized an 8-bit scheme (256 chars) as defactostandard
(PCs !)
See Appendix I : overlaps with 7-bit ASCII for displayable chars
J ava: unicode 16-bit scheme (65,536 chars)
multi-lingual character sets
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 0000
2
unsigned: 240
10
signed: 16
10
8-bit ASCII:
other?