0% found this document useful (0 votes)
17 views

Instructor: Tim Dosen DDAK Slide By: Erdefi Rakun: Fasilkom UI

Uploaded by

Diona Varas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Instructor: Tim Dosen DDAK Slide By: Erdefi Rakun: Fasilkom UI

Uploaded by

Diona Varas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

CSIM601251

Instructor: Tim Dosen DDAK


Slide By : Erdefi Rakun
Fasilkom UI
• Review of information representation
• Base conversion
• Binary arithmetic operations
• Negative numbers
Bit (Binary digit)
0 and 1
Represent false and true in logic
Represent the low and high states in electronic
devices
Other units
Byte: 8 bits
Nibble: 4 bits (seldom used)
Word: Multiples of byte (e.g.: 1 byte, 2 bytes, 4
bytes, 8 bytes, etc.), depending on the
architecture of the computer system

Introduction 3
N bits can represent up to 2N values.
Examples:
2 bits  represent up to 4 values (00, 01, 10, 11)
3 bits  rep. up to 8 values (000, 001, 010, …, 110, 111)
4 bits  rep. up to 16 values (0000, 0001, 0010, …., 1111)

To represent M values, log2M bits are required.


Examples:
32 values  requires 5 bits
64 values  requires 6 bits
1024 values  requires 10 bits
40 values  how many bits?
100 values  how many bits?

Introduction 4
A weighted-positional number system
• Base or radix is 10 (the base or radix of a number system is
the total number of symbols/digits allowed in the system)
• Symbols/digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
• Position is important, as the value of each symbol/digit is
dependent on its type and its position in the number
• Example, the 9 in the two numbers below has different
values:
• (7594)10 = (7 × 103) + (5 × 102) + (9 × 101) + (4 × 100)
• (912)10 = (9 × 102) + (1 × 101) + (2 × 100)
• In general, (a a … a . f f … f ) =
n n-1 0 1 2 m 10
(an x 10n) + (an-1x10n-1) + … + (a0 x 100) +
(f1 x 10-1) + (f2 x 10-2) + … + (fm x 10-m)
Introduction 5
• Weighing factors (or weights) are in powers of 10:
… 103 102 101 100 . 10-1 10-2 10-3 …
• To evaluate the decimal number 593.68, the digit
in each position is multiplied by the
corresponding weight:
5×102 + 9×101 + 3×100 + 6×10-1 + 8×10-2
= (593.68)10

Introduction 6
• Binary (base 2)
 Weights in powers of 2
 Binary digits (bits): 0, 1
• Octal (base 8)
 Weights in powers of 8
 Octal digits: 0, 1, 2, 3, 4, 5, 6, 7.
• Hexadecimal (base 16)
 Weights in powers of 16
 Hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
• Base/radix R:
 Weights in powers of R

Introduction 7
In some programming languages/software, special
notations are used to represent numbers in certain
bases
• In programming language C
 Prefix 0 for octal. E.g.: 032 represents the octal number (32)8
 Prefix 0x for hexadecimal. E.g.: 0x32 represents the hexadecimal
number (32)16
• In PCSpim (a MIPS simulator)
 Prefix 0x for hexadecimal. E.g.: 0x100 represents the
hexadecimal number (100)16
• In Verilog, the following values are the same
 8’b11110000: an 8-bit binary value 11110000
 8’hF0: an 8-bit binary value represented in hexadecimal F0
 8’d240: an 8-bit binary value represented in decimal 240

Introduction 8
• Review of information representation
• Base conversion
• Binary arithmetic operations
• Negative numbers
• Useful for Base Conversion

Exponent Value Exponent Value


0 1 11 2,048
1 2 12 4,096
2 4 13 8,192
3 8 14 16,384
4 16 15 32,768
5 32 16 65,536
6 64 17 131,072
7 128 18 262,144
8 256 19 524,288
9 512 20 1,048,576
10 1024 21 2,097,152
Number Systems and Codes 10
• To convert to decimal, use decimal arithmetic to
form S (digit × respective power of 2).
• Example:Convert 110102 to N10:

Number Systems and Codes 11


Easy!
1101.1012 = 1×23 + 1×22 + 1×20 + 1×2-1 + 1×2-3

