1 week 2: binary, octal and hexadecimal numbers reading: chapter 2

23
1

Upload: myron-bryce-spencer

Post on 02-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1

Page 2: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

22

Positional Notation:BaseQuick Re-cap:

•1. Conversion from other bases to base 10, where b is the base of the original number, di is the digit at the i-th position :

• 2. Conversion from base 10 to other bases

EECS 1520 -- Computer Use: Fundamentals

dn-1 * bn-1 + … + d2 * b2 + d1 * b1 + d0 * b0

Step 1: Always divide the decimal number by the new base, write down the quotient and the reminder

Step 2: Divide the quotient by the new base, write down the new quotient and the new reminder

Step 3: Repeat step 2 until the quotient is 0

Page 3: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

33

Positional Notation:Base• In general, we can convert numbers in any bases with the following method:

EECS 1520 -- Computer Use: Fundamentals

(number)A (number)10 (number)B

where A ≠ B ≠ 10

Page 4: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

44

Positional Notation:Adding binary numbers• Basic idea in adding digits in decimal: 1 + 2 = 3, 4 + 1 = 5….

EECS 1520 -- Computer Use: Fundamentals

• When you try to add two numbers whose sum is equal to or greater than the base value (i.e. 10), we reuse the same digits and rely on position.

• The rules of adding binary numbers are similar, but we run out of digits much faster (since we only have 2 digits)

0+ 1 1

1+ 0 1

0+ 0 0

1+ 1 1 0

9+ 1 1 0

1 is carried into the next position to the left

1 is called the “carry bit”

Page 5: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

55

Positional Notation:Adding binary numbers• Example 1: what is the sum of the following two binary numbers?

EECS 1520 -- Computer Use: Fundamentals

• We can confirm that the above sum is correct by converting each of the above binary numbers to their decimal representations

0011+ 1001 1100

Page 6: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

66

Positional Notation:Adding binary numbers

EECS 1520 -- Computer Use: Fundamentals

0011

1001

convert to base 10

convert to base 10

1*21 + 1*20 = 3

1100 convert to base 10

1*23 + 1*20 = 9

1*23 + 1*22 = 12

Page 7: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

77

Positional Notation:Adding binary numbers• Example 2: what is the sum of the following two binary numbers?

EECS 1520 -- Computer Use: Fundamentals

0111+ 1101 10100

0111

1101

convert to base 10

convert to base 10

1*22 + 1*21 + 1*20 = 4+2+1 = 7

10100 convert to base 10

1*23 + 1*22 + 1*20 = 8+4+1 = 13

1*24 + 1*22 = 16+4 = 20

Page 8: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

88

Positional Notation:Power-of-2 number systems

EECS 1520 -- Computer Use: Fundamentals

Decimal (base 10) Binary (base 2) Octal (base 8)

0 0000 0

1 0001 1

2 0010 2

3 0011 3

4 0100 4

5 0101 5

6 0110 6

7 0111 7

8 1000 10

9 1001 11

10 1010 12

Page 9: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

99

Positional Notation:Power-of-2 number systems• An unique relationship between binary and octal numbers, as 8 is a power of

2 (i.e. 8 = 23)

• Given an octal number, you can convert it directly to binary by replacing each digit in the octal number with the binary representation of that digit (note: the largest digit can only be “7”)

EECS 1520 -- Computer Use: Fundamentals

Decimal Binary Octal

0 000 0

1 001 1

2 010 2

3 011 3

4 100 4

5 101 5

6 110 6

7 111 7

Page 10: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1010

Positional Notation:Power-of-2 number systems

EECS 1520 -- Computer Use: Fundamentals

• Example: what is the binary representation of the octal “154” ?

1 5 4

100101001binary

octal

Page 11: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1111

Positional Notation:Power-of-2 number systems• Now, if we convert the binary number to base 10:

EECS 1520 -- Computer Use: Fundamentals

• We got 108, same as the number we got when we converted the octal number “154” to its base 10 representation:

dn-1 * bn-1 + … + d2 * b2 + d1 * b1 + d0 * b0

1 * 82 + 5 * 81 + 4 * 80

= 64 + 40 + 4 = 108

001 101 100 1 * 26 + 1 * 25 + 1* 23 + 1 * 22

= 64 + 32 + 8 + 4 = 64 + 44 = 108

Page 12: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1212

Positional Notation:Power-of-2 number systems• Convert from binary number to octal number:

EECS 1520 -- Computer Use: Fundamentals

Step 1: start at the rightmost binary digit and mark the digits in groups of threesStep 2: convert each group of three into its octal value

Note: Add 0s to the front if the leftmost group does not have 3 binary digits

• Example: what is the octal representation of the binary number: 1101111110 ?

