Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 104, Lecture 051 Binary / Hex Binary and Hex The number systems of Computer Science.

Similar presentations


Presentation on theme: "CMSC 104, Lecture 051 Binary / Hex Binary and Hex The number systems of Computer Science."— Presentation transcript:

1 CMSC 104, Lecture 051 Binary / Hex Binary and Hex The number systems of Computer Science

2 CMSC 104, Lecture 052 Number Systems in Main Memory l The on and off states of the capacitors in RAM can be thought of as the values 1 and 0, respectively. l Therefore, thinking about how information is stored in RAM requires knowledge of the binary (base 2) number system. l Let’s review the decimal (base 10) number system first.

3 CMSC 104, Lecture 053 The Decimal Numbering System l The decimal numbering system is a positional number system. l Example: 5 6 2 11 X 10 0 = 1 10 3 10 2 10 1 10 0 2 X 10 1 = 20 6 X 10 2 = 600 5 X 10 3 = 5000

4 CMSC 104, Lecture 054 What is the base ? l The decimal numbering system is also known as base 10. The values of the positions are calculated by taking 10 to some power. l Why is the base 10 for decimal numbers ? Because we use 10 digits: the digits 0 through 9.

5 CMSC 104, Lecture 055 The Binary Number System l The binary numbering system is also known as base 2. The values of the positions are calculated by taking 2 to some power. l Why is the base 2 for binary numbers ? Because we use 2 digits: the digits 0 and 1.

6 CMSC 104, Lecture 056 The Binary Number System (con’t) l The Binary Number System is also a positional numbering system. l Instead of using ten digits, 0 - 9, the binary system uses only two digits, 0 and 1. l Example of a binary number and the values of the positions: 1 0 0 0 0 0 1 2 6 2 5 2 4 2 3 2 2 2 1 2 0

7 CMSC 104, Lecture 057 Converting from Binary to Decimal 1 0 0 1 1 0 11 X 2 0 = 1 2 6 2 5 2 4 2 3 2 2 2 1 2 0 0 X 2 1 = 0 1 X 2 2 = 4 2 0 = 1 2 4 = 16 1 X 2 3 = 8 2 1 = 2 2 5 = 32 0 X 2 4 = 0 2 2 = 4 2 6 = 64 0 X 2 5 = 0 2 3 = 81 X 2 6 = 64 77

8 CMSC 104, Lecture 058 Converting from Binary to Decimal (con’t) Practice conversions: Binary Decimal 11101 1010101 100111

9 CMSC 104, Lecture 059 Converting Decimal to Binary First make a list of the values of 2 to the powers of 0 to the number being converted - e.g: 2 0 = 1, 2 2 = 4, 2 3 = 8, 2 4 = 16, 2 5 = 32, 2 6 = 64,… Perform successive divisions by 2, placing the remainder of 0 or 1 in each of the positions from right to left. Continue until the quotient is zero; e.g.: 42 10 2 5 2 4 2 3 2 2 2 1 2 0 32 16 8 4 2 1 1 0 1 0 1 0 alternative method: “subtract maximum powers and repeat”. E.g.: 42 - 32 = 10 - 8 = 2 - 2 = 0 2 5 2 4 2 3 2 2 2 1 2 1 0 1 0 1 0

10 CMSC 104, Lecture 0510 Converting From Decimal to Binary (con’t) Practice conversions: Decimal Binary 59 82 175

11 CMSC 104, Lecture 0511 Counting in Binary l Binary 0 1 10 11 100 101 110 111 l Decimal equivalent 0 1 2 3 4 5 6 7

12 CMSC 104, Lecture 0512 Addition of Binary Numbers l Examples: 1 0 0 10 0 0 11 1 0 0 + 0 1 1 0 + 1 0 0 1 + 0 1 0 1 1 1 1 11 0 1 0 1 0 0 0 1

13 CMSC 104, Lecture 0513 Addition of Large Binary Numbers l Example showing larger numbers: 1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 1 + 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0

14 CMSC 104, Lecture 0514 Working with large numbers 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 l Humans can’t work well with binary numbers - there are too many digits to deal with. We will make errors. l Memory addresses and other data can be quite large. Therefore, we sometimes use the hexadecimal number system (shorthand for binary that’s easier for us to work with)

15 CMSC 104, Lecture 0515 The Hexadecimal Number System l The hexadecimal number system is also known as base 16. The values of the positions are calculated by taking 16 to some power. l Why is the base 16 for hexadecimal numbers ? oBecause we use 16 symbols, the digits 0 and 1 and the letters A through F.

16 CMSC 104, Lecture 0516 The Hexadecimal Number System (con’t) Binary Decimal Hexadecimal 0 0 0 1010 10 A 1 1 1 1011 11 B 10 2 2 1100 12 C 11 3 3 1101 13 D 100 4 4 1110 14 E 101 5 5 1111 15 F 110 6 6 111 7 7 1000 8 8 1001 9 9

17 CMSC 104, Lecture 0517 The Hexadecimal Number System (con’t) l Example of a hexadecimal number and the values of the positions: 3 C 8 B 0 5 1 16 6 16 5 16 4 16 3 16 2 16 1 16 0

18 CMSC 104, Lecture 0518 Binary to Hexadecimal Conversion Binary 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 Hex 5 0 9 7 Written: 5097 16

19 CMSC 104, Lecture 0519 What is Hexadecimal really ? Binary 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 Hex 5 0 9 7 A number expressed in base 16. It’s easy to convert binary to hex and hex to binary because 16 is 2 4.

20 CMSC 104, Lecture 0520 Another Binary to Hex Conversion Binary 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 Hex 7 C 3 F 7C3F 16

21 CMSC 104, Lecture 0521 Summary of Number Systems l Binary is base 2, because we use two digits, 0 and 1 l Decimal is base 10, because we use ten digits, 0 through 9. l Hexadecimal is base 16. How many digits do we need to express numbers in hex ? 16 (0 through F: 0 1 2 3 4 5 6 7 8 9 A B C D E F)

22 CMSC 104, Lecture 0522 Example of Equivalent Numbers Binary: 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 2 Decimal: 20647 10 Hexadecimal: 50A7 16 Notice how the number of digits gets smaller as the base increases.


Download ppt "CMSC 104, Lecture 051 Binary / Hex Binary and Hex The number systems of Computer Science."

Similar presentations


Ads by Google