Computer Math CPS 120 Introduction to Computer Science

  • Slides: 30
Download presentation
Computer Math CPS 120 Introduction to Computer Science Lecture 4

Computer Math CPS 120 Introduction to Computer Science Lecture 4

Memory Units n n n 1 1 1 nibble byte word long word quad

Memory Units n n n 1 1 1 nibble byte word long word quad word octa-word n n n 4 consecutive bits 8 consecutive bits 2 consecutive bytes 4 consecutive bytes 8 consecutive bytes 16 consecutive bytes

Larger Units of Memory n n n 1 1 1 Kilobyte Megabyte Gigabyte Terabyte

Larger Units of Memory n n n 1 1 1 Kilobyte Megabyte Gigabyte Terabyte Petabyte Exabyte n n n 1024 bytes ~106 bytes ~109 bytes ~1012 bytes ~1015 bytes ~1018 bytes 32 Mb = 32*103 Kb = 32 * 103 *1024 bytes = 32, 768, 000 bytes

Representing Data n The computer knows the type of data stored in a particular

Representing Data n The computer knows the type of data stored in a particular location from the context in which the data are being used; n n i. e. individual bytes, a word, a longword, etc 01100011 01100101 01000000 n n n Bytes: 99(10, 101 (10, 68 (10, 64(10 Two byte words: 25, 445 (10 and 17, 472 Longword: 1, 667, 580, 992 (10

Alphanumeric Codes n American Standard Code for Information Interchange (ASCII) n n n 7

Alphanumeric Codes n American Standard Code for Information Interchange (ASCII) n n n 7 -bit code Since the unit of storage is a bit, all ASCII codes are represented by 8 bits, with a zero in the most significant digit H e l l o W o r l d 48 65 6 C 6 C 6 F 20 57 6 F 72 6 C 64 Extended Binary Coded Decimal Interchange Code (EBCDIC)

Number Systems n We use the DECIMAL (10 system n Computers use BINARY (2

Number Systems n We use the DECIMAL (10 system n Computers use BINARY (2 or some shorthand for it like OCTAL (8 or HEXADECIMAL (16

Codes Given any positive integer base (RADIX) N, there are N different individual symbols

Codes Given any positive integer base (RADIX) N, there are N different individual symbols that can be used to write numbers in the system. The value of these symbols range from 0 to N-1 n All systems we use in computing are positional systems n n 495 = 400 + 90 +5

Conversions

Conversions

Decimal Equivalents n Assuming the bits are unsigned, the decimal value represented by the

Decimal Equivalents n Assuming the bits are unsigned, the decimal value represented by the bits of a byte can be calculated as follows: 1. Number the bits beginning on the right using superscripts beginning with 0 and increasing as you move left • 2. 3. 4. Note: 20, by definition is 1 Use each superscript as an exponent of a power of 2 Multiply the value of each bit by its corresponding power of 2 Add the products obtained

Horner’s Method n Another procedure to calculate the decimal equivalent of a binary number

Horner’s Method n Another procedure to calculate the decimal equivalent of a binary number n n Note: This method works with any base Horner’s Method: n n n Step Step digit 1: 2: 3: 4: 5: Start with the first digit on the left Multiply it by the base Add the next digit Multiply the sum by the base Continue the process until you add the last

Binary to Hex n Step 1: Form four-bit groups beginning from the rightmost bit

Binary to Hex n Step 1: Form four-bit groups beginning from the rightmost bit of the binary number n If the last group (at the leftmost position) has less than four bits, add extra zeros to the left of the group to make it a four-bit group n n n 011001111010100111 becomes 0001 1110 1010 0111 Step 2: Replace each four-bit group by its hexadecimal equivalent n 19 EAA 7(16

Converting Decimal to Other Bases n n n Step 1: Divide the number by

Converting Decimal to Other Bases n n n Step 1: Divide the number by the base you are converting to (r) Step 2: Successively divide the quotients by (r) until a zero quotient is obtained Step 3: The decimal equivalent is obtained by writing the remainders of the successive division in the opposite order in which they were obtained n n Know as modulus arithmetic Step 4: Verify the result by multiplying it out

Addition & Subtraction Terms n A+B n n n A is the augend B

Addition & Subtraction Terms n A+B n n n A is the augend B is the addend C–D n n C is the minuend D is the subtrahend

Addition Rules – All Bases Addition n Step 1: Add a column of numbers

Addition Rules – All Bases Addition n Step 1: Add a column of numbers n Step 2: Determine if there is a single symbol for the result n Step 3: If so, write it and go to the next column. If not, write the accompanying number and carry the appropriate value to the next column

Subtraction Rules – All Bases n n Step 1: Start with the rightmost column,

Subtraction Rules – All Bases n n Step 1: Start with the rightmost column, if the column of the minuend is greater than that of the subtrahend, do the subtraction, if not… Step 2: Borrow one unit from the digit to the left of the once being processed n n n The borrowed unit is equal to “borrowing” the radix Step 4: Decrease the column form which you borrowed by one Step 3: Subtract the subtrahend from the minuend and go to the next column

Addition of Binary Numbers n Rules for adding or subtracting very similar to the

Addition of Binary Numbers n Rules for adding or subtracting very similar to the ones in decimal system n 0 n 1 n Limited to only two digits + + 0 1 = = 0 1 1 0 carry 1

Addition & Subtraction of Hex n n n Due to the propensity for errors

Addition & Subtraction of Hex n n n Due to the propensity for errors in binary, it is preferable to carry out arithmetic in hexadecimal and convert back to binary If we need to borrow in hex, we borrow 16 It is convenient to think “in decimal” and then translate the results back to hex

Representing Signed Numbers n Remember, all numeric data is represented inside the computer as

Representing Signed Numbers n Remember, all numeric data is represented inside the computer as 1 s and 0 s n n Arithmetic operations, particularly subtraction raise the possibility that the result might be negative Any numerical convention needs to differentiate two basic elements of any given number, its sign and its magnitude n Conventions n n n Sign-magnitude Two’s complement One’s complement

Representing Negatives n It is necessary to choose one of the bits of the

Representing Negatives n It is necessary to choose one of the bits of the “basic unit” as a sign bit n n Usually the leftmost bit By convention, 0 is positive and 1 is negative Positive values have the same representation in all conventions However, in order to interpret the content of any memory location correctly, it necessary to know the convention being used for negative numbers

Comparing the Conventions

Comparing the Conventions

Sign-Magnitude For a basic unit of N bits, the leftmost bit is used exclusively

Sign-Magnitude For a basic unit of N bits, the leftmost bit is used exclusively to represent the sign n The remaining (N-1) bits are used for the magnitude n The range of number represented in this convention is – 2 N+1 to +2 N-1 -1 n

Sign-magnitude Operations n Addition of two numbers in sign-magnitude is carried out using the

Sign-magnitude Operations n Addition of two numbers in sign-magnitude is carried out using the usual conventions of binary arithmetic n n n If both numbers are the same sign, we add their magnitude and copy the same sign If different signs, determine which number has the larger magnitude and subtract the other from it. The sign of the result is the sign of the operand with the larger magnitude If the result is outside the bounds of – 2 n+1 to +2 n -1 – 1, an overflow results

One’s Complement Convention Devised to make the addition of two numbers with different signs

One’s Complement Convention Devised to make the addition of two numbers with different signs the same as two numbers with the same sign n Positive numbers are represented in the usual way n For negatives n n n STEP 1: Start with the binary representation of the absolute value STEP 2: Complement all of its bits

One's Complement Operations n n Treat the sign bit as any other bit For

One's Complement Operations n n Treat the sign bit as any other bit For addition, carry out of the leftmost bit is added to the rightmost bit – end-around carry

Two’s Complement Convention n n A positive number is represented using a procedure similar

Two’s Complement Convention n n A positive number is represented using a procedure similar to sign-magnitude To express a negative number 1. 2. 3. – – Express the absolute value of the number in binary Change all the zeros to ones and all the ones to zeros (called “complementing the bits”) Add one to the number obtained in Step 2 The range of negative numbers is one larger than the range of positive numbers Given a negative number, to find its positive counterpart, use steps 2 & 3 above

Two’s Complement Operations n Addition: n Treat the numbers as unsigned integers n n

Two’s Complement Operations n Addition: n Treat the numbers as unsigned integers n n n The sign bit is treated as any other number Ignore any carry on the leftmost position Subtraction n n Treat the numbers as unsigned integers If a "borrow" is necessary in the leftmost place, borrow as if there were another “invisible” one-bit to the left of the minuend

Overflows in Two’s Complement n n n The range of values in two’s-complement is

Overflows in Two’s Complement n n n The range of values in two’s-complement is – 2 n+1 to +2 n-1 – 1 Results outside this band are overflows In all overflow conditions, the sign of the result of the operation is different than that of the operands n n If the operands are positive, the result is negative If the operands are negative, the result is positive

Transmission Errors n When binary data is transmitted, there is a possibility of an

Transmission Errors n When binary data is transmitted, there is a possibility of an error in transmission due to equipment failure or "noise" n Bits change from 0 to 1 or vice-versa

Categorizing Coding Schemes n The number of bits that have to change within a

Categorizing Coding Schemes n The number of bits that have to change within a byte before it becomes invalid characterizes the code n Single-error-detecting code n n To detect single errors have occurred we use an added parity check bit – makes each byte either even or odd Two-error-detecting code

Error Detection: Even Parity n n n Bytes Transmitted 01100011 11100001 01110100 11110011 00000101

Error Detection: Even Parity n n n Bytes Transmitted 01100011 11100001 01110100 11110011 00000101 Parity Block B I T n n n Bytes Received 01100011 11100001 1111110011 00000101 Parity Block B I T