HomeWork 3 - WordPress.com

advertisement

Exercise 3.1 a.)

A B

3174 0522 b.) 4165 1654

3.1.1 What is the sum of A and B if they represent unsigned 12-bit octal numbers? The result should

be written in octal. Show your work. a.) First is to convert the octal numbers base 8 into binary numbers.

A B

3

011

1

001

7

111

4

100

0

000

5

101

2

010

2

010

So in Binary A: 011 001 111 100 two

and B: is 000 101 010 010 two

A 011 001 111 100

+B

=

000

011

101

111

010

001

010

110

The binary sum of A and B is 011 111 001 110 two

, in octal it is 3716. b.) We’ll do the same thing as in a)

A B

4

100

1

001

6

110

5

101

1

001

6

110

5

101

So the binary representation of A: 100 001 110 101 two

and B: 001 110 101 100 two

.

A 100 001 110 101

+B

=

001

110

110

000

101

100

100

001

4

100

The binary sum of A and B is 110 000 100 001 two

, in octal it is 6041.

To check this and every problem I just used the calculator on my computer and set it to programmer.

3.1.2 What is the sum of A and B if they represent signed 12-bit octal numbers stored in sign-

magnitude format? The result should be written in octal. Show your work.

“In computer science , the sign bit is a bit in a signed number representation that indicates the sign of a number. Only signed numeric data types have a sign bit, and its place is usually the leftmost, where the most significant bit in unsigned numbers resides. Floating point numbers in IEEE format are always signed, with the sign bit in the leftmost position. Typically if the sign bit is 1 then the number is negative (in the case of two’s complement integers) or non-positive (for ones’ complement integers, sign-and-magnitude integers, and floating point numbers), while 0 indicates a non-negative number” ( http://en.wikipedia.org/wiki/Sign_bit ).

We are going to convert the octal numbers into binary as in 3.1.1 a.)

A B

3

011

1

001

7

111

4

100

0

000

5

101

2

010

2

010

Since the most significant bit is 0 for both A and B, we get the same value for signed numbers. The binary representation of A: 011 001 111 100 two

and B: 000 101 010 010 two

.

A 011 001 111 100

+B

=

000

011

101

111

010

001

010

110

The binary sum of A and B is 011 111 001 110 two

, in octal it is 3716. b.) We’ll start off the same way as above by converting the octal into binary.

A B

4

100

1

001

6

110

5

101

1

001

6

110

5

101

4

100

In A the most significant bit is 1, so it is negative. Leave the most significant bit which is the 1, which leaves you with:

00 001 110 101

Now you want to take the 2′s complement, simply switch every 0 to 1 and every 1 to 0, and add one to the result. You will get:

A’2

11 110 001

Take that and add 1 to it:

11 110 001

11 110 001

010

010

+1

011

B is the same as before, since the number is positive.

B

1

001

6

110

5

101

4

100

The binary representation of A is 11 110 001 011 two

, and B is 001 110 101 100 two

. The sum of A and B is:

A’2 11 110 001 011

+B 001 110 101 100

= 01 100 110 111

01 100 110 111 two

= 1467 octal.

3.1.4 What is A – B if they represent unsigned 12-bit octal numbers? The result should be written in

octal. Show your work.

A B a.) b.)

7040

4365

0444

3412 a.) The first step is the same as 3.1.1 and 3.1.2, convert to binary:

A B

7

111

0

000

4

100

0

000

0

000

4

100

A – B:

A

-B

=

111

000

110

000

100

011

100

100

111

110 011 111 100 two

, which is 6374 octal. b.) Again convert the octal into binary:

000

100

100

4

100

4

100

A

4

100

3

011

6

110

5

101

B

3

011

4

100

1

001

2

010

A – B:

A

-B

=

110

011

000

011

100

111

110

001

101

101

010

011

= 000 111 101 011_two, which is 0753 octal.

3.1.5 What is A- B if they represent signed 12-bit octal numbers stored in sign-magnitude format? The

result should be written in octal. Show your work. a.) Since A is negative you are going to do steps similar to problem 3.1.2, drop the most significant bit giving you 11 000 100 000 two

, then take the 2′s complement (simply switch every 0 to 1 and every 1 to 0) and then add one to the result. The table below shows the steps:

A

7 0 4 0

111 000 100 000

Drop the most significant bit

11 000 100

Switch 0′s and 1′s

000

00 111 011

2′s complement

111

00 111 100 000

B is the same as before, since the number is positive.

B

0 4 4 4

000 100

