Binary / Hex Binary and Hex The number systems of Computer Science
Main Memory Memory cells have two states: On Off Work best with two-state number system --- Binary number system Investigate Decimal number system first
The Decimal Numbering System The decimal number system is a positional number system: the same digit will have different values in different positions. Examples: 5 “five” = 5 * 1 50 “fifty” = 5 * 10 500 “five-hundred” = 5*100
The Decimal Numbering System Combinations yield any integer. Example: 5 6 2 1 1 X 100 1000 100 10 1 2 X 101 6 X 102 5 X 103
What is the base ? The decimal numbering system is also known as base 10. The values of the positions are calculated by taking 10 to some power. The common number system is a decimal or base 10 system because we use 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
(Can you feel the tension building?) What is the base ? The binary numbering system is called binary because it uses base 2. The values of the positions are calculated by taking 2 to some power. The Binary system uses just two digits: 0 and 1 A memory cell has just two states… (Can you feel the tension building?)
The Binary Numbering System The Binary Numbering System is also a positional numbering system. Instead of ten digits, 0 - 9, the binary system uses only two digits, 0 and 1. Example of a binary number & the values of the positions. 1 0 0 0 0 0 1 ( = 65) 26 25 24 23 22 21 20
Computing the Decimal Values of Binary Numbers 1 0 0 0 0 0 1 1 X 20 = 1 26 25 24 23 22 21 20 0 X 21 = 0 0 X 22 = 0 20 = 1 24 = 16 0 X 23 = 0 21 = 2 25 = 32 0 X 24 = 0 22 = 4 26 = 64 0 X 25 = 0 23 = 8 1 X 26 = 64 65
Converting Decimal to Binary First make a list of the values of 2 to the powers of 0 to 8, then use the subtraction method. 20 = 1, 21 = 2, 22 = 4, 23 = 8, 24 = 16, 25 = 32, 26 = 64, 27 = 128, 28 = 256 Example: 42 42 10 2 - 32 - 8 - 2 1 0 1 0 1 0 25 24 23 22 21 20
Counting in Binary Binary 1 10 11 100 101 110 111 Decimal equivalent 1 1 10 11 100 101 110 111 Decimal equivalent 1 2 3 4 5 7 8
Addition of Binary Numbers Examples: 1 0 0 1 0 0 0 1 1 1 0 0 + 0 1 1 0 + 1 0 0 1 + 0 1 0 1 1 1 1 1 1 0 1 0 1 0 0 0 1
Addition of Large Binary Numbers 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
Working with large numbers 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 Humans can’t work well with binary numbers. We will make errors. 110010111001101010 * 110010100101 Hexadecimal: Shorthand for binary
Hexadecimal Binary 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 Hex 5 0 9 7 Written: 509716
The Amazing Secret of Hexadecimal Notation! Binary 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 Hex 5 0 9 7 The amazing secret: 16 = 28
Hexadecimal Binary is base 2 two digits: 0, 1 Decimal is base 10 ten digits: 0, 1, 3, 4, 5, 6, 7, 8, 9. Hexadecimal (hex) is base 16. How many digits do we need to express numbers in hex? 16: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Counting in Hex Binary Hex Binary Hex 0 0 0 0 0 1 0 0 0 8 0 0 0 0 0 1 0 0 0 8 0 0 0 1 1 1 0 0 1 9 0 0 1 0 2 1 0 1 0 A 0 0 1 1 3 1 0 1 1 B 0 1 0 0 4 1 1 0 0 C 0 1 0 1 5 1 1 0 1 D 0 1 1 0 6 1 1 1 0 E 0 1 1 1 7 1 1 1 1 F
Another Binary to Hex Conversion Hex 7 C 3 F 7C3F16
Storing Negative Numbers in Binary Since we need to store negative numbers as well as positive numbers in binary, special variations on binary are used. 2’s Complement notation is used for integers.
Building the 2’s Complement of a Binary Number Start with a binary number x = 1001 0101 Replace 1 by 0, replace each 0 by 1 x1 = 0110 1010 (“one’s complement”) Add 1 to the least significant digit x2 = 0110 1011 (“two’s complement”)
Why Two’s Complement? Adding the two’s complement of a number is like subtracting the number x+x2 = 1001 0101 + 0110 1011 1001 0101 0110 1011 (1) 0000 0000 where the leading 1 is in parentheses because it will be ignored. Replaces Subtraction by Addition
Review The decimal, binary, and hexadecimal number systems are positional systems The decimal number system has 10 digits, the binary system two, the hexadecimal system has 16 The binary system is good system to use for two state memory devices.
Review The hexadecimal system (“hex”) needs 16 digits, so it uses 0-9,A,B,C,D,E,F The hexadecimal system is a often a convenient shorthand for the binary system: 4-bits to one hex digit Negative numbers are stored using “two’s complement” which turns subtraction into addition.