Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic.

Similar presentations


Presentation on theme: "1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic."— Presentation transcript:

1 1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic

2 2  Counting in binary  Binary ↔ decimal  Hexadecimal, octal and BCD  Binary arithmetic  Signed numbers

3 3 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 … 10 2 10 1 10 0. 10 −1 10 −2 … 1 7 3. 6 2 (1×10 2 ) + (7×10 1 ) + (3×10 0 ) + (6×10 -1 )+(2×10 -2 ) 100 + 70 + 3 + 0.6 + 0.02 base = 10 weights

4 4 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: … N 3 N 2 N 1 N 0. N -1 N -2 …  In this course, we are interested in: N=2binary (Base-2)0,1 N=8octal (Base-8)0,1,…,7 N=10decimal0,1,…,9 N=16hexadecimal (“hex”)???

5 5 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...

6 6 Counting in binary 0 1 10 11 100 101 110 111 In-class exercise: Fill in the next eight rows

7 7 Counting in binary 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 Decimal Number Binary Number N bits We meet the binary counting sequence many times in this course You’ll need to remember how to count in binary Decimal number equal to binary 111…1? 2 N - 1 Decimal number equal to binary 111…1?

8 8 Number systems and arithmetic  Counting in binary  Binary ↔ decimal  Hexadecimal, octal and BCD  Binary arithmetic  Signed numbers

9 9 1.Write the decimal weight of each column 2.Place 1 ’ s in the columns that sum to the decimal number Decimal → binary Example: Convert decimal 49 to binary 2 6 2 5 2 4 2 3 2 2 2 1 2 0 64 32 16 8 4 2 1 0 1 1 0 0 0 1 Decimal 49 = binary 110001 Write: 49 10 = 110001 2

10 10 … 32 16 8 4 2 1. ½ ¼ … 1 0 0 1 0 1. 0 1 In binary, weights are powers of 2 Binary → decimal … 2 5 2 4 2 3 2 2 2 1 2 0. 2 −1 2 −2 … 32 +4 +1 +¼ = 37.25 10 Example: Convert binary 100101.01 to decimal

11 11 Convert 10111101.011 2 to decimal Convert 19 10 and 99 10 to binary Convert 75.5 10 and 119.25 10 to binary

12 12 Number systems and arithmetic  Counting in binary  Binary ↔ decimal  Hexadecimal, octal and BCD  Binary arithmetic  Signed numbers

13 13 Octal  Base-8 weighted number system  8 digits: 0,…,7  Digits 8 and 9 not allowed in octal!  Weights are powers of 8 8 3 8 2 8 1 8 0 512 64 8 1 Example: Express 3702 8 in decimal 3×512 + 7×64 + 0×8 + 2×1 = 1986 10 512 64 8 1 3 7 0 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 DecimalOctalBinary

14 14 Hexadecimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0123456789ABCDEF0123456789ABCDEF 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 DecimalHexadecimalBinary  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 1×4096 + 10×256 + 2×16 +15×1 = 6703 10 16 3 16 2 16 1 16 0 4096 256 16 1 Example: Express 1A2F 16 in decimal 4096 256 16 1 1 A 2 F Hexadecimal is much more commonly used than octal

15 15 Convert 439 10 to octal Convert 650 10 to hexadecimal

16 16 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 111111000101101001 2 in hexadecimal 11 1111000101101001 00111111000101101001 3F1693F169 111111000101101001 2 = 3F169 16 Break into 4-bit groups … adding leading zeros if necessary Replace each group with hexadecimal digit

17 17 Binary → octal Same way as converting binary to hexadecimal except break binary number into 3-bit groups Example: Express 111111000101101001 2 in octal-base

18 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 DecimalBinaryBCD 0001 0001 0001 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010 0011 0100 0101 Binary-coded decimal (BCD) 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 2469 10 in BCD 24692469 0010010001101001

19 19 Number systems and arithmetic  Counting in binary  Binary ↔ decimal  Hexadecimal, octal and BCD  Binary arithmetic  Signed numbers

