computer math cps120: lecture 3. binary computers have storage units called binary digits or bits:...

71
Computer Math CPS120: Lecture 3

Upload: emil-burke

Post on 05-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Computer Math

CPS120: Lecture 3

Page 2: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary computers have storage units called binary digits or bits:

Low Voltage = 0High Voltage = 1 all bits have 0 or 1

22

Binary and Computers

Page 3: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

8 bits = 1 byte

The number of bytes in a word determines the word length of the computer:

32-bit machines 64-bit machines etc.

23

Binary and Computers

Page 4: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary Representations

One bit can be either 0 or 1. Therefore, one bit can represent only two things.

To represent more than two things, we need multiple bits. Two bits can represent four things because there are four combinations of 0 and 1 that can be made from two bits: 00, 01, 10,11.

Page 5: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary Representations (Cont’d)

If we want to represent more than four things, we need more than two bits. Three bits can represent eight things because there are eight combinations of 0 and 1 that can be made from three bits.

Page 6: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary & Alphanumeric Codes

A binary code is a group of n bits that assume up to 2n distinct combinations of 1’s and 0’s with each combination representing one element of the set that is being coded- i.e. permutations

With two bits we can form a set of four elements

With three bits we can represent 8 elements With four bits we can represent 16 elements

Page 7: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary Representations (Cont’d)

Figure 3.4 Bit combinations

Page 8: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary Representations (Cont’d)

In general, bits can represent 2 things because there are 2 combinations of 0 and 1 that can be made from n bits. Note that every time we increase the number of bits by 1, we double the number of things we can represent.

Page 9: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Memory Units

1 nibble 1 byte 1 word 1 long word 1 quad word 1 octa-word

4 consecutive bits 8 consecutive bits 2 consecutive bytes 4 consecutive bytes 8 consecutive bytes 16 consecutive

bytes

Page 10: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Larger Units of Memory

1 Kilobyte 1 Megabyte 1 Gigabyte 1 Terabyte 1 Petabyte 1 Exabyte

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

Page 11: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Data

The computer knows the type of data stored in a particular location from the context in which the data are being used; i.e. individual bytes, a word, a longword, etc 01100011 01100101 01000100 01000000