Now do A – B:

A 00

-B

=

000

00

100

111

100

010

100

100

100

111

000

100

100

00 010 111 100 two

is 0274 octal. b.) Since A is negative you are going to do steps similar to problem 3.1.2, drop the most significant bit giving you 00 011 110 101 two

, then take the 2′s complement (simply switch every 0 to 1 and every 1 to 0) and then add one to the result. The table below shows the steps:

A

4

100

3

011

6

110

5

101

Drop the most significant bit

00 011 110

Switch 0′s and 1′s

11 100 001

2′s complement

101

010

11 100 001 011

B is the same as before, since the number is positive.

B

3 4 1 2

011

A – B

A

100 001 010

-B

=

11

011

00

100

100

000

00 000 000 001 two

is 0001 octal.

Exercise 3.2

A B

001

001

000

011

010

001 a. b.

1446

2460

672F

4935

3.2.1 What is the sum of A and B if they represent unsigned 16-bit hexadecimal numbers? The result should be written in hexadecimal. Show your work.

Since the left most bit is the sign bit in the binary number, then the following rule can be used with any binary number in order to figure out either it is a positive number, or a negative number: 1)If the most left bit is 0, and then the binary number is a positive number. 2) If the most left bit is 1, then the binary number is a negative number.

“Hexadecimal is a positional numeral system with a radix , or base, of 16. It uses sixteen distinct symbols, most often the symbols 09 to represent values zero to nine, and A, B, C, D, E, F (or alternatively af) to represent values ten to fifteen.” (http://en.wikipedia.org/wiki/Hexadecimal) a.) The first step is to convert from hexadecimal to binary.

A B

1

0001

A + B

A

4

0100

0001

4

0100

0100

6

0110

0100 0110

6

0110

7

0111

2

0010

F

1111

+B

=

0110

0111

0111

1011

0010

0111

1111

0101

Now you want to convert 0111 1011 0111 0101 back into hexadecimal.

0111 1011 0111 0101

7 B 7 5

A + B = 7B75 b.) Again convert the hexadecimal numbers into binary.

A B

2

0010

A + B

A

+B

4

0100

0010

0100

6

0110

0100

1001

0

0000

0110

0011

0000

0101

4

0100

9

1001

3

0011

5

0101

= 0110 1101 1001 0101

Now take 0110 1101 1001 0101 and convert it back into hexadecimal.

0110 1101 1001 0101

6 D 9 5

A + B = 6D95

3.2.2 What is the sum of A and B if they represent signed 16-bit hexadecimal numbers stored in sign-

magnitude format? The result should be written in hexadecimal. Show your work

A B a. 1446 672F b. 2460 4935 a.)The first step is to convert from hexadecimal to binary.

A B

1 4 4 6 6 7 2 F

0001 0100 0100 0110 0110 0111 0010 1111

Both A and B have zero at the most left bit, so they are positive numbers. Since both A and B are positive numbers, then the sum of A+B in singed 12-bits will be similar in unsigned 16 -bits.

A in binary

B

0001

0110

0100

0111

0100

0010

0110

1111

A+B (binary) 0111 1011 0111 0101

A+B = 7b75 b.)The first step is to convert from hexadecimal to binary.

A B

2

0010

4

0100

6

0110

0

0000

4

0100

9

1001

3

0011

5

0101

Both A and B have zero at the most left bit, so they are positive numbers. Since both A and B are positive numbers, then the sum of A+B in singed 12-bits will be similar in unsigned 16 -bits.

A in binary

B

0010

0100

0100

1001

0110

0011

0000

0101

0110 1101 1001 0101 A+B (binary)

A+B= 6d95

3.2.3 Convert A into a decimal number, assuming it is unsigned.

Repeat assuming it stored in sign-magnitude format. Show your work.

A a. b.

1446

2460

Unsigned Format: a.) The value of A which is 1446 (base 16) that can be represented in a binary form (base 2) by

A 1 4 4 6

0001 0100 0100 then converting the binary number to decimal number:

0110

(0* 2^0) + (1* 2^1)+(1* 2^2)+(0* 2^3)+(0* 2^4)+(0* 2^5)+(1* 2^6)+(0* 2^7)+(0* 2^8)+(0* 2^9)+(1*

2^10)+(0* 2^11) +(1* 2^12)+(0* 2^13)+(0* 2^14)+(0* 2^15) = 2+4+64+1024++4096 =

5190 (base 10). b.)The value of A is 2460 (base 16) which can be represented in a binary form (base 2) by