572.68 =

2A.816 =

341.245 =

Number Systems and Codes 12


• Method 1
 Sum-of-Weights Method
• Method 2
 Repeated Division-by-2 Method (for whole
numbers)
 Repeated Multiplication-by-2 Method (for
fractions)

Number Systems and Codes 14


Determine the set of binary weights
whose sum is equal to the decimal
number
 (9)10 = 8 + 1 = 23 + 20 = (1001)2
 (18)10 = 16 + 2 = 24 + 21 = (10010)2
 (58)10 = 32 + 16 + 8 + 2 = 25 + 24 + 23 + 21 = (111010)2
 (0.625)10 = 0.5 + 0.125 = 2-1 + 2-3 = (0.101)2

Number Systems and Codes 15


To convert a whole number to binary, use
successive division by 2 until the quotient is 0.
The remainders form the answer, with the first
remainder as the least significant bit (LSB) and the
last as the most significant bit (MSB).
(43)10 = (101011)2 2 43
2 21 rem 1  LSB
2 10 rem 1
2 5 rem 0
2 2 rem 1
2 1 rem 0
0 rem 1  MSB
Number Systems and Codes 16
To convert decimal fractions to binary, repeated
multiplication by 2 is used, until the fractional
product is 0 (or until the desired number of
decimal places). The carried digits, or carries,
produce the answer, with the first carry as the
MSB, and the last as the LSB.
(0.3125)10 = (.0101)2 Carry
0.3125×2=0.625 0 MSB
0.625×2=1.25 1
0.25×2=0.50 0
0.5×2=1.00 1 LSB

Number Systems and Codes 17


 Base-R to decimal: multiply digits with their
corresponding weights.
 Decimal to binary (base 2)
• Whole numbers repeated division-by-2
• Fractions: repeated multiplication-by-2
 Decimal to base-R
• Whole numbers: repeated division-by-R
• Fractions: repeated multiplication-by-R

Number Systems and Codes 18


QUICK REVIEW QUESTIONS (3)
• DLD page 37
Questions 2-5 to 2-8.
2-5. Convert each of the following decimal numbers to binary (base two) with at
most eight digits in the fractional part, rounded to eight places.
a. 2000 b. 0.875 c. 0.07 d. 12.345

2-6. Convert each of the decimal numbers in question 2-5 above to septimal (base 7)
with at most six digits in the fractional part rounded to six places.

2-7. Convert each of the decimal numbers in question 2-5 above to octal (base 8)
with at most four digits in the fractional part rounded to four places.

2-8. Convert each of the decimal numbers in question 2-5 above to hexadecimal
(base 16) with at most two digits in the fractional part rounded to two places.

Number Systems and Codes 19


• In general, conversion between
bases can be done via decimal:
Base-2 Base-2
Base-3 Base-3
Base-4 Decimal Base-4
… ….
Base-R Base-R

• Shortcuts for conversion between


bases 2, 4, 8, 16 (see next slide)

Number Systems and Codes 20


• Binary  Octal: partition in groups of 3
(10 111 011 001 . 101 110)2 =

• Octal  Binary: reverse


(2731.56)8 =

• Binary  Hexadecimal: partition in


groups of 4
(101 1101 1001 . 1011 1000)2 =

• Hexadecimal  Binary: reverse


(5D9.B8)16 =
Number Systems and Codes 21
QUICK REVIEW QUESTIONS (4)
• DLD page 37
Questions 2-9 to 2-10.

2-9. Which of the following octal value is equivalent to the binary


number (110001)2?
a. (15)8 b. (31)8 c. (33)8 d. (49)8 e. (61)8

2-10. Convert the binary number (1001101)2 to


a. quaternary b. octal c. decimal d. hexadecimal

Number Systems and Codes 22


