Dale & Lewis Chapter 3 Data Representation
Data and computers Everything inside a computer is stored as patterns of 0s and 1s Numbers, text, audio, video, images, graphics, etc. −How do you convert these to 0s and 1s? −How do you store the digital information efficiently?
Representing numeric data Representing Natural numbers with a finite number of digits General Property: Number of digitsMinMax n0b n -1 Example: −b=10, n=3000 to 999 −b=2, n=3000 to 111 (equivalent to 0 to 7 in decimal) −b=8, n=3000 to 777 (equivalent to 0 to 511 in decimal) −b=16, n=3000 to FFF (equivalent to 0 to 4,095 in decimal)
Representing negative numbers (integers) Basic definition −An integer is a number with no fractional part −Examples: Integers (signed natural numbers) −Previously we looked at unsigned numbers, i.e. natural numbers −Need a mechanism to represent both positive and negative numbers −Two schemes: 1) sign-magnitude, 2) complementary representaion
Sign-magnitude In binary: −Sign: left-most bit (0 = positive, 1 = negative) −Magnitude: remaining bits Example with 6-bit sign-magnitude representation +5 = = Ranges in binary UnsignedSign-magnitude # of bitsMinMaxMinMax n02 n -1-(2 n-1 -1) +(2 n-1 -1)
Complementary representation Difficulties with Sign-magnitude −Two representations for zero +0 = = −Arithmetic is awkward, especially subtraction Complementary representation −Positive numbers are represented by their corresponding natural numbers −Negative numbers are represented as very large natural numbers −Subtracting numbers reduces to performing addition operations negative numbers: negative(x) ≡ b n -x
Complementary representation negative numbers: negative(x) ≡ b n -x Example: let b = 10 and n = 2 Positive numbersNegative numbers (+0) 00 (+1) 01(-1) 99 (+2) 02(-2) 98 (+3) 03(-3) 97 … (+49) 49(-49) 51 (-50) 50
Visualization
Examples of arithmetic in Ten’s complement -35 plus +25 equals plus 25 equals 90 Ten’s complement +17 plus +25 equals plus 25 equals 42 Ten’s complement +20 plus -30 equals plus 70 equals 90 Ten’s complement -20 plus +30 equals plus 30 equals 10 (110) Ten’s complement
Examples of arithmetic in Ten’s complement Subtraction reduces to addition because A – B = A + (-B) Easy to convert between positive number and its negative counterpart −This conversion can be made efficiently and simplify logic circuitry (we will see how)
Two’s complement representation Negative numbers: negative(x) ≡ 2 n -x Used in computers because subtraction reduces to addition simpler circuits To form a negative number −Start with the positive version of the number −Flip the bits: 0 1 and 1 0 −Add 1 to the number produced in the previous step Same process for conversion back to positive numbers The above steps are an indirect way of carrying our the evaluation of 2 n -x, or more conveniently [(2 n -1)-x]+1
Examples of Two’s complement +5 (0101) to -5 (1011) -5 (1011) to +5 (0101) Evaluating 2 n -x is = 16-5 = Or 1011 Evaluating [(2 n -1)-x]+1 is [ ( )-5]+1 = = (this way you never borrow)
+2 = = = = = = ↑ throw away -2 = 1110 = = 0010 = = = = = = = 1111 ↑ throw away
Sample test/exam questions Q: Convert -173 to 12-bit two’s complement representation. Show all your work. A: Step 1: Convert 173 to binary by repeated division by ÷ ÷ ÷ ÷ ÷ ÷ ÷ ÷
Sample test/exam questions Q: Convert -173 to 12-bit two’s complement representation. Show all your work. A: Step 1: Convert 173 to binary by repeated division by 2 Step 2: Expand answer in Step 1 to 12-bits Step 3: Flip-the-bits and add one Final answer: -173 =
Sample test/exam questions Q: Convert +173 to 12-bit two’s complement representation. Show all your work. Q: Convert the 12-bit two’s complement number to decimal.