1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic
2 Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
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 … −1 10 −2 … (1×10 2 ) + (7×10 1 ) + (3×10 0 ) + (6×10 -1 )+(2×10 -2 ) base = 10 weights
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 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:
6 Counting in binary In-class exercise: Fill in the next eight rows
7 Counting in binary 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 Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
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 Decimal 49 = binary Write: =
10 … ½ ¼ … In binary, weights are powers of 2 Binary → decimal … −1 2 −2 … ¼ = Example: Convert binary to decimal
11 Convert to decimal Convert and to binary Convert and to binary
12 Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
13 Octal Base-8 weighted number system 8 digits: 0,…,7 Digits 8 and 9 not allowed in octal! Weights are powers of Example: Express in decimal 3× ×64 + 0×8 + 2×1 = DecimalOctalBinary
14 Hexadecimal ABCDEF ABCDEF 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× × ×16 +15×1 = Example: Express 1A2F 16 in decimal A 2 F Hexadecimal is much more commonly used than octal
15 Convert to octal Convert to hexadecimal
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 in hexadecimal F1693F = 3F Break into 4-bit groups … adding leading zeros if necessary Replace each group with hexadecimal digit
17 Binary → octal Same way as converting binary to hexadecimal except break binary number into 3-bit groups Example: Express in octal-base
DecimalBinaryBCD 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 in BCD
19 Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
20 Adding two bits Four possible cases: = 0sum=0, carry= = = 1sum=1, carry= = = 1sum=1, carry= = = 10sum=0, carry= = 2 augend addend + carry sum Decimal
21 Binary addition Example: Add the binary numbers 011 and 001 and show the equivalent decimal addition
22 Binary addition with carry in = 01sum=1, carry= = = 10sum=0, carry= = = 11sum=1, carry= = 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 Example: Add the binary numbers and Show the equivalent decimal addition Binary addition
24 Number systems and arithmetic Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers
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 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: (39= ) Change sign bit to a 1 to represent −39:
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 ↓↓↓↓↓↓↓↓
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 ’s complement 1 +add ’s complement
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 using the shortcut method binary number ’s complement 1’s complement of original bits These bits stay the same
30 2’s complement Find the 2’s complement of using both methods described above
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 = (since 25 = ) 1’s complement of is
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 = ’s complement of is
33 Express 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 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: = = = = = = = = = -9 1 Discard carry out of sign bit Arithmetic with signed binary numbers: addition
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 = = = − = − = +2 Examples: Answers are both incorrect as sign bit of result disagrees with sign of augend and addend Discard carry Overflow condition
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.
= +151 Discard carry Take 2’s complement of subtrahend and add: = = -15 Examples: same numbers on slide 14, but subtract rather than add: = = = = +71 Discard carry = = +8 (+30) –(+15) (+14) –(-17) (-1) –(-8) Arithmetic with signed binary numbers: subtraction