• Note: any digits > 7 do not exist in octal, if you get an octal value > 7, something is wrong.

001 101 111 110

1 5 7 6

binary

octal

Page 13: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1313

Positional Notation:Power-of-2 number systems

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

• Recall that, in base-16, we have:

EECS 1520 -- Computer Use: Fundamentals

• The previous “grouping” method can be applied to convert a binary number to its hexadecimal number (since 16 = 24)

0000, ……………………………………….., 1111 binary

hexadecimal

Page 14: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1414

Positional Notation:Power-of-2 number systems• Convert from binary number to hexadecimal number:

EECS 1520 -- Computer Use: Fundamentals

Step 1: start at the rightmost binary digit and mark the digits in groups of foursStep 2: convert each group of four into its hexadecimal value

Note: Add 0s to the front if the leftmost group does not have 4 binary digits

• Example: what is the octal representation of the binary number: 1101111110 ?

0011 0111 1110

3 7 E

binary

hexadecimal

Page 15: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1515

Positional Notation:Power-of-2 number systems• Conversion between octal and hexadecimal numbers are much easier now

as we can utilize the intermediate binary digits “grouping” method

EECS 1520 -- Computer Use: Fundamentals

• Example: what is the hexadecimal representation of the octal number: 4567 ?

100 101 110 111

4 5 6 7 octal

binary

1001 0111 0111

hexadecimal 9 7 7

Page 16: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1616

Positional Notation:Adding octal numbers

EECS 1520 -- Computer Use: Fundamentals

• When you try to add two numbers whose sum is equal to or greater than the base value (i.e. 8), we reuse the same digits and rely on position.

(7)8

+ (1)8

(1 0)8

1 is carried into the next position to the left

• Example 1: what is the sum of the following two octal numbers?

(27)8

+ (35)8

(64)8

Page 17: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1717

Positional Notation:Adding octal numbers

EECS 1520 -- Computer Use: Fundamentals

(27)8

(35)8

convert to base 10

convert to base 10

2*81 + 7*80 = 16+7 = 23

(64)8 convert to base 10

3*81 + 5*80 = 24+5 = 29

6*81 + 4*80 = 48+4 = 52

Page 18: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1818

Positional Notation:Adding hexadecimal numbers

EECS 1520 -- Computer Use: Fundamentals

• Same concept is applied to adding two hexadecimal numbers.

• When you try to add two hex numbers whose sum is equal to or greater than the base value (i.e. 16), we reuse the same digits and rely on position.

(F)16

+ (1)16

(1 0)16

1 is carried into the next position to the left

(F)16

+ (2)16

(1 1)16

(F)16

+ (3)16

(1 2)16

(F)16

+ (4)16

(1 3)16

and so on…..

Page 19: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

1919

Positional Notation:Adding hexadecimal numbers

EECS 1520 -- Computer Use: Fundamentals

• Example: (2F)16

+ (73)16

(A2)16

(2F)16

(73)16

convert to base 10

convert to base 10

2*161 + 15*160 = 32+15 = 47

(A2)16 convert to base 10

7*161 + 3*160 = 112+3 = 115

10*161 + 2*160 = 160+2 = 162

Page 20: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

2020

Positional Notation:Subtracting two base 10 numbers

EECS 1520 -- Computer Use: Fundamentals

• When you try to subtract a larger digit from a smaller one, we have to “borrow one” from the next left digit of the number from which you are subtracting. More precisely, we borrow one power of the base, such as in base 10, we borrow 10.

52- 38 1 4 Borrow “10” from 5, so

we have 10- 8 + 2 = 4

• Example:

Page 21: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

2121

Positional Notation:Subtracting two binary numbers

EECS 1520 -- Computer Use: Fundamentals

• In base 2, when borrow one power of the base to do subtraction, we borrow 2. In fact, we only need to borrow a 2 when we subtract 0 from 1.

1101- 0110

• Example:

0111

(1101)2

(0110)2

convert to base 10

convert to base 10

13

(0111)2 convert to base 10

6

7

Page 22: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

2222

Positional Notation:Subtracting two octal numbers

EECS 1520 -- Computer Use: Fundamentals

• In base 8, when borrow one power of the base to do subtraction, we borrow 8.

(153)8

- (67)8

• Example:

(64)8

(153)8

(67)8

convert to base 10

convert to base 10

107

(64)8 convert to base 10

55

52

Page 23: 1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2

2323

Positional Notation:Subtracting two hex numbers

EECS 1520 -- Computer Use: Fundamentals

• In base 16, when borrow one power of the base to do subtraction, we borrow 16.

(153)16

- (67)16

• Example:

(EC)16

(153)16

(67)16

convert to base 10

convert to base 10

339

(EC)16 convert to base 10

103

236