Bytes: 99(10, 101 (10, 68 (10, 64(10

Two byte words: 24,445 (10 and 17,472 (10 Longword: 1,667,580,992 (10

Page 12: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

2

Natural NumbersZero and any number obtained by repeatedly adding one to it.

Examples: 100, 0, 45645, 32

Negative NumbersA value less than 0, with a – sign

Examples: -24, -1, -45645, -32

Numbers

Page 13: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

3

IntegersA natural number, a negative number, zero

Examples: 249, 0, - 45645, - 32

Rational NumbersAn integer or the quotient of two integers

Examples: -249, -1, 0, ¼ , - ½

Numbers (Cont’d)

Page 14: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

4

How many ones are there in 642?

600 + 40 + 2 ?

Or is it

384 + 32 + 2 ? -- Octal

Or maybe…

1536 + 64 + 2 ? -- Hexadecimal

Natural Numbers

Page 15: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

5

642 is 600 + 40 + 2 in BASE 10

The base of a number determines the number of digits and the value of digit positions

Natural Numbers

Page 16: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

6

Continuing with our example…642 in base 10 positional notation is:

6 x 10² = 6 x 100 = 600

+ 4 x 10¹ = 4 x 10 = 40 + 2 x 10º = 2 x 1 = 2 = 642 in base 10

This number is in base 10

The power indicates the position of

the number

Positional Notation

Page 17: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

7

 dn * Rn-1 + dn-1 * R

n-2 + ... + d2 * R + d1

As a formula:

642 is:  63 * 102 +  42 * 10 +  21

R is the base of the number

n is the number of digits in the number

d is the digit in the ith position

in the number

Positional Notation

Page 18: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

68

What if 642 has the base of 13?

642 in base 13 is equivalent to 1068 in base 10

+ 6 x 13² = 6 x 169 = 1014 + 4 x 13¹ = 4 x 13 = 52 + 2 x 13º = 2 x 1 = 2

= 1068 in base 10

Positional Notation

Page 19: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

9

Decimal is base 10 and has 10 digits: 0,1,2,3,4,5,6,7,8,9

Binary is base 2 and has 2 digits:

0,1For a number to exist in a given number system, the number system must include those digits. For example:The number 284 only exists in base 9 and higher.

Binary

Page 20: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

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

All systems we use in computing are positional systems 495 = 400 + 90 +5

Page 21: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Number Systems

We use the DECIMAL (10 system

Computers use BINARY (2 or some shorthand for it like OCTAL (8 or HEXADECIMAL (16

Page 22: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary Octal Decimal

000 0 0

001 1 1

010 2 2

011 3 3

100 4 4

101 5 5

110 6 6

111 7 7

100 10 8

1001 11 9

1010 12 10 16

Power of 2 Number System

Page 23: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

10

How are digits in bases higher than 10 represented?

Base 16:0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F

Bases Higher than 10

Page 24: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

ConversionsDecimal Binary Octal Hex

0 0000 0 01 0001 1 12 0010 2 23 0011 3 34 0100 4 45 0101 5 56 0110 6 67 0111 7 78 1000 10 89 1001 11 910 1010 12 A11 1011 13 B12 1100 14 C13 1101 15 D14 1110 16 E

Page 25: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Decimal Equivalents

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• Note: 20, by definition is 1

2. Use each superscript as an exponent of a power of 2

3. Multiply the value of each bit by its corresponding power of 2

4. Add the products obtained

Page 26: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

What is the decimal equivalent of the octal number 642?

6 x 8² = 6 x 64 = 384 + 4 x 8¹ = 4 x 8 = 32 + 2 x 8º = 2 x 1 = 2

= 418 in base 10

11

Converting Octal to Decimal

Page 27: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

What is the decimal equivalent of the hexadecimal number DEF?

D x 16² = 13 x 256 = 3328 + E x 16¹ = 14 x 16 = 224 + F x 16º = 15 x 1 = 15

= 3567 in base 10

Remember, base 16 is 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Converting Hexadecimal to Decimal

Page 28: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

What is the decimal equivalent of the binary number 1101110?

1 x 26 = 1 x 64 = 64 + 1 x 25 = 1 x 32 = 32 + 0 x 24 = 0 x 16 = 0 + 1 x 23 = 1 x 8 = 8 + 1 x 22 = 1 x 4 = 4

+ 1 x 21 = 1 x 2 = 2 + 0 x 2º = 0 x 1 = 0

= 112 in base 10

13

Converting Binary to Decimal

Page 29: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Horner’s Method

Another procedure to calculate the decimal equivalent of a binary number Note: This method works with any base

Horner’s Method: Step 1: Start with the first digit on the left Step 2: Multiply it by the base Step 3: Add the next digit Step 4: Multiply the sum by the base Step 5: Continue the process until you add

the last digit

Page 30: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

• Groups of Three (from right)• Convert each group

10101011 10 101 011 2 5 3

10101011 is 253 in base 8

17

Converting Binary to Octal

Page 31: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Binary to Hex

Step 1: Form four-bit groups beginning from the rightmost bit of the binary number 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

0110011110101010100111 becomes 0001 1001 1110 1010 1010 0111

Step 2: Replace each four-bit group by its hexadecimal equivalent 19EAA7(16

Page 32: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Converting Decimal to Other Bases

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 Know as modulus arithmetic

Step 4: Verify the result by multiplying it out

Page 33: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

222 13 0 16 3567 16 222 16 13

32 16 0 36 62 13 32 48 47 14 32 15

D E F

21

Converting Decimal to Hexadecimal

Page 34: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Addition & Subtraction Terms

A + B A is the augend B is the addend

C – D C is the minuend D is the subtrahend

Page 35: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Addition Rules – All Bases

Addition Step 1: Add a column of numbers Step 2: Determine if there is a

single symbol for the result 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

Page 36: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Addition of Binary Numbers

Rules for adding or subtracting very similar to the ones in decimal system Limited to only two digits

0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 carry 1

Page 37: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Remember: there are only 2 digits in binary: 0 and 1

Position is key, carry values are used:

Carry Values 1 1 1 1 1 1 1 0 1 0 1 1 1 +1 0 0 1 0 1 1

1 0 1 0 0 0 1 0

14

Arithmetic in Binary

Page 38: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Number Overflow

Overflow occurs when the value that we compute cannot fit into the number of bits we have allocated for the result. For example, if each value is stored using eight bits, adding 127 to 3 overflow:

01111111+ 00000011 10000010

Overflow is a classic example of the type of problems we encounter by mapping an infinite world onto a finite machine.

Page 39: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Subtraction Rules – All Bases

Step1: 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 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

Page 40: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Remember borrowing? Apply that concept here:

1 2 2 0 2

1 0 1 0 1 1 1 - 1 1 1 0 1 1

0 0 1 1 1 0 0

15

Subtracting Binary Numbers

Page 41: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Addition & Subtraction of Hex

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

Page 42: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Real Numbers

Real numbers have a whole part and a fractional part. For example 104.32, 0.999999, 357.0, and 3.14159 the digits represent values according to their position, and those position values are relative to the base.

The positions to the right of the decimal point are the tenths position (10-1 or one tenth), the hundredths position (10-2 or one hundredth), etc.

Page 43: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Real Numbers (Cont’d)

In binary, the same rules apply but the base value is 2. Since we are not working in base 10, the decimal point is referred to as a radix point.

The positions to the right of the radix point in binary are the halves position (2-1 or one half), the quarters position (2-2 or one quarter), etc.

Page 44: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Real Numbers (Cont’d)

A real value in base 10 can be defined by the following formula:

The representation is called floating point because the number of digits is fixed but the radix point floats.

Page 45: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Real Numbers (Cont’d)

Likewise, a binary floating –point value is defined by the following formula:sign * mantissa * 2exp

Page 46: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Real Numbers (Cont’d)

Scientific notation is a term with which you may already be familiar, so we mention it here. Scientific notation is a form of floating-point representation in which the decimal point is kept to the right of the leftmost digit.

For example, 12001.32708 would be written as 1.200132708E+4 in scientific notation.

Page 47: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Text

To represent a text document in digital form, we simply need to be able to represent every possible character that may appear.

There are finite number of characters to represent. So the general approach for representing characters is to list them all and assign each a binary string.

A character set is simply a list of characters and the codes used to represent each one. By agreeing to use a particular character set, computer manufacturers have made the processing of text data easier.

Page 48: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Alphanumeric Codes

American Standard Code for Information Interchange (ASCII) 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 6C 6C 6F 20 57 6F 72 6C 64

Extended Binary Coded Decimal Interchange Code (EBCDIC)

Page 49: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

The ASCII Character Set

ASCII stands for American Standard Code for Information Interchange. The ASCII character set originally used seven bits to represent each character, allowing for 128 unique characters.

Later ASCII evolved so that all eight bits were used which allows for 256 characters.

Page 50: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

The ASCII Character Set (Cont’d)

Page 51: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

The ASCII Character Set (Cont’d)

Note that the first 32 characters in the ASCII character chart do not have a simple character representation that you could print to the screen.

Page 52: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

The Unicode Character Set

The extended version of the ASCII character set is not enough for international use.

The Unicode character set uses 16 bits per character. Therefore, the Unicode character set can represent 216, or over 65 thousand, characters.

Unicode was designed to be a superset of ASCII. That is, the first 256 characters in the Unicode character set correspond exactly to the extended ASCII character set.

Page 53: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

The Unicode Character Set (Cont’d)

A few characters in the Unicode character set

Page 54: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Signed Numbers

Remember, all numeric data is represented inside the computer as 1s and 0s 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 Conventions

Sign-magnitude Ten's complement Two’s complement One’s complement

Page 55: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negatives

It is necessary to choose one of the bits of the “basic unit” as a sign bit 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 used for negative numbers

Page 56: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Comparing the Conventions

Bit Pattern UnsignedSign-

Magnitude1's

Complement2's

Complement000 0 0 0 0001 1 1 1 1010 2 2 2 2011 3 3 3 3100 4 -0 -3 -4101 5 -1 -2 -3110 6 -2 -1 -2111 7 -3 -0 -1

Page 57: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values

You have used the signed-magnitude representation of numbers since grade school. The sign represents the ordering, and the digits represent the magnitude of the number.

Page 58: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values (Cont’d)

There is a problem with the sign-magnitude representation: There are two representations of zero. There is plus zero and minus zero.

Two representations of zero within a computer can cause unnecessary complexity.

Page 59: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Sign-Magnitude

For a basic unit of N bits, the leftmost bit is used exclusively to represent the sign

The remaining (N-1) bits are used for the magnitude

The range of number represented in this convention is –2 N+1 to +2 N-1 -1

Page 60: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Sign-magnitude Operations

Addition of two numbers in sign-magnitude is carried out using the usual conventions of binary arithmetic 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

Page 61: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Other Ways of Representing Negative

Values If we allow only a fixed number of values, we

can represent numbers as just integer values, where half of them represent negative numbers.

For example, if the maximum number of decimal digits we can represent is two, we can let 1 through 49 be the positive numbers 1 through 49 and let 50 through 99 represent the negative numbers -50 through -1.

Page 62: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values (Cont’d)

To perform addition within this scheme, you just add the numbers together and discard any carry.

Page 63: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values (Cont’d)

A-B=A+(-B). We can subtract one number from another by adding the negative of the second to the first.

Page 64: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values (Cont’d)

There is a formula that you can use to compute the negative representation:

This representation of negative numbers is called the ten’s complement.

Page 65: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values (Cont’d)

Two’s Complement: To make it easier to look at long binary numbers, we make the number line vertical.

Page 66: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Representing Negative Values (Cont’d)

Addition and subtraction are accomplished the same way as in 10’s complement arithmetic:

-127 10000001+ 1 00000001-126 10000010

Notice that with this representation, the leftmost bit in a negative number is always a 1.

Page 67: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Two’s Complement Convention

A positive number is represented using a procedure similar to sign-magnitude

To express a negative number1. Express the absolute value of the number in binary2. Change all the zeros to ones and all the ones to zeros

(called “complementing the bits”)3. 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

Page 68: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Two’s Complement Operations

Addition: Treat the numbers as unsigned integers

The sign bit is treated as any other number Ignore any carry on the leftmost position

Subtraction 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

Page 69: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

Overflows in Two’s Complement

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

If the operands are positive, the result is negative If the operands are negative, the result is positive

Page 70: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

One’s Complement

Devised to make the addition of two numbers with different signs the same as two numbers with the same sign

Positive numbers are represented in the usual way

For negatives STEP 1: Start with the binary

representation of the absolute value STEP 2: Complement all of its bits

Page 71: Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or

One's Complement Operations

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