• Good idea to memorize!
Decimal Binary Octal Hexadecimal
(Base 10) (Base 2) (Base 8) (Base 16)
00 00000 00 00
01 00001 01 01
02 00010 02 02
03 00011 03 03
04 00100 04 04
05 00101 05 05
06 00110 06 06
07 00111 07 07
08 01000 10 08
09 01001 11 09
10 01010 12 0A
11 01011 13 0B
12 01100 14 0C
13 01101 15 0D
14 01110 16 0E
15 01111 17 0F
16 10000 20 10
Number Systems and Codes 23
• Review of information representation
• Base conversion
• Binary arithmetic operations
• Negative numbers
• Single Bit Addition with Carry
• Multiple Bit Addition
• Single Bit Subtraction with Borrow
• Multiple Bit Subtraction
• Multiplication
• Division

Introduction 25
Given two binary digits (X,Y), a carry in (Z) we get the
following sum (S) and carry (C):
Carry in (Z) of 0: Z 0 0 0 0
X 0 0 1 1
+Y +0 +1 +0 +1
CS 00 01 01 10

Carry in (Z) of 1: Z 1 1 1 1
X 0 0 1 1
+Y +0 +1 +0 +1
CS 01 10 10 11

Introduction 26
• Extending this to two multiple bit examples:
Carries 0 0
Augend 01100 10110
Addend + 10001 + 10111
Sum

• Note: The 0 is the default Carry-In to the least significant bit.

Introduction 27
• Given two binary digits (X,Y), a borrow in (Z) we
get the following difference (S) and borrow (B):
• Borrow in (Z) of 0: Z 0 0 0 0

X 0 0 1 1

-Y -0 -1 -0 -1

BS 00 11 01 00

• Borrow in (Z) of 1: Z 1 1 1 1

X 0 0 1 1

-Y -0 -1 -0 -1

BS 11 10 00 11

Introduction 28
• Extending this to two multiple bit examples:
Borrows 0 0
Minuend 10110 10110
Subtrahend - 10010 - 10011
Difference
• Notes:
The 0 is a Borrow-In to the least significant bit. If the
Subtrahend > the Minuend, interchange and append a – to the
result.

Introduction 29
The binary multiplication table is simple:
0∗0=0 | 1∗0=0 | 0∗1=0 | 1∗1=1
Extending multiplication to multiple digits:
Multiplicand 1011
Multiplier × 101
Partial Products 1011
0000-
+ 1011--
Product 110111

Introduction 30
QUICK REVIEW QUESTIONS (1)

What is (1011)2 x (101)2 ?


a. (10000)2 b. (110111)2 c. (111111)2 d. (111011)2 e. (101101)2

Perform the following operations on binary numbers.


a. (10111110)2 + (10001101)2
b. (11010010)2 – (01101101)2
c. (11100101)2 – (00101110)2

Introduction 31
100110 Quotient
Divisor 100010 100101010 Dividend
- 1000
10
101
1010
- 1000
1010 Remainder

Dividend = Quotient x Divisor + Remainder


Introduction 32
101011 = 43
12 521 = 01100 01000001001
- 384 = - 0110000000
137 = 010001001
- 0 = - 0
137 = 010001001
- 96 = - 01100000
41 = 0101001
- 0 = - 0
41 = 0101001
- 24 = - 011000
17 = 010001
- 12 = - 01100
5 = 101

Taken from Daniel J. Sorin Slide Introduction 33


Arithmetic 34
• Review of information representation
• Base conversion
• Binary arithmetic operations
• Negative numbers
• Unsigned numbers: only non-negative
values.
• Signed numbers: include all values
(positive and negative)
• There are 3 common representations for
signed binary numbers:
 Sign-and-Magnitude
 1s Complement
 2s Complement
Number Systems and Codes 36
•The sign is represented by a ‘sign bit’
•0 for +
•1 for -
•E.g.: a 1-bit sign and 7-bit magnitude format.

sign magnitude

 00110100  +1101002 = +5210


 10010011  -100112 = -1910
Number Systems and Codes 37
• Largest value: 01111111 = +12710
• Smallest value: 11111111 = -12710
• Zeros: 00000000 = +010
10000000 = -010
• Range: -12710 to +12710
• Question:
For an n-bit sign-and-magnitude
representation, what is the range of values
that can be represented?

Number Systems and Codes 38


• To negate a number, just invert the sign bit.
• Examples:
 How to negate 00100001sm (decimal 33)?
Answer: 10100001sm (decimal -33)
 How to negate 10000101sm (decimal -5)?