A 2 4 6 0

0010 0100 0110 0000

(0* 2^0) + (0* 2^1)+(0* 2^2)+(0* 2^3)+(0* 2^4)+(1* 2^5)+(1* 2^6)+(0* 2^7)+(0* 2^8)+(0* 2^9)+(1*

2^10)+(0* 2^11)+(0* 2^12)+(1* 2^13)+(0* 2^14)+(0* 2^15) = 32+64+1024+8192 =

9312 (base 10).

Sign-Magnitude Format: a.)The value of A which is 1446 (base 16) that can be represented in a binary form (base 2) by

A 1 4 4 6

0001 0100 0100 0110 then converting the binary number to decimal number by the following way:

(0* 2^0) + (1* 2^1)+(1* 2^2)+(0* 2^3)+(0* 2^4)+(0* 2^5)+(1* 2^6)+(0* 2^7)+(0* 2^8)+(0* 2^9)+(1*

2^10)+(0* 2^11) +(1* 2^12)+(0* 2^13)+(0* 2^14)+(0*(- 2^15)) = 2+4+64+1024++4096 =

5190 (base 10). b.)

The value of A is 2460 (base 16) which can be represented in a binary form (base 2) by

A 2 4 6 0

0010 0100 0110 0000

(0* 2^0) + (0* 2^1)+(0* 2^2)+(0* 2^3)+(0* 2^4)+(1* 2^5)+(1* 2^6)+(0* 2^7)+(0* 2^8)+(0* 2^9)+(1*

2^10)+(0* 2^11)+(0* 2^12)+(1* 2^13)+(0* 2^14)+(0* *(-2^15)) = 32+64+1024+8192 =

9312 (base 10).

3.2.4 What is A – B if they represent unsigned 16-bit hexadecimal numbers? The result should be

written in hexadecimal. Show your work.

A B a. b.

C352

5ED4

36AE

07A4 a.) The first step is to convert from hexadecimal to binary. Then subtract A to B

A C 3 5 2 B 3 6

1100

1100

0011

0011

0101

0101

0010

0010

A E

0011 0110 1010 1110

A-B =

0011

1000

0110

1100

1010

1010

1110

0100

A-B= 8CA4 b.) The first step is to convert from hexadecimal to binary. Then subtract A to B

A 5 E D 4 B 0 7

0101

0101

1110

1110

1101

1101

0100

0100

A 4

0000 0111 1010 0100

A-B =

0000

0101

0111

0111

1010

0011

0100

0000

A-B=5730

3.2.5 What is A – B if they represent signed 16-bit hexadecimal numbers stored in sign-magnitude

format? The result should be written in hexadecimal. Show your work.

A B a. b.

C352

5ED4

36AE

07A4 a.) The first step is to convert from hexadecimal to binary

A C 3 5 2 B 3 6 A E

1100 0011 0101 0010 0011 0110 1010 1110

Since A is negative you are going to take the 2′s complement (simply switch every 0 to 1 and every 1 to

0) and then add one to the result

0011 1100 1010 1101

0011 1100 1010

+1

1110

B is the same as before, since the number is positive.

A

-

B

0011

0011

1100

0110

1010

1010

1110

1110

A-B =

A-B=0600

0000 0110 0000 0000 b.) The first step is to convert from hexadecimal to binary

A 5 E D 4 B 0

0101 1110 1101 0100 0000

7

0111

A

1010

4

0100

Both A and B have zero at the most left bit, so they are positive numbers. Since both A and B are positive numbers, then the sum of A-B in singed 12-bits will be similar in unsigned 16 -bits.

A

-

B

A-B =

0101

0000

0101

1110

0111

0111

1101

1010

0011

0100

0100

0000

A-B= 5730

3.2.6 Convert A into a binary number. What makes base 16 (hexadecimal) an attractive numbering

system for representing values in computers?

A a. b.

C352

5ED4 a.)The value of A which is C352 (base 16) that can be represented in a binary form (base 2) by

A C 3 5 2

1100 0011 0101 then converting the binary number to decimal number:

0010

(0* 2^0) + (1* 2^1)+(0* 2^2)+(0* 2^3)+(1* 2^4)+(0* 2^5)+(1* 2^6)+(0* 2^7)+(1* 2^8)+(1* 2^9)+(0*

2^10)+(0* 2^11) +(0* 2^12)+(0* 2^13)+(1* 2^14)+(1* 2^15)

= 50002(base 10). b.)The value of A is 5ED4 (base 16) which can be represented in a binary form (base 2) by

