370 likes | 604 Views
ELEC1700 Computer Engineering 1 Week 2 Monday lecture Number systems and arithmetic Semester 1, 2013. Number systems and arithmetic. Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers. Decimal numbers.
E N D
ELEC1700Computer Engineering 1Week 2 Monday lectureNumber systems and arithmetic Semester 1, 2013
Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
Decimal numbers • Position of each digit in a weighted number system is assigned a weight based on the base or radix of the system • Radix of decimal numbers is ten, because only ten symbols (0,1,2,…,9) are used to represent any number base = 10 weights … 102 101 100 . 10−1 10−2 … 1 73. 6 2 (1×102) + (7×101) + (3×100) + (6×10-1)+(2×10-2) 100 + 70 + 3 + 0.6 + 0.02
Base-N numbers • Decimal numbers are just a special case of more general base-N numbers • digits take values from 0,1,…,N−1 • weights have values: … N3 N2 N1 N0 . N-1 N-2 … • In ELEC1700, we are interested in: N=2 binary 0,1 N=8 octal 0,1,…,7 N=10 decimal 0,1,…,9 N=16 hexadecimal (“hex”) ??? What digits can we use for hex? (Ans: We’ll see in a few slides!)
Counting Decimal 0,1,2,3,4,5,6,7,8,9, 10,11,12,13,…,98,99, 100,101,… Binary Only have two digits, called 0 and 1. Now the counting sequence becomes: 0 1 10 11 100 101 110 111 1000 1001 ...
Counting in binary 0 1 10 11 100 101 110 111 In-class exercise: Fill in the next eight rows
Counting in binary Decimal Number Binary Number 0 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 4 0 1 0 0 5 0 1 0 1 6 0 1 1 0 7 0 1 1 1 8 1 0 0 0 9 1 0 0 1 10 1 0 1 0 11 1 0 1 1 12 1 1 0 0 13 1 1 0 1 14 1 1 1 0 15 1 1 1 1 N bits Decimal number equal to binary 111…1? 2N - 1 Decimal number equal to binary 111…1? We meet the binary counting sequence many times in this course You’ll need to remember how to count in binary
Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
Binary → decimal Example: Convert binary 100101.01 to decimal In binary, weights are powers of 2 … 2524 2322 21 20 . 2−1 2−2 … … 3216 84 2 1 . ½ ¼ … 1 0 0 1 0 1 . 0 1 32+4 +1 +¼ = 37.2510
Decimal → binary • Convert a decimal whole number to binary by reversing the procedure on previous slide: • Write the decimal weight of each column • Place 1’s in the columns that sum to the decimal number Example:Convert decimal 49 to binary 262524 2322 21 20 64 3216 8 4 2 1 01 1 0 0 0 1 Decimal 49 = binary 110001 Write: 4910 = 1100012
Convert 10111101.0112 to decimal Convert 910 to binary Convert 9.812510 to binary
Decimal fractions → binary To convert a decimal fraction to binary: • repeatedly multiply the fractional parts of successive multiplications by 2 • The integer parts form the binary number, one bit at a time • Continue until one of two things happens: • Fractional part = 0 (exact result obtained) • Specified number of bits is reached (desired accuracy is obtained) Most significant bit (MSB) Example:Convert 0.18810 to binary. Express your answer to 5 binary places. 0.188 × 2 = 0.376 integer part = 0 0.376 × 2 = 0.7520 0.752 × 2 = 1.5041 0.504 × 2 = 1.0081 0.008 × 2 = 0.0160 0.18810 ≈ 0.001102
Decimal fractions → binary Why does this method work? Multiply by 2: …b-1.b-2 b-3 b-4 b-5… Multiply by 2 again… …b-2.b-3 b-4 b-5 b-6… And multiply by 2 again… …b-3.b-4 b-5 b-6 b-7… We want to find these values … b0 .b-1 b-2b-3 b-4 … …20 .2−1 2−2 2−3 2−4 … Weights
Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
512 64 8 1 3 7 0 2 3×512 + 7×64 + 0×8 + 2×1 = 198610 Octal Decimal Octal Binary • Base-8 weighted number system • 8 digits: 0,…,7 • Digits 8 and 9 not allowed in octal! • Weights are powers of 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 0 1 2 3 4 5 6 7 10 1112 13 14 15 16 17 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 838281 80 512 64 8 1 Example:Express 37028 in decimal
Decimal Hexadecimal Binary 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 0 1 2 3 4 5 6 7 8 9 A B C D E F 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 163162161 160 4096 256 16 1 Example:Express 1A2F16 in decimal 4096 256 16 1 1 A 2 F 1×4096 + 10×256 + 2×16 +15×1 = 670310 Hexadecimal • Base-16 weighted number system • 16 digits: 0,…,9 plus characters A,B,C,D,E,F • Weights are powers of 16 → compact way of writing very large numbers Hexadecimal is much more commonly used than octal
Convert 43910 to octal Convert 65010 to hexadecimal
Binary → hexadecimal • Convert a binary number to hexadecimal by: • breaking binary number into 4-bit groups, starting at right-most bit • Replacing each group with equivalent hexadecimal digit Example:Express 1111110001011010012 in hexadecimal 11 1111 0001 0110 1001 0011 1111 0001 0110 1001 3 F 1 6 9 1111110001011010012 = 3F16916 Break into 4-bit groups … adding leading zeros if necessary Replace each group with hexadecimal digit
Binary → octal Same idea as converting binary to hexadecimal except break binary number into 3-bit groups Example:Express 1111110001011010012 in octal
Binary-coded decimal (BCD) Decimal Binary BCD 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010 0011 0100 0101 • In BCD, each decimal digit is expressed with it’s 4-bit binary code • 1010,1011,1100,1101,1110, 1111 are invalid BCD codes (why?) • BCD not as efficient as straight binary, but used widely for applications involving limited processing, eg. keypad inputs & clock displays • Example:Express 246910 in BCD 0001 0001 0001 0001 0001 0001 2 4 6 9 0010 0100 0110 1001
Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
Adding two bits Four possible cases: 0 0 1 1 0 + 1 + 0 + 1 + 0 0 0 10 11 0 augend addend + carry sum 0 + 0 = 0sum=0, carry=0 0 + 0 = 0 0 + 1 = 1sum=1, carry=0 0 + 1 = 1 1 + 0 = 1sum=1, carry=0 1 + 0 = 1 1 + 1 = 10sum=0, carry=1 1 + 1 = 2 Decimal
Binary addition Example:Add the binary numbers 011 and 001 and show the equivalent decimal addition
Binary addition with carry in • When a carry is generated in one column: • need to add three bits in the next column • one bit from each number, plus the carry bit 1 + 0 + 0 = 01 sum=1, carry=0 1+0+0 = 1 1 + 0 + 1 = 10 sum=0, carry=1 1+0+1 = 2 1 + 1 + 1 = 11 sum=1, carry=1 1+1+1 = 3 Decimal
Binary addition Example:Add the binary numbers 00111 and 10101. Show the equivalent decimal addition 0 1 1 1 001117 1010121 28 1 1 1 0 0
Half-adders and full-adders • Half-adder • Accepts two binary digits as inputs • Generates sum bit and carry bit as outputs • Full-adder • Accepts two binary digits and input carry as inputs • Generates sum output and output carry
Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
Signed numbers So far we have considered only addition of positive numbers. What about negative numbers? Signed numbers have both sign and magnitude information • Three ways of representing signed binary numbers: • Sign-magnitude form • 1’s complement form • 2’s complement form • All three methods use the sign bit (left-most bit) to tell whether the number is positive or negative: • Sign bit = 0 : positive number • Sign bit = 1 : negative number 2’s complement method is by far the most widely used method in applications
Signed numbers in sign-magnitude form To represent a signed binary number in sign-magnitude form: • left-most bit is the sign bit • remaining bits represent the magnitude Example:Express the decimal number −39 as an 8-bit number in sign-magnitude form Write 8-bit number for +39: 00100111 (39=32+4+2+1) Change sign bit to a 1 to represent −39: 10100111
1’s complement 1’s complement of a binary number: change all 1s to 0s, and all 0s to 1s Example:Find the 1’s complement of 10110010 10110010 ↓↓↓↓↓↓↓↓ 01001101
2’s complement To get the 2’s complement of a binary number: add 1 to the 1’s complement Example:Find the 2’s complement of 10110010 10110010 01001101 1’s complement 1 + add 1 01001110 2’s complement
2’s complement — shortcut method • Start at the right-most bit and write the bits as they are, up to and including the first 1 • Take the 1’s complement of the remaining bits Example:Find the 2’s complement of 10110010 using the shortcut method 10110010 binary number 01001110 2’s complement 1’s complement of original bits These bits stay the same
2’s complement Find the 2’s complement of 10010000 using both methods described above
Signed numbers in 1’s complement form To represent a signed binary number in 1’s complement form: • Positive numbers: • represented as “normal” binary numbers, i.e. same as positive sign-magnitude • Negative numbers: • represented as 1’s complement of corresponding positive number Example:Represent −2510 in 8-bit 1’s complement form +2510 = 000110012 (since 25 = 16+8+1) 1’s complement of 00011001 is 11100110
Signed numbers in 2’s complement form To represent a signed binary number in 2’s complement form: • Positive numbers: • represented as “normal” binary numbers, i.e. same as positive sign-magnitude • Negative numbers: • represented as 2’s complement of corresponding positive number Example:Represent −2510 in 8-bit 2’s complement form +2510 = 000110012 2’s complement of 00011001 is 11100111
Decimal value of signed numbers in 2’s complement form The negative number −2510 is written in 8-bit 2’s complement form as: Note that 11001112 ≠ 2510 This isn’t sign-magnitude form! −25 =11100111 Sign bit Magnitude bits An easy way to read a signed number that uses this notation is to assign the sign bit a column weight of −128 (for an 8-bit number). Then add the column weights for the 1’s.For an n-bit number, column weight of the sign bit is −2n-1 Example: show that 11000110 represents −58 as an 8-bit 2’s complement signed number: We’ll see why this works on Wednesday Column weights: −12864 32 16 8 4 2 1 1 1 0 0 0 1 1 0 −128+64 +4 +2 = −58