36
Lecture for Week 9 Lecture for Week 9 2013 Spring 2013 Spring

Lecture for Week 9 2013 Spring. Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Embed Size (px)

Citation preview

Page 1: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Lecture for Week 9Lecture for Week 9

2013 Spring 2013 Spring

Page 2: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday life. The decimal system is an example of a positional number system. Each digit has a place value that depends on its position with respect to the decimal point.

The Binary Number System

Page 3: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Example:2386.75 = 2*103+3*102+8*10+6*100+7*10-1+5*10-2

where 6 represents number of units, 8 number of tens, 3 number of hundreds, 2 number of thousands

To the right of decimal point we have7 number of tenths,5 number of hundredths,and so on.

The Binary Number System

Page 4: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

The decimal system is said to use a base of 10 because the place values are powers of 10. Because we use the decimal system all the time it is easy to forget that there is no mathematical reason for using powers of 10 as the place values. The choice of 10 probably arose because people used their 10 fingers for counting.But in fact we can use any number as the base of a positional number system.A familiar example of non-decimal number system is the subdivision of an hour into 60mins, a minute into 60sec.2 h 26 m 35 s = 2*602 + 26*601 + 35*600

The Binary Number System

Page 5: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

It turns out that in Computing bases 2, 8 and 16 are particularly useful.Base 2 is the binary number systemBase 8 is the octal number systemBase 16 is the hexadecimal number systemThe binary number system is a positional number system that uses 2 as the base.For the purpose of this course we shall be working only with non-negative integers ( Z+{0} ).

The Binary Number System

Page 6: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Example:101102 = 1*24 +0*23 +1*22 +1*21 +0*20

= 16 + 0 + 4 + 2 +0 = 22

Note that subscript 2 represents base 2 and it is necessary to state it unless we work in the decimal number system.

(Expressing negative numbers or real numbers is not really any different)

The Binary Number System

Page 7: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

The table shows the integers from 0 to 20 in their binary representation

The Binary Number System

Binary Decimal Binary Decimal01

1011

100101110111

100010011010

0123456789

10 

 10111100110111101111

1000010001100101001110100

 11121314151617181920

Page 8: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

The binary system uses the 2 binary digits 0 and 1. Every number in binary will appear as strings of zeros and ones.

In octal as strings of 0,1,2,…7In decimal as strings of 0,1,2,…,9 In hexadecimal as strings of 0,1,2,…,9,A,B,C,D,E,F where A represents 10, B represents 11, C represents 12, D represents 13, E represents 14 and F represents 15.

The Binary Number System

Page 9: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Base-10 number system Consist of ten numbers- 0 to 9 Common type of number system used everyday.

Examples- (20)10 , (50)10 , (145)10 , etc.

Decimal number system

Page 10: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

From the table we can see that1. odd numbers end with 12. even numbers end with 0

These two lines can be summarised as follows: 

The last digit in the binary representation is the remainder after dividing the number by 2.

Conversion from Decimal to Binary

Page 11: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

So the first step is to divide (integer division) n by 2 to obtain quotient and remainder (both whole numbers and remainder can only be 0 or 1).

Quotient remainder form

PASCAL notation:

N DIV 2 = quotient (7 DIV 2 = 3)N MOD 2 = remainder. (7 MOD 2 = 1)

Conversion from Decimal to Binary

Page 12: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

In order to proceed further we make another observation  

By removing the rightmost digit in a binary representation we obtain the binary

representation of n DIV 2.

Conversion from Decimal to Binary

Page 13: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Example1:14 = 11102

removing last digit (0) we obtain 111 which in base 10 represents 7.14 DIV 2 = 7

Example211 = 10112

removing last digit (1) we obtain 101 which in base 10 represents 5.11 DIV 2 = 5 ( DIV only returns quotient)

Conversion from Decimal to Binary

Page 14: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Each time we perform division we record remainder until n DIV 2 (quotient) becomes zero. Then we return the string of remainders in reverse order.

Conversion from Decimal to Binary

Page 15: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Convert (250)10 to binary

250/2=125 remainder is 0 125/2=62 remainder is 1 62/2=31 remainder is 0 31/2=15 remainder is 1 15/2=7 remainder is 1 7/2=3 remainder is 1 3/2=1 remainder is 1 1/2=0 remainder is 1 Therefore….(250)10=(11111010)2

Decimal to binary Conversion

Page 16: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Convert 25 into binary.

25 = 12*2+112 = 6*2+06 = 3*2+03 = 1*2+11 = 0*2+1Therefore, 25 = (11001)2

Decimal to binary Conversion

Page 17: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Taking binary number as (11111010)2 0×20 =0 1 ×21 =2 0 ×22 =0 1 ×23 =8 1 ×24 =16 1 ×25 =32 1 ×26 =64 1 ×27 =128 Adding the numbers we get…0+2+0+8+16+32+64+128=250 Therefore….(11111010)2 =(250)10