A 5

0101

E

1110

D

1101

4

0100

(0* 2^0) + (0* 2^1)+(1* 2^2)+(0* 2^3)+(1* 2^4)+(0* 2^5)+(1* 2^6)+(1* 2^7)+(0* 2^8)+(1* 2^9)+(1*

2^10)+(1* 2^11)+(1* 2^12)+(0* 2^13)+(1* 2^14)+(0* 2^15)

= 4+16+64+128+512+1024+2048+4096+16384

= 24276(base 10).

Exercise 3.3

Overflow occurs when a result is too large to be represented accurately given a finite word size. Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow by many, but in this textbook, that is considered an overflow.) The following table shows pairs of decimal numbers.

A B a. 216 255 b. 185 122

A

3.3.1 Assume A and B are unsigned 8-bit decimal integers. Calculate

A – B. Is there overflow, underflow, or neither? a.) The first step is to convert from decimal to binary, then subtract

A 216 B 255

-B

=

11011000

11011000

1111111

1100111

1111111

A

-B

=

A

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow by many, but in this textbook, that is considered an overflow. " (given by the book)

There is underflow b.) The first step is to convert from decimal to binary, then subtract

185

10111001

B 122

1111010

10111001

1111010

00111111

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow " (given by the book)

There is underflow, but in this textbook, it is considered as an overflow. Because two negative numbers give a positive number.

3.3.2 Assume A and B are signed 8-bit decimal integers stored in sign magnitude format. Calculate A + B. Is there overflow, underflow, or neither?

A B a. 216 255 b. 185 122 a.) The first step is to convert from decimal to binary, then Add A to B

A 216

11011000

B 255

1111111

A

+B

=

11011000

1111111

101010111

A

-B

=

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow by many, but in this textbook, that is considered an overflow. " (given by the book)

There is underflow b.) The first step is to convert from decimal to binary, then subtract

A 185

10111001

B 122

01111010

10111001

01111010

100110011

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow " (given by the book)

There is overflow

3.3.3 Assume A and B are signed 8-bit decimal integers stored in sign magnitude

format. Calculate A – B. Is there overflow, underflow, or neither?

A B a. 216 b. 185

255

122

A

-B

= a.) The first step is to convert from decimal to binary, then Add A to B

A 216

11011000

B 255

01111111

11011000

01111111

101010111

A

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow by many, but in this textbook, that is considered an overflow. ”(given by the book)

There is neither overflow, nor underflow b.) The first step is to convert from decimal to binary, then subtract

A 185 B 122

-B

=

10111001

10111001

01111010

00111111

01111010

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow " (given by the book)

There is neither overflow, nor underflow

3.3.4 Assume A and B are signed 8-bit decimal integers stored in two’s complement format. Calculate A + B using saturating arithmetic. The result should be written in decimal. Show your work.

A B a. 15 139 b. 151 214 a.) The first step is to convert from decimal to binary, Then take the 2’s complement and Add 1,

A 15

00001111

B 139

10001011

2’s complement 11110000

+ 1 11110001

01110100

01110101

Then A + B

A

+B

11110001

01110101

= 01100110

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow" (given by the book)

There is neither overflow, nor underflow b.) The first step is to convert from decimal to binary, Then take the 2’s complement and Add 1,

A 151 B 214

10010111

2’s complement 01101000

+ 1 01101001

11010110

00101001

00101010

Then A + B

A

+B

01101001

00101010

= 10010010

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow "(given by the book)

There is overflow because the sum of two positive numbers gives a negative number.

3.3.5 Assume A and B are signed 8-bit decimal integers stored in two’s complement format. Calculate A – B using saturating arithmetic. The result should be written in decimal. Show your work. a.) The first step is to convert from decimal to binary, Then take the 2’s complement and Add 1,

A 15 B 139

A

-B

=

00001111

2’s complement 11110000

+ 1 11110001

Then A - B

11110001

01110101

01111100

10001011

01110100

01110101

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow" (given by the book)

There is overflow

b.) The first step is to convert from decimal to binary, Then take the 2’s complement and Add 1,

A 151

10010111

B 214

11010110

2’s complement 01101000

+ 1

Then A - B

01101001

00101001

00101010

A

-B

=

01101001

00101010

00111110

"Underflow occurs when a number is too small to be represented correctly—a negative result when doing unsigned arithmetic, for example. (The case when a positive result is generated by the addition of two negative integers is also referred to as underflow "(given by the book)

There is underflow

Download