Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
Counting…. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, … What comes next? Why do we have ten digits?
Number Representations We are used to base/radix 10 (decimal) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 10, 20, 30, …, , …, 100, …, 1000, …,
Decimal Example: : Represents: Decimal 1* * *10 + 1*1 + 2/10 + 0/ /
Number Representations General radix r number, N r Represents: d p d p-1 … d 2 d 1 d 0. d –1 d –2 … d –q r p r p-1 … r 2 r 1 r 0 r -1 r -2 … r -q d p r p + d p-1 r p-1 + …+ d 2 r 2 + d 1 r 1 + d 0 r 0 + d –1 r d –2 r --2 +…d –q r -q
Binary Computer uses presence/absence of voltage Digits 0 and 1(off/on). i.e. base 2 Bit = B inary dig it
Binary Binary Example: : Represents: 1*8 + 0*4 + 0*2 + 1*1 + 1/2 + 0/4 + 1/8 =
Binary Integers (whole numbers) stored in n-bit words n-bit binary number has range: 0 to (2 n -1) Largest 8-bit number: = = = = =255 10
Binary to Decimal Conversion 1.quot = number; i = 0; 2.repeat until quot = 0: 1.quot = quot/2; 2.digit i = remainder; 3.i++; gives digits from least to most significant bit Example: 33/2 = 16 rem 1(lsb) 16/2 = 8 rem 0 8/2 = 4 rem 0 4/2 = 2 rem 0 2/2 = 1 rem 0 1/2 = 0 rem 1(msb) => =
Converting fractional numbers Convert int and frac. parts separately i = 0; repeat until N = 1.0 or i = n: N = FracPart(N); N *= 2; digit i = IntPart(N); i++ Example: *2 = int = 1 (msb) *2 = int = *2 = int = *2 = int = 1 (lsb) => = Caution: Many numbers cannot be represented accurately: = 0.0[1001]... 2 (bracket repeats, limited by bit size)
Binary Addition Adding binary numbers: = = = = 0 carry 1 Add to : ____________
Binary Addition Adding binary numbers: = = 1; = 0; = 0 carry 1 Add to : Check it!
Binary Addition: Overflow Possibility of overflow Add 2 10 to : [1] = We only have 8 bits to store answer...so it's zero!
Binary Addition: Overflow Overflow: what happens? Program can generate an “exception” to let us know Usually many bits used (e.g. Intel word is 32-bits) so occurrence is reduced
Signed Numbers So far, only have positive numbers What about negative numbers? What about subtraction?
Signed Numbers Can use left-most bit to encode sign 0 -> pos, 1 -> neg
Signed Numbers For 8-bits, range is: Problems: –Two zeros!! –Addition not straight forward –Must check sign-bit before adding (bad for hardware implementors) This is non-sensical and wasteful: can use extra bit pattern -(2 7 -1) +(2 7 -1)
One’s Complement Try “one's complement”: –negative numbers obtained by flipping bits –positive numbers unchanged –Example: Left-most bit still indicates sign
Subtraction with One’s complement Now easy to subtract: complement number and add: = complement ( ) = = = complement ( ) = (-1)
Subtraction with One’s complement Caution: If overflow, –if numbers have different signs, carry is added into lsb –if numbers have same sign, actual overflow has occured
Subtraction with One’s complement Evaluate 10–7 using 8-bit one’s complement arithmetic:
Subtraction with One’s complement Evaluate 10–7 using 8-bit one’s complement arithmetic: = complement ( ) = = carry 1 = = = 3 10
Two’s Complement Still have two zeros two’s complement –Complement then add 1 –Our number range now asymmetric: -2 7 to –Used extra zero bit pattern
Two’s Complement Now when we add, discard carry = complement ( ) = = carry 1 (discard) = 3 Same overflow test can be used (same/different signs)
Octal and Hexadecimal Base 8 (octal) and base 16 (Hexadecimal) are sometimes used (powers of 2) Octal uses 8 digits (0-7) Hex uses 16 “digits”: 0, 1, 2, 3, 4, 5, 6, 7,8, 9, A, B, C, D, E, F
Octal and Hexadecimal Each octal digit represents 3-bits Each hex digit represents 4-bits Examples: = = (001)(101) 2 = 15 8 = (1101) 2 = D 16
Octal and Hexadecimal Conversion from decimal as for bin: –divide/multiply by 8 or 16 instead –May be easier to convert to binary first Binary to octal or hexadecimal –group bits into 3 (octal) or 4 (hex) from LS bit –pad with leading zeros if required
Octal and Hexadecimal: Example
Octal and Hexadecimal: Example = (000) (100) (011) (011) (010) (111) = = (0100) (0110) (1101) (0111) = 46D7 16 Note padding at front of number
Octal and Hexadecimal To convert from hex/octal to binary: reverse procedure FF 16 = (1111)(1111) = (011)(111)(111) 2 NOTE: for fractional conversion, move from left to right and pad at right end: = 0. (110) (011) (010) (110) = = 0.(110) 2 = Convert fractional/integer part separately