Binary to decimal Conversion

Page 18: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Convert the following as indicated

1.34 to binary 2.64 to binary 3.123 to binary 4.111000 to decimal 5.10101010 to decimal 6.101110011 to decimal 7.878 to binary

Practice Problem

Page 19: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Adding binary numbers is a very simple task, and very similar to the longhand addition of decimal numbers. As with decimal numbers, you start by adding the bits (digits) one column, or place weight, at a time, from right to left. Unlike decimal addition, there is little to memorize in the way of rules for the addition of binary bits:

Addition and Multiplication in Binary

0 + 0 = 0 1 + 0 = 1 0 + 1 = 1 1 + 1 = 10 1 + 1 + 1 = 11

Page 20: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Just as with decimal addition, when the sum in one column is a two-bit (two-digit) number, the least significant figure is written as part of the total sum and the most significant figure is "carried" to the next left column. Consider the following examples:

Addition and Multiplication in Binary

Page 21: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

The addition problem on the left did not require any bits to be carried, since the sum of bits in each column was either 1 or 0, not 10 or 11. In the other two problems, there definitely were bits to be carried, but the process of addition is still quite simple.

Addition and Multiplication in Binary

Page 22: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Binary multiplication is actually much simpler than decimal multiplication. In the case of decimal multiplication, we need to remember 3 x 9 = 27, 7 x 8 = 56, and so on. In binary multiplication, we only need to remember the following,

Binary multiplication

0 x 0 = 00 x 1 = 01 x 0 = 01 x 1 = 1

Page 23: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Note that since binary operates in base 2, the multiplication rules we need to remember are those that involve 0 and 1 only.

 101   x11First we multiply 101 by 1, which produces 101. Then we put a 0 as a placeholder as we would in decimal multiplication, and multiply 101 by 1, which produces 101.   101  x11  101  1010  <-- the 0 here is the placeholder

Binary multiplication

Page 24: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

The next step, as with decimal multiplication, is to add. The results from our previous step indicates that we must add 101 and 1010, the sum of which is 1111.

  101  x11   101  1010 1111

Binary multiplication

Page 25: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Binary multiplication

Multiply 11010 and 1011

Page 26: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Multiply the following:1.1011 x 112.110 x 1013.111 x 104.1110 x 11015.10110 x 101

Practice Problem

Page 27: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Subtraction ( and division) of binary numbers is performed in the same way as with decimal numbers. Binary subtraction is simplified as well, as long as we remember how subtraction and the base 2 number system.  Let's first look at an easy example.

Subtraction and Division

Page 28: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Since addition is opposite operation of subtraction the table for subtraction can be deduced from the addition table. 0 + 0 = 0 then 0 - 0 = 0;1 + 0 = 1 then 1 - 0 = 1;0 + 1 = 1 then 1 - 1 = 0;1 + 1 = 0 (and 'carry' 1)then 0 - 1 = 1 ( and 'borrow' 1 from the next position ).

Subtraction and Division

Page 29: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Note that the difference is the same if this was decimal subtraction. Also similar to decimal subtraction is the concept of "borrowing." Watch as "borrowing" occurs when a larger digit, say 8, is subtracted from a smaller digit, say 5, as shown below in decimal subtraction.

Subtraction and Division

Page 30: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

For 10 minus 1, 1 is borrowed from the "tens" column for use in the "ones" column, leaving the "tens" column with only 2. The following examples show "borrowing" in binary subtraction.

Subtraction and Division

Page 31: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Binary division is almost as easy, and involves our knowledge of binary multiplication. Take for example the division of 1011 into 11.

Subtraction and Division

Page 32: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

To check our answer, we first multiply our divisor 11 by our quotient 11. Then we add its' product to the remainder 10, and compare it to our dividend of 1011.

The sum is equal to our initial dividend, therefore our solution is correct.

Subtraction and Division

Page 33: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Complements are used for storing numbers in the computer as well as for subtraction. In subtraction instead of calculating A - B we can add A to 1-complement of B plus 1 or by adding 2-complement of B to A.Unfortunately, this method needs a little bit more refining. Before we continue with it we first define complements of a binary number.

One's and Two's Complements

Page 34: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

One's complement of a binary number A is a binary number obtained by subtracting each digit of A from 1.

Alternatively, one's complement of a binary number A is a binary number such that:Each digit of A is changed into its opposite (i.e. 1 into 0, 0 into 1)

One's and Two's Complements

Page 35: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Example 1's complement of 110101 is 001010Two's complement of a binary number A is a binary number obtained by adding 1 to one's complement of A.

ExampleFrom the above exampleA is 110101 Then 1's compl. is 0010102's compl. is 001010 + 1 ----------- 001011

One's and Two's Complements

Page 36: Lecture for Week 9 2013 Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday

Any questions