20 20 Adding two bits Four possible cases: 0011 0 + 1 + 0 + 1 + 0 0 0 1 01 1 0 0 + 0 = 0sum=0, carry=00 + 0 = 0 0 + 1 = 1sum=1, carry=00 + 1 = 1 1 + 0 = 1sum=1, carry=01 + 0 = 1 1 + 1 = 10sum=0, carry=11 + 1 = 2 augend addend + carry sum Decimal

21 21 Binary addition Example: Add the binary numbers 011 and 001 and show the equivalent decimal addition

22 22 Binary addition with carry in 1 + 0 + 0 = 01sum=1, carry=01+0+0 = 1 1 + 0 + 1 = 10sum=0, carry=11+0+1 = 2 1 + 1 + 1 = 11sum=1, carry=11+1+1 = 3 Decimal When a carry is generated in one column:  need to add third bit (1) in the next column  means sum of one bit from each number, plus the carry bit

23 23 Example: Add the binary numbers 00111 and 10101. Show the equivalent decimal addition 00111 7 10101 21 0 1 0 1 1 1 1 0 1 28 Binary addition

24 24 Number systems and arithmetic  Counting in binary  Binary ↔ decimal  Hexadecimal, octal and BCD  Binary arithmetic  Signed numbers

25 25 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: 1.Sign-magnitude form 2.1’s complement form 3.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

26 26 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

27 27 1’s complement of a binary number: change all 1s to 0s, and all 0s to 1s 1’s complement Example: Find the 1’s complement of 10110010 10110010 ↓↓↓↓↓↓↓↓ 01001101

28 28 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 010011011’s complement 1 +add 1 010011102’s complement

29 29 2’s complement — shortcut method 1.Start at the right-most bit and write the bits as they are, till the first 1 2.Take the 1’s complement of the remaining bits Example: Find the 2’s complement of 10110010 using the shortcut method 10110010binary number 010011102’s complement 1’s complement of original bits These bits stay the same

30 30 2’s complement Find the 2’s complement of 10010000 using both methods described above

31 31 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 −25 10 in 8-bit 1’s complement form +25 10 = 00011001 2 (since 25 = 16+8+1) 1’s complement of 00011001 is 11100110

32 32 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 −25 10 in 8-bit 2’s complement form +25 10 = 00011001 2 2’s complement of 00011001 is 11100111

33 33 Express +68 10 as a binary number in 8-bit 2’s complement form Express −41 10 as a binary number in 8-bit 2’s complement form

34 34 To add two signed numbers in 2’s complement form:  Perform the addition  Discard any final carry out of MSB (=sign bit)  Result is in signed 2’s complement form  Result will be correct provided range is not exceeded Three examples: 00011110 = +30 00001111 = +15 00101101= +45 00001110 = +14 11101111 = -17 11111101= -3 11111111 = -1 11111000 = -8 11110111= -9 1 Discard carry out of sign bit Arithmetic with signed binary numbers: addition

35 35 Overflow occurs when result of addition is out of range  number of bits required to represent sum exceeds number of bits in the two numbers added  occurs if (+A) + (+B) = −Cor(−A) + (−B) = +C 01000000 = +64 01000001 = +65 10000001= −127 10000001 = −127 100000010= +2 Examples: Answers are both incorrect as sign bit of result disagrees with sign of augend and addend Discard carry Overflow condition

36 36 Arithmetic with signed binary numbers: subtraction To subtract two signed numbers in 2’s complement form:  Take 2’s complement of subtrahend, then add the minuend  A − B = A + (−B)  Digital computers can use the same circuitry to add and subtract → saving in hardware  Remember: 2’s complement of subtrahend B is the negation of B  Discard any final carry out of MSB. The result is in signed form.

37 37 00001111= +151 Discard carry Take 2’s complement of subtrahend and add: 00011110 = +30 11110001 = -15 Examples: same numbers on slide 14, but subtract rather than add: 00011110 00001111  00001110 11101111 11111111 11111000  00011111= +31 00001110 = +14 00010001 = +17 00000111= +71 Discard carry 11111111 = -1 00001000 = +8 (+30) –(+15) (+14) –(-17) (-1) –(-8) Arithmetic with signed binary numbers: subtraction


Download ppt "1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic."

Similar presentations


Ads by Google