CSI 333 Lecture 1 Number Systems 1 1 / 23
Basics of Number Systems Ref: Appendix C of Deitel & Deitel. Weighted Positional Notation: 192 = 2 10 0 + 9 10 1 + 1 10 2 General: Digit sequence : d n 1 d n 2... d 1 d 0 Value : Decimal system: n 1 ( di 10 i) i=0 Base (or Radix) : 10 Digits : 0, 1, 2,..., 9 1 2 / 23
Number Systems (continued) Base r system: Digits : 0, 1,..., r 1 Digit sequence : s n 1 s n 2... s 1 s 0 Value : n 1 ( si r i) i=0 s 0 - - Least significant digit s n 1 - - Most significant digit 1 3 / 23
Number Systems (continued) Common Values for Radix r: r = 2 : Binary - - Digits: 0, 1. r = 8 : Octal - - Digits: 0, 1..., 7. r = 10 : Decimal - - Digits: 0, 1,..., 9. r = 16 : Hexadecimal (Hex) - - Digits: 0, 1,..., 9, A, B, C, D, E, F (where A = 10, B = 11,..., F = 15). Convention: Base written as subscript (e.g. 277 8 ). 1 4 / 23
Converting from any base to Decimal Example 1: Find the decimal value of 277 8. 277 8 = 7 8 0 + 7 8 1 + 2 8 2 = 7 + 56 + 128 = 191 10 Example 2: Find the decimal value of 3F 4 16. 3F 4 16 = 4 16 0 + 15 16 1 + 3 16 2 = 4 + 240 + 768 = 1012 10 1 5 / 23
Converting to Decimal (continued) Example 3: Find the decimal value of 10110 2. Convention in C: 315 - - Decimal 0315 - - Octal 0x315 - - Hex 10110 2 = 2 1 + 2 2 + 2 4 = 2 + 4 + 16 = 22 10 Machine hardware: Uses binary. 1 6 / 23
Example: 1 0 0 0 1 1 0 1 1 1 1 2 = 2157 8 1 7 / 23 Octal and Hexadecimal Number Systems Note: Examples: Each octal digit - - 3 bits Each hex digit - - 4 bits 6 8 = 110 2 C 16 = 1100 2 Converting from Binary to Octal: 1 Moving from right to left, form groups of three bits. (May need to add leading zeros.) 2 Write octal equivalent for each group.
Octal and Hexadecimal (continued) Converting from Binary to Hex: Similar to binary to octal conversion, except that we form groups of 4 bits. Example: 1 0 1 0 1 1 0 1 1 0 1 2 = 56D 16 Converting Octal or Hex to Binary: Express each digit in binary. Examples: 124 8 = 001 010 100 2 194 16 = 0001 1001 0100 2 Note: Octal and hex are short forms for representing binary numbers. 1 8 / 23
Converting from Decimal to Other Bases Example 1: Convert 37 10 to binary. So, 37 10 = 100101 2. Division Quotient Remainder 37 2 18 1 18 2 9 0 9 2 4 1 4 2 2 0 2 2 1 0 1 2 0 1 Remark: In the binary representation, the remainders are written in bottom-to-top order. 1 9 / 23
Converting from Decimal... (continued) Example 2: Convert 844 10 to hex. So, 844 10 = 34C 16. Division Quotient Remainder 844 16 52 12 = C 52 16 3 4 3 16 0 3 Remark: Again, the remainders are written in bottom-to-top order. 1 10 / 23
2 s Complement form for Negative Integers Sign Convention in Binary: Most significant bit = 0 for non-negative integers. Most significant bit = 1 for negative integers. An Algorithm for Finding 2 s Complement Form: 1 Compute the binary representation for the corresponding positive integer. 2 Complement each bit. 3 Add 1. 1 11 / 23
2 s Complement form... (continued) Recall: Binary addition table. Inputs Sum Carry ------ --- ----- 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 1 12 / 23
2 s Complement form... (continued) Problem: Find the 2 s complement representation of 15 using 8 bits. Solution: +15 in 8-bit binary: 0 0 0 0 1 1 1 1 Complement each bit: 1 1 1 1 0 0 0 0 Add 1 : 1 --------------- 1 1 1 1 0 0 0 1 <-- Answer 1 13 / 23
2 s Complement form... (continued) Problem: Find the 2 s complement representation of 64 using 8 bits. Solution: +64 in 8-bit binary: 0 1 0 0 0 0 0 0 Complement each bit: 1 0 1 1 1 1 1 1 Add 1 : 1 1 1 1 1 1 1 <-- Carries --------------- 1 1 0 0 0 0 0 0 <-- Answer 1 14 / 23
2 s Complement form... (continued) Another method for 2 s complement: 1 Start with the binary representation of the positive value. 2 Copy bits from right to left, until the first 1 has been copied. 3 Complement every bit thereafter. Problem: Find the 2 s complement representation of 64 using 8 bits. Solution: +64 in 8-bit binary: 0 1 0 0 0 0 0 0 2 s complement for -64: 1 1 0 0 0 0 0 0 ^ (First 1 copied) 1 15 / 23
2 s Complement form... (continued) Problem: What is the 2 s complement of 00000000? Solution: Given: 0 0 0 0 0 0 0 0 Complement: 1 1 1 1 1 1 1 1 Add 1 : 1 1 1 1 1 1 1 1 <--- Carries --------------- 0 0 0 0 0 0 0 0 <--- Answer (Value = 0) 1 --> Carry out of sign bit (should be ignored) 1 16 / 23
Representing Real Numbers Remark: Unlike integers, some real numbers cannot be represented exactly. Weighted Positional Notation (for Reals): Example: 119.75 10 Integer part: 119 10 Fractional part: 0.75 10 119 10 = 9 10 0 + 1 10 1 + 1 10 2 0.75 10 = 7 10 1 + 5 10 2 1 17 / 23
Representing Reals (continued) General formula for base r: Sequence : s n 1 s n 2... s 1 s 0. s 1 s 2... s p Value : n 1 ( si r i) + i=0 p ( s i r i) i=1 Example 1: Convert 74.26 8 to decimal. 74.26 8 = 4 8 0 + 7 8 1 + 2 8 1 + 6 8 2 = 60 + 0.25 + 0.09375 = 60.34375 10 1 18 / 23
Representing Reals (continued) Example 2: Convert 101.011 2 to decimal. 101.011 2 = 2 0 + 2 2 + 2 2 + 2 3 = 5 + 0.25 + 0.125 = 5.375 10 Example 3: Convert 21.1 3 to decimal. 21.1 3 = 1 3 0 + 2 3 1 + 1 3 1 = 7 + 0.333... (non-terminating) = 7.3 10 1 19 / 23
Decimal to Binary Conversion for Real Numbers Real number: Integer part. Fractional part Method: Example 1: Convert each part separately. Convert 19.125 10 to binary. 19 10 = 10011 2 (decimal to binary for integer part). To convert 0.125 10 to binary: 0.125 2 = 0.25 : d 1 = 0 0.25 2 = 0.5 : d 2 = 0 0.5 2 = 1.0 : d 3 = 1 So, 0.125 10 = 0.001 2. Therefore, 19.125 10 = 10011.001 2. 1 20 / 23
Decimal to Binary for Reals (continued) Example 2: Convert 76.3125 10 to binary. 76 10 = 1001100 2 (decimal to binary for integer part). To convert 0.3125 10 to binary: 0.3125 2 = 0.625 : d 1 = 0 0.625 2 = 1.25 : d 2 = 1 0.25 2 = 0.5 : d 3 = 0 0.5 2 = 1.0 : d 4 = 1 So, 0.3125 10 = 0.0101 2. Therefore, 76.3125 10 = 1001100.0101 2. 1 21 / 23
Decimal to Binary for Reals (continued) Example 3: Convert 0.7 10 to binary. 0.7 2 = 1.4 : d 1 = 1 0.4 2 = 0.8 : d 2 = 0 0.8 2 = 1.6 : d 3 = 1 0.6 2 = 1.2 : d 4 = 1 0.2 2 = 0.4 : d 5 = 0 0.4 2 = 0.8 : d 6 = 0. The part 0110 repeats indefinitely; so, 0.7 10 = 0.10110 2. Later: IEEE Standard for representing real (or floating point) numbers. 1 22 / 23
Suggestions and Questions to Think About Try additional base conversion problems, including those involving real numbers. Try converting negative integers into 2 s complement form. Recall that converting a real number from base 3 to base 10 may lead to a non-terminating representation. Can this happen when we do the following? Convert a real number in base 5 to base 10. Convert a real number in base 10 to base 5. 1 23 / 23