Answer: 00000101sm (decimal +5)

Number Systems and Codes 39


• Given a number x which can be expressed as an n-bit
binary number, its negated value can be obtained in 1s-
complement representation using:
-x = 2n – x – 1
• Example: With an 8-bit number 00001100 (or 1210), its
negated value expressed in 1s-complement is:
-000011002 = 28 – 12 – 1 (calculation in decimal)
= 243
= 111100111s
(This means that -1210 is written as 11110011 in 1s-
complement representation.)

Number Systems and Codes 40


• Essential technique to negate a value: invert all
the bits.
• Largest value: 01111111 = +12710
• Smallest value: 10000000 = -12710
• Zeros: 00000000 = +010
11111111 = -010
• Range: -12710 to +12710
• The most significant (left-most) bit still
represents the sign: 0 for positive; 1 for
negative.

Number Systems and Codes 41


Examples (assuming 8-bit numbers):
(14)10 = (00001110)2 = (00001110)1s
-(14)10 = -(00001110)2 = (11110001)1s
-(80)10 = -( ? )2 = ( ? )1s

Number Systems and Codes 42


• Given a number x which can be expressed as an n-bit
binary number, its negated value can be obtained in
2s-complement representation using:
-x = 2n – x
• Example: With an 8-bit number 00001100 (or 1210), its
negated value expressed in 1s-complement is:
-000011002 = 28 – 12 (calculation in decimal)
= 244
= 111101002s
(This means that -1210 is written as 11110100 in 2s-
complement representation.)

Number Systems and Codes 43


• Essential technique to negate a value: invert
all the bits, then add 1.
• Largest value: 01111111 = +12710
• Smallest value: 10000000 = -12810
• Zero: 00000000 = +010
• Range: -12810 to +12710
• The most significant (left-most) bit still
represents the sign: 0 for positive; 1 for
negative

Number Systems and Codes 44


Examples (assuming 8-bit numbers):
(14)10 = (00001110)2 = (00001110)2s
-(14)10 = -(00001110)2 = (11110010)2s
-(80)10 = -( ? )2 = ( ? )2s

Number Systems and Codes 45


4-bit system
Positive values Negative values

Value Sign-and- 1s 2s Value Sign-and- 1s 2s


Magnitude Comp. Comp. Magnitude Comp. Comp.
+7 0111 0111 0111 -0 1000 1111 -
+6 0110 0110 0110 -1 1001 1110 1111
+5 0101 0101 0101 -2 1010 1101 1110
+4 0100 0100 0100 -3 1011 1100 1101
+3 0011 0011 0011 -4 1100 1011 1100
+2 0010 0010 0010 -5 1101 1010 1011
+1 0001 0001 0001 -6 1110 1001 1010
+0 0000 0000 0000 -7 1111 1000 1001
-8 - - 1000

Number Systems and Codes 46


• We can extend the idea of complement
on fractions.
• Examples:
 Negate 0101.01 in 1s-complement
Answer: 1010.10
 Negate 111000.101 in 1s-complement
Answer: 000111.010
 Negate 0101.01 in 2s-complement
Answer: 1010.11

Number Systems and Codes 47


• Algorithm for addition, A + B:
1. Perform binary addition on the two numbers.
2. Ignore the carry out of the MSB.
3. Check for overflow. Overflow occurs if the ‘carry
in’ and ‘carry out’ of the MSB are different, or if
result is opposite sign of A and B.
• Algorithm for subtraction, A – B:
A – B = A + (-B)
1. Take 2s-complement of B.
2. Add the 2s-complement of B to A.

Number Systems and Codes 48


• Signed numbers are of a fixed range.
• If the result of addition/subtraction goes
beyond this range, an overflow occurs.
• Overflow can be easily detected:
 positive add positive  negative
 negative add negative  positive
• Example: 4-bit 2s-complement system
 Range of value: -810 to 710
 01012s + 01102s = 10112s
510 + 610 = -510 ?! (overflow!)
 10012s + 11012s = 101102s (discard end-carry) = 01102s
-710 + -310 = 610 ?! (overflow!)

Number Systems and Codes 49


