Number Systems
Computer Arithmetic
CS 21 - Computer Organization and Assembly Language Programming Lecture 1 Number Systems and Computer Arithmetic
University of the Philippines - Diliman College of Engineering Department of Computer Science
Number Systems
Outline
1
Number Systems
2
Computer Arithmetic
Computer Arithmetic
Number Systems
Outline
1
Number Systems
2
Computer Arithmetic
Computer Arithmetic
Number Systems
Computer Arithmetic
Number systems 1 2
3
numbers could be represented in any base in a base system, digits used range from 0 to (base - 1). For example, in base 3, digits 0, 1 and 2 are used. prefix/subscript/suffix used in indicating what base representation was used
Number Systems
Computer Arithmetic
Number systems 1 2
3
4
numbers could be represented in any base in a base system, digits used range from 0 to (base - 1). For example, in base 3, digits 0, 1 and 2 are used. prefix/subscript/suffix used in indicating what base representation was used which of the following is an invalid number?
Number Systems
Binary Number System
1
base 2 natural for computers - on and off signals
2
base 10 natural for human - why?
Computer Arithmetic
Number Systems
Computer Arithmetic
Decimal to Binary Conversion from Decimal to Binary: 1 Divide the decimal number by 2, store the remainder 2 Continuously divide until quotient becomes less than 1 3 Read the remainders in reverse acquisition order
Number Systems
Computer Arithmetic
Question
What is the highest number we could represent using n-bits?
Number Systems
Computer Arithmetic
Binary to Decimal Conversion from Binary to Decimal: 1
Starting from the right, multiply the binary digit by powers of 2. Begin at 2 raised to 0(which is 1).
2
Add sums
Number Systems
Hexadecimal Number System Hexadecimal Digits - Decimal equivalent 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=8 9=9 A = 10 B = 11 C = 12 D = 13 E = 14 F = 15
Computer Arithmetic
Number Systems
Computer Arithmetic
Decimal to Hexadecimal Conversion
How to convert from Decimal to Hexadecimal? Vice-versa?
Number Systems
Computer Arithmetic
Decimal to Hexadecimal Conversion
How to convert from Decimal to Hexadecimal? Vice-versa? same procedure as Decimal to Binary/Binary to Decimal - just replace the divisor by 16, and the powers of 2 by powers of 16!
Number Systems
Hexadecimal and Binary Remember: Bit = smallest unit Nibble = 4 bits Byte = 8 bits or 2 nibbles Word = depends on number of bits in processor
Computer Arithmetic
Number Systems
Computer Arithmetic
Hexadecimal and Binary Remember: Bit = smallest unit Nibble = 4 bits Byte = 8 bits or 2 nibbles Word = depends on number of bits in processor Binary �Shorthand� 1 hexadecimal digit could represent the value of 4 bits when converting from binary to hexadecimal, no need to pass through decimal format
Number Systems
Computer Arithmetic
Hexadecimal and Binary Remember: Bit = smallest unit Nibble = 4 bits Byte = 8 bits or 2 nibbles Word = depends on number of bits in processor Binary �Shorthand� 1 hexadecimal digit could represent the value of 4 bits when converting from binary to hexadecimal, no need to pass through decimal format
Note that this could be done for octal as well
Number Systems
Outline
1
Number Systems
2
Computer Arithmetic
Computer Arithmetic
Number Systems
Arithmetic Operation: Addition An Example: What is 1001 + 0101? Is this correct?
Computer Arithmetic
Number Systems
Computer Arithmetic
Arithmetic Operation: Addition Solve: 1001 + 0101. Remember, possible sums of 2 addend bits are 0, 1 and 2. A single sum bit is not enough to represent all those!
Number Systems
Computer Arithmetic
Arithmetic Operation: Addition Solve: 1001 + 0101. Remember, possible sums of 2 addend bits are 0, 1 and 2. A single sum bit is not enough to represent all those! Solution: carryout!
Number Systems
Computer Arithmetic
Arithmetic Operation: Addition Solve: 1001 + 0101. Remember, possible sums of 2 addend bits are 0, 1 and 2. A single sum bit is not enough to represent all those! Solution: carryout!
So in each column, how many bits are we actually adding?
Number Systems
Computer Arithmetic
Arithmetic Operation: Addition Solve: 1001 + 0101. Remember, possible sums of 2 addend bits are 0, 1 and 2. A single sum bit is not enough to represent all those! Solution: carryout!
So in each column, how many bits are we actually adding?
Number Systems
Computer Arithmetic
Overflow What is overflow? Overflow indicates an insufficiency of the number of our bits to represent the sum
Number Systems
Computer Arithmetic
Overflow What is overflow? Overflow indicates an insufficiency of the number of our bits to represent the sum How is this detected?
Number Systems
Computer Arithmetic
Overflow What is overflow? Overflow indicates an insufficiency of the number of our bits to represent the sum How is this detected? If the CARRYOUT value of the LEFTMOST digits is 1, overflow occured.
Number Systems
Computer Arithmetic
Arithmetic Operation: Multiplication
How could we do multiplication? Could we pull off multiplication using just ADDITION?
Number Systems
Computer Arithmetic
Arithmetic Operation: Multiplication
How could we do multiplication? Could we pull off multiplication using just ADDITION? Yes! Multiplication is just a repeated addition
Number Systems
Computer Arithmetic
Arithmetic Operation: Multiplication
How could we do multiplication? Could we pull off multiplication using just ADDITION? Yes! Multiplication is just a repeated addition Take note: Another possible hardware solution - ROM!
Number Systems
Arithmetic Operation: Subtraction
How could we do subraction? What is subtraction in essence anyway?
Computer Arithmetic
Number Systems
Computer Arithmetic
Arithmetic Operation: Subtraction
How could we do subraction? What is subtraction in essence anyway? It’s just an addition between oppositely signed numbers 1-1=0 1 + (-) 1 = 0
Number Systems
Signed Binary Numbers
How do we represent negative binary numbers?
Computer Arithmetic
Number Systems
Signed Binary Numbers
How do we represent negative binary numbers? We use 2C or Two’s Complement Notation.
Computer Arithmetic
Number Systems
Computer Arithmetic
Two’s Complement Notation
We get the negative of a binary number by doing the following:
Number Systems
Computer Arithmetic
Two’s Complement Notation
We get the negative of a binary number by doing the following: 1
Get the complement of each and every digit in the number. This is called its One’s Complement Representation
Number Systems
Computer Arithmetic
Two’s Complement Notation
We get the negative of a binary number by doing the following: 1
Get the complement of each and every digit in the number. This is called its One’s Complement Representation
2
Add 1 to the One’s Complement
Number Systems
Two’s Complement Notation
Why don’t we just use the 1C Notation?
Computer Arithmetic
Number Systems
Computer Arithmetic
Two’s Complement Notation
Why don’t we just use the 1C Notation? There are 2 representations for 0: 0000 and 1111 - confusing!
Number Systems
Computer Arithmetic
Two’s Complement Notation
Characteristics of Two’s Complement Notation Leftmost digit tells us whether the number is positive or negative - 0 for positive, 1 for negative
Number Systems
Computer Arithmetic
Two’s Complement Notation
Characteristics of Two’s Complement Notation What is the implication of this for the representable range?
Number Systems
Computer Arithmetic
Two’s Complement Notation
Characteristics of Two’s Complement Notation Binary numbers in 2C notation would usually have the subscript ”2C” to differentiate from those in ordinary notation Do we convert to decimal the same way as before?
Number Systems
Computer Arithmetic
Overflow in 2C Addition
How do we detect overflow in 2C addition? Leftmost CARRYOUT value no longer indicative. Instead, we check the signs of addends and sum. If signs of operands are different, overflow could NOT occur! (subtraction) If signs of operands are the same, sum MUST also have the same sign. If not, then overflow occured.
Number Systems
Computer Arithmetic
Usage
Where do we use unsigned numbers? 2C? 2C notation is used where signed numbers are needed - data. Unsigned notation used in character-encoding schemes and addresses.
Number Systems
Computer Arithmetic
Usage
Where do we use unsigned numbers? 2C? 2C notation is used where signed numbers are needed - data. Unsigned notation used in character-encoding schemes and addresses. ASCII Unicode
Number Systems
ASCII
Computer Arithmetic
Number Systems
Unicode
Computer Arithmetic
Number Systems
Computer Arithmetic
So...
Any questions?