Binary Representation Number Systems Positional Notation Base
Total Page:16
File Type:pdf, Size:1020Kb
Binary Representation Number Systems • The basis of all digital data is binary representation. • To talk about binary data, we must first talk about • Binary - means ‘two’ number systems –1, 0 –True, False • The decimal number system (base 10) you should be familiar with! –Hot, Cold – A digit in base 10 ranges from 0 to 9. –On, Off – A digit in base 2 ranges from 0 to 1 (binary number • We must be able to handle more than just values for system). A digit in base 2 is also called a ‘bit’. real world problems – A digit in base R can range from 0 to R-1 – 1, 0, 56 – A digit in Base 16 can range from 0 to 16-1 – True, False, Maybe (0,1,2,3,4,5,5,6,7,8,9,A,B,C,D,E,F). Use letters A-F to – Hot, Cold, LukeWarm, Cool represent values 10 to 15. Base 16 is also called – On, Off, Leaky Hexadecimal or just ‘Hex’. V 0.1 1 V 0.1 2 Positional Notation Base 10, Base 2, Base 16 Value of number is determined by multiplying each digit by a The textbook uses subscripts to represent different weight and then summing. The weight of each digit is a bases (ie. A2F16 , 953.7810, 1011.112 ) POWER of the BASE and is determined by position. I will use special symbols to represent the different bases. The default base will be decimal, no special symbol for 953.78 = 9 x 102 + 5 x 101 + 3 x 100 + 7 x 10-1 + 8 x 10-2 base 10. = 900 + 50 + 3 + .7 + .08 = 953.78 The ‘$’ will be used for base 16 ( $A2F) Will also use ‘h’ at end of number (A2Fh) % 1011.11 = 1x23 + 0x22 + 1x21 + 1x20 + 1x2-1 + 1x2-2 The ‘%’ will be used for base 2 (%10101111) = 8 + 0 + 2 + 1 + 0.5 + 0.25 = 11.75 If ALL numbers on a page are the same base (ie, all in base $ A2F = 10x162 + 2x161 + 15x160 16 or base 2 or whatever) then no symbols will be used and = 10 x 256 + 2 x 16 + 15 x 1 a statement will be present that will state the base (ie, all = 2560 + 32 + 15 = 2607 numbers on this page are in base 16). V 0.1 3 V 0.1 4 Common Powers Conversion of Any Base to Decimal 2-3 = 0.125 Converting from ANY base to decimal is done by multiplying 0 0 2-2 = 0.25 16 = 1 = 2 each digit by its weight and summing. 2-1 = 0.5 161 = 16 = 24 20 = 1 162 = 256 = 28 1 2 = 2 163 = 4096 = 212 Binary to Decimal 22 = 4 23 = 8 % 1011.11 = 1x23 + 0x22 + 1x21 + 1x20 + 1x2-1 + 1x2-2 24 = 16 = 8 + 0 + 2 + 1 + 0.5 + 0.25 25 =32 26 = 64 = 11.75 27 = 128 210 = 1024 = 1 K 8 2 = 256 220 = 1048576 = 1 M (1 Megabits) = 1024 K = 210 x 210 Hex to Decimal 29 = 512 230 = 1073741824 = 1 G (1 Gigabits) 210 = 1024 A2Fh = 10x162 + 2x161 + 15x160 211 = 2048 = 10 x 256 + 2 x 16 + 15 x 1 212 = 4096 = 2560 + 32 + 15 = 2607 V 0.1 5 V 0.1 6 1 Conversion of Decimal Integer To ANY Base Least Significant Digit Divide Number N by base R until quotient is 0. Remainder at Most Significant Digit EACH step is a digit in base R, from Least Significant digit to Most significant digit. Convert 53 to binary 53 = % 110101 53/2 = 26, rem = 1 Least Significant Digit 26/2 = 13, rem = 0 13/2 = 6 , rem = 1 6 /2 = 3, rem = 0 Most Significant Digit Least Significant Digit 5 3/2 = 1, rem = 1 (has weight of 2 or (has weight of 20 or 1). 1/2 = 0, rem = 1 Most Significant Digit 32). For base 2, also For base 2, also called called Most Significant Least Significant Bit 53 = % 110101 Bit (MSB). Always (LSB). Always = 1x25 + 1x24 + 0x23 + 1x22 + 0x21 + 1x20 LEFTMOST digit. RIGHTMOST digit. = 32 + 16 + 0 + 4 + 0 + 1 = 53 V 0.1 7 V 0.1 8 More Conversions Hex (base 16) to Binary Conversion Convert 53 to Hex Each Hex digit represents 4 bits. To convert a Hex number to Binary, simply convert each Hex digit to its four bit value. 53/16 = 3, rem = 5 Hex Digits to binary: 3 /16 = 0 , rem = 3 Hex Digits to binary (cont): $ 0 = % 0000 $ 9 = % 1001 53 = 35h $ 1 = % 0001 $ A = % 1010 1 0 $2 = % 0010 = 3 x 16 + 5 x 16 $ B = % 1011 = 48 + 5 = 53 $3 = % 0011 $ C = % 1100 $4 = % 0100 $ D = % 1101 $5 = % 0101 $ E = % 1110 $6 = % 0110 $ F = % 1111 $7 = % 0111 $8 = % 1000 V 0.1 9 V 0.1 10 A Trick! Hex to Binary, Binary to Hex If faced with a large binary number that has to be converted to decimal, I first convert the binary number A2Fh = % 1010 0010 1111 to HEX, then convert the HEX to decimal. Less work! 345h = % 0011 0100 0101 % 110111110011 = % 1101 1111 0011 = D F 3 Binary to Hex is just the opposite, create groups of 4 bits = 13 x 162 + 15 x 161 + 3x160 starting with least significant bits. If last group does not = 13 x 256 + 15 x 16 + 3 x 1 have 4 bits, then pad with zeros for unsigned numbers. = 3328 + 240 + 3 = 3571 % 1010001 = % 0101 0001 = 51h Of course, you can also use the binary, hex conversion feature on your calculator. Too bad calculators won’t be allowed on Padded with a zero the first test, though…... V 0.1 11 V 0.1 12 2 Binary Numbers Again Binary Arithmetic, Subtraction The rules for binary arithmetic The rules for binary subtraction Recall than N binary digits (N bits) can represent unsigned are: are: integers from 0 to 2N-1. 0 + 0 = 0, carry = 0 0 - 0 = 0, borrow = 0 4 bits = 0 to 15 1 + 0 = 1, carry = 0 1 - 0 = 1, borrow = 0 8 bits = 0 to 255 16 bits = 0 to 65535 0 + 1 = 1, carry = 0 0 - 1 = 1, borrow = 1 Besides simply representation, we would like to also do 1 + 1 = 0, carry = 1 1 - 1 = 0, borrow = 0 arithmetic operations on numbers in binary form. Principle operations are addition and subtraction. Borrows, Carries from digits to left of current of digit. Binary subtraction, addition works just the same as decimal addition, subtraction. V 0.1 13 V 0.1 14 Subtraction Binary, Decimal addition Decimal Binary Binary Decimal 900 % 100 % 101011 34 - 001 - % 001 + % 000001 ------- ------- + 17 --------------- 899 011 ------ 101100 51 From LSB to MSB: 0-1 = 9; with borrow of 1 0-1 = 1; with borrow of 1 from LSD to MSD: 1+1 = 0, carry of 1 from next column from next column 7+4 = 1; with carry out of 1 1 (carry)+1+0 = 0, carry of 1 0 -1 (borrow) - 0 = 9, with 0 -1 (borrow) - 0 = 1, with to next column 1 (carry)+0 + 0 = 1, no carry borrow of 1 borrow of 1 1 +0 = 1 9 - 1 (borrow) - 0 = 8. 0 + 0 = 0 1 - 1 (borrow) - 0 = 0. 1 (carry) + 3 + 1 = 5. Answer = 899. Answer = % 011. 1 + 0 = 1 answer = 51. answer = % 101100 V 0.1 15 V 0.1 16 Hex Addition Hex addition again Why is Ah + 8h = 2 with a carry out of 1? Decimal check. 3Ah The carry out has a weight equal to the BASE (in this case 3Ah = 3 x 16 + 10 16). The digit that gets left is the excess (BASE - sum). + 28h = 58 -------- 28h = 2 x 16 + 8 Ah + 8h = 10 + 8 = 18. 62h = 40 58 + 40 = 98 18 is GREATER than 16 (BASE), so need a carry out! A+8 = 2; with carry out of 1 to next column Excess is 18 - BASE = 18 - 16 = 2, so ‘2’ is digit. 62h = 6 x 16 + 2 Exactly the same thing happens in Decimal. = 96 + 2 = 98!! 1 (carry) + 3 + 2 = 6. 5 + 7 = 2, carry of 1. answer = $ 62. 5 + 7 = 12, this is greater than 10!. So excess is 12 - 10 = 2, carry of 1. V 0.1 17 V 0.1 18 3 Hex Subtraction Hex subtraction again Decimal check. 34h Why is 4h – 7h = $D with a borrow of 1? 34h = 3 x 16 + 4 - 27h = 52 The borrow has a weight equal to the BASE (in this case -------- 27h = 2 x 16 + 7 16). 0Dh = 39 52 - 39 = 13 BORROW +4h – 7h = 16 + 4 -7 = 20 -7 = 13 = Dh. 4-7 = D; with borrow of 1 from next column Dh is the result of the subtraction with the borrow. 0Dh = 13 !! Exactly the same thing happens in decimal. 3 - 1 (borrow) - 2 = 0. 3 - 8 = 5 with borrow of 1 answer = $ 0D. borrow + 3 - 8 = 10 + 3 - 8 = 13 - 8 = 5. V 0.1 19 V 0.1 20 Fixed Precision Unsigned Overflow With paper and pencil, I can write a number with as many digits as I want: In this class I will use 8 bit precision most of the time, 16 bit 1,027,80,032,034,532,002,391,030,300,209,399,302,992,092,920 occassionally. A microprocessor or computing system usually uses FIXED Overflow occurs when I add or subtract two numbers, and the PRECISION for integers; they limit the numbers to a fixed correct result is a number that is outside of the range of number of bits: allowable numbers for that precision. I can have both $ AF4500239DEFA231 64 bit number, 16 hex digits unsigned and signed overflow (more on signed numbers later) $ 9DEFA231 32 bit number, 8 hex digits $ A231 16 bit number, 4 hex digits 8 bits -- unsigned integers 0 to 28 -1 or 0 to 255.