• Examples: 4-bit system
+3 0011 -2 1110
+ +4 + 0100 + -6 + 1010
---- ------- ---- -------
+7 0111 -8 11000
---- ------- ---- -------
+6 0110 +4 0100
+ -3 + 1101 + -7 + 1001
---- ------- ---- -------
+3 10011 -3 1101
---- ------- ---- -------

• Which of the above is/are overflow(s)?


Number Systems and Codes 50
• Examples: 4-bit system

-3 1101 +5 0101
+ -6 + 1010 + +6 + 0110
---- ------- ---- -------
-9 10111 +11 1011
---- ------- ---- -------

• Which of the above is/are overflow(s)?

Number Systems and Codes 51


• Algorithm for addition, A + B:
1. Perform binary addition on the two numbers.
2. If there is a carry out of the MSB, add 1 to the
result.
3. Check for overflow. Overflow occurs if result is
opposite sign of A and B.
• Algorithm for subtraction, A – B:
A – B = A + (-B)
1. Take 1s-complement of B.
2. Add the 1s-complement of B to A.

Number Systems and Codes 52


• Examples: 4-bit system Any overflow?
+3 0011 +5 0101
+ +4 + 0100 + -5 + 1010
---- ------- ---- -------
+7 0111 -0 1111
---- ------- ---- -------

-2 1101 -3 1100
+ -5 + 1010 + -7 + 1000
---- ------ ---- -------
-7 10111 -10 10100
---- + 1 ---- + 1
------ -------
1000 0101
Number Systems and Codes 53
QUICK REVIEW QUESTIONS (5)
• DLD page 37

2-13. In a 6-bit 2’s complement binary number system, what is the decimal
value represented by (100100)2s?
a. -4 b. 36 c. -36 d. -27 e. -28

2-14. In a 6-bit 1’s complement binary number system, what is the decimal
value represented by (010100)1s?
a. -11 b. 43 c. -43 d. 20 e. -20

2-15. What is the range of values that can be represented in a 5-bit 2’s
complement binary systems?
a. 0 to 31 b. -8 to 7 c. -8 to 8 d. -15 to 15 e. -16 to 15

Number Systems and Codes 54


QUICK REVIEW QUESTIONS (5)
• DLD page 37
2-16. In a 4-bit 2’s complement scheme, what is the result of this operation: (1011)2s
+ (1001)2s?
a. 4 b. 5 c. 20 d. -12 e. overflow

2-17. Assuming a 6-bit 2’s complement system, perform the following subtraction
operation by converting it into addition operation:
a. (011010)2s - (010000)2s
b. (011010)2s - (001101)2s
c. (000011)2s - (010000)2s

2-18. Assuming a 6-bit 1’s complement system, perform the following subtraction
operation by converting it into addition operation:
a. (011111)1s - (010101)1s
b. (001010)1s - (101101)1s
c. (100000)1s - (010011)1s
Number Systems and Codes 55
ADD SUBTRACT Magnitudes
Operation Magnit
udes A>B A<B A=B
(+A) + (+B) + (A + B)
(+A) + (-B) + (A – B ) - (B – A ) + (A – B )
(-A) + (+B) - (A – B ) + (B – A ) + (A – B )
(-A) + (-B) - ( A + B)
(+A) - (+B) + (A – B ) - (B – A ) + (A – B )
(+A) - (-B) + (A + B)
(-A) - (+B) - ( A + B)
(-A) - (-B) - (A – B ) + (B – A ) + (A – B )

Number Systems and Codes 56


Example of adding two +3 0 011
magnitudes when the
result is the sign of + +2 0 010
both operands:
+5 0 101

Example of adding two -3 1 011


magnitudes when the + +2 0 010
result is the sign of the
-( +3 011
larger magnitude:
- +2) 010
- 1 1 001
Number Systems and Codes 57
QUICK REVIEW QUESTIONS (6)

Using 8-bit signed magnitude:


a) 0100 11112 + 0010 00112
b) 0100 11112 + 0110 00112
c) (99)10 – (79)10
d) (-19)10 + (13)10
e) Subtract (-24)10 from (-43)10

Number Systems and Codes 58

You might also like