1.02k likes | 2.12k Views
Binary Number System. Why Study? Necessary for: Digital Design Design Debug and Verification Troubleshooting Circuits Lab and field instruments Its cousin, Boolean Algebra. Number Systems and Codes. Decimal or Human Number System Binary Number System Hexadecimal Number System
E N D
Binary Number System • Why Study? • Necessary for: • Digital Design • Design Debug and Verification • Troubleshooting Circuits • Lab and field instruments • Its cousin, Boolean Algebra
Number Systems and Codes • Decimal or Human Number System • Binary Number System • Hexadecimal Number System • Octal Number System
Decimal Number System • Ten unique numbers 0,1..9 • Combination of digits • Positional Number System • 275 = 2 x 102 + 7 x 101 + 5 x 100 • Base or Radix 10 • Weight: powers of 10 1, 10, 100, 1000 ….
Representing Fractions • Fractions can be represented in decimal number system in a similar manner = 3 x 102 + 8 x 101 + 2 x 100 + 9 x 10-1 + 1 x 10-2 = 300 + 80 + 2 + 0.9 + 0.01 = 382.91
Binary Number System • Two unique numbers 0 and 1 • Base – 2 • A binary digit is a bit • Combination of bits to represent larger values • In computer work, some special titles: 210 – K (kilo) 220 – M (mega) 230 – G (giga)
Combination of Binary Bits • Combination of Bits • 100112 = 1910 = (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (1 x 20) = (1 x 16) + (0 x 8) + (0 x 4) + (1 x 2) + (1 x 1) = 16 + 0 + 0 + 2 + 1 = 1910
Fractions in Binary • Fractions in Binary • 1011.1012 = 11.625 = (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) + (1 x 2-1) + (0 x 2-2) + (1 x 2-3) = (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1) + (1 x 1/2) + (0 x 1/4) + (1 x 1/8) = 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125 = 11.62510 • Floating Point Notations (to be discussed later)
Binary to Decimal Conversion • Sum-of-Weights • Add weights for non-zero terms • Drop zero terms
Binary to Decimal Conversion • Example 1 • 101012 = (1 x 24) + (0 x 23) + (1 x 22) + (0 x 21) + (1 x 20) = 24 + 22 + 20 = 16 + 4 + 1 = 2110
Binary to Decimal Conversion • Example 2 • 1010.1012 = 8 + 2 + 1/2 + 1/8 = 10 + 5/8 = 10.62510
Decimal to Binary Conversion • Sum-of-Weights method used in reverse • Highest binary weight less than the decimal number • Subsequent smaller weights that add up to decimal number • Repeated division by 2 • Systematic • Works for numbers of any length
Decimal-Binary Fraction Conversion • Repeated multiplication by 2 STOP when 0 reached
Binary Arithmetic • Binary Addition • Binary Subtraction • Binary Multiplication • Binary Division
Binary Addition • Four Basic Rules
Binary Subtraction • Four Basic Rules
Binary Multiplication • Four Basic rules
Binary Multiplication Example 1101 (13d) x 101 (5d) 1st partial product 1101 2nd partial product 0000 3rd partial product + 1101 Product 1000001 (65d)
Binary Division Example • Same rules as decimal division 10 2 101 | 1101 5 | 13 101 10 011 000___ 11 3
Division/Multiplication by Shifting • In Decimal a number shifted one place to the right is the same as divide by 10. e.g. 29 2.9 • Likewise, a number shifted left is multiply by 10. e.g. 13 130 • In Binary each right or left shift accomplishes divide or multiply by 2. • Right sh: 111012 (29d) 1110.12 (14.5d) • Left sh: 1101 (13d) -> 11010 (26d) 110100 (52d )
Negative Number Representation • 3 Options • Sign-magnitude • One’s Complement • Two’s Complement used in computers and most digital systems
Sign-magnitude Representation • Signed Binary Numbers • Most significant bit represents sign • MSB of 0 => positive number; MSB of 1 => negative number • Problem • Representing 0 (e.g. +0 0000 and -0 1000) • Not computation friendly Sign Magnitude Form 1101 -5 0010 +2 1111 -7!!
1’s & 2’s Complement Form Representation • 1’s complement form • Derived by inverting all bits • 2’s complement form • Derived by adding 1 to 1’s complement Binary number 00001101 (13d) 1’s complement 11110010 (-13d) + 1 2’s complement 11110011 (-13d)
Signed Number Example • +92 01011100 • In sign-magnitude form -92 11011100 • In 1’s complement form -92 10100011 • In 2’s complement form -92 10100100 Shortcut to form 2’s complement: • Do not flip any bits to the right of and including the first “1” • Flip all bits to the left of the first “1”
Decimal Value of Signed Numbers • Sign-magnitude form • Sum the weights in all magnitude bit positions where there are 1s • Sign is determined by examination of the sign bit 110111 - (16 + 4 + 2 + 1) -23d • 1’s Complement form • Assign a negative value to the weight of the sign bit • Sum the weights in all bit positions, where there are 1s • Add 1 to the result 100110 (-32 +4 +2) +1 -25d • 2’s Complement form • Assign a negative value to the weight of sign bit • Sum the weights in all bit positions, where there are 1s 110111 (-32 +16 + 4 +2 + 1) -9d
Decimal Value of Signed Numbers Alternate Method • 1’s Complement form • Convert Negative Number to Positive • Attach –ve sign bit 100110 => 011001 (-ve) 25d • 2’s Complement form • Convert Negative Number to Positive • Attach –ve sign bit 110111 => 001001 (-ve) 9d
Range of Binary Numbers • Unsigned Numbers Total combos for “n” binary bits = 2n Range: 0 … + (2n -1) • Sign Magnitude Form - (2n-1 -1)… + (2n-1 -1) • 2’s Complement Form - (2n-1)… + (2n-1 -1)
Important Info about 2’s Comp #s • 2’s complement representation is for negative numbers only • Before representing 2’s complement numbers, ensure it can fit in the given range of bits - e.g for n-bit numbers range is: - (2n-1)… + (2n-1 -1) • Overflow occurs only when MSB changes contrary to result expectation, otherwise discard carry.
Arithmetic Operations w/ Signed Numbers • Addition • straightforward as we shall see • Subtraction • Change sign of Subtrahend and Add • Multiplication • Shift and add each partial product term • Division • Repetitively subtract Divisor from Dividend – but subtraction is again addition
Addition with 2’s Complement 1011 -5 0101 +5 0010 +21110 -2 1101 -3 0011 +3 1011 -5 0101 +5 1110 -20010 +2 1 1001 -7 0111 +7 Discard any final carry bit
Subtraction with 2’s Complement 01011001 89 01011001 89 - 00101101 45 + 11010011 -45 ???????? ?? 1 00101100 44 Discard any final carry bit
Overflows • Overflow conditions • Indicated by an incorrect sign bit • There is a carry into the sign bit which produces an overflow condition • Can occur only when both numbers are positive or both numbers are negative 01111101 (+125d) 00111010 (+58d) 10110111 (+183d)
Floating Point Numbers • To represent very large/very small integers • Numbers that have both integer and fractional parts • A Floating point number consists of two parts plus a sign • Exponent: Number of places that the binary point is to be moved (power of 2) • Mantissa; represents the magnitude of the number • A format is defined by ANSI/IEEE 754 • Single precision, double precision and extended precision
S Biased Exp 10001011 10110000011100000000000 0 Floating Point Numbers • Single Precision Floating Point Binary Number • +11011000001112 +1.101100000111 x 212(Normalized form) S = 0 Biased Exponent = 10001011 (127 + 12 = 139) Mantissa = 10110000011100000000000 (hidden 1) Mantissa
10100000000000000000000 0 10000100 01011000000000000000000 1 00000001 00000000000000000000000 0 00000000 00000000000000000000000 0 11111111 Floating Point Numbers +1.101 x 25 -1.01011 x 2-126 0 ∞
Need for Hexadecimal Number System • Long binary numbers are difficult to read and write; easy to transpose or drop a bit • Most digital systems process binary data in groups that are multiples of four bits -> easy to convert • Base 16 number system • Ten digits and six alphabetic characters • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Hexadecimal Number System • Counting in Hexadecimal • Once we get to F, we start over with another column and continue
Hexadecimal Number System • Counting in Hexadecimal
Hexadecimal Number System • Binary to Hexadecimal Conversion • Starting from the LSB, replace each 4-bit group with the equivalent hexadecimal symbol 1101 0110 1011 1001 0110 D 6 B 9 6 • Hexadecimal to binary conversion • Replace each hexadecimal digit with appropriate four bits F D 1 3 1111 1101 0001 0011
Hexadecimal Number System • Hexadecimal-to-Decimal Conversion • Sum-of-Weights Method CA02h = (C x 163) + (A x 162) + (0 x 161) + (2 x 160) = (12 x 163) + (10 x 162) + (0 x 161) + (2 x 160) = (12 x 4096) + (10 x 256) + (0 x 16) + (2 x 1) = 49152 + 2560 + 0 + 2 = 51714
Hexadecimal Number System • Decimal to Hexadecimal Conversion • Repeated Division-by-16 method Result: 35Bh STOP when 0 reached
Hexadecimal Number System • Hexadecimal Addition • In any given column, think of two hexadecimal digits in terms of their decimal values • Example Carry 1 2 A C 6 6+5=11d Bh + 9 2 B 5 C+B=23d 17h B D7 B A+2+1=13d Dh 2+9=11d Bh
Hexadecimal Number System • Hexadecimal Subtraction • Example Borrow 111 92B5 21-6=15d Fh - 2AC6 26-C=14d Eh 67EF 17-A=7d 7h 8-2=6d 6h
Octal Numbers • Composed of eight digits 0,1,2,3,4,5,6,7 • To count above 7, begin another column and start over
Octal Numbers • Counting in Octal
Octal Numbers • Binary-to-Octal Conversion • Start with the right most group of three bits • Moving from right to left convert each 3-bit group to the equivalent octal digit • For the left most group, add zeros to complete the group if required • Example 11010110101110010110 011 010 110 101 110 010 110 3 2 6 5 6 2 6
Octal Numbers • Octal-to-Binary Conversion • Each octal digit can be represented by a 3-bit binary # 17268 001 111 010 110 • Octal-to-Decimal Conversion • Sum-of-Weights method 40338 (4 x 83) + (0 x 82) + (3 x 81) + (3 x 80) (4 x 512) + (0 x 64) + (3 x 8) + (3 x 1) 2048 + 0 + 24 + 3 2075