Skip navigation

Tag Archives: overflow addition

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.30 (my answer differs in form, but I believe is still correct, from the answer in the answer section:

Write a function with the following prototype:

/* Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y);

This function should return 1 if arguments x and y can be added without causing
overflow

Answer:

int tadd_ok(int x, int y) {

  int sum;

  sum = x + y;
  if (sum >= 0 && x < 0 && y < 0) {
    return 0;
  }
  if (sum  0 && y > 0) {
    return 0;
  }
  return 1;

}

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.29:

Fill in the following table in the style of Figure 2.24. Give the integer value of
the 5-bit arguments, the values of both their integer and two's-complement sums,
the bit-level representation of the two's-complment sum, and the case from the
derivation of Equation 2.14.

   x         y        x + y    x+t5y        Case
-----------------------------------------------------
[10100]   [10001]      -27      5             1
-----------------------------------------------------
[11000]   [11000]      -16     -16            2
-----------------------------------------------------
[10111]   [01000]       -1     -1             2
-----------------------------------------------------
[00010]   [00101]        7      7             3
-----------------------------------------------------
[01100]   [00100]       16     -16            4
-----------------------------------------------------

Work:

[10100]   [10001]

 -2^4 + 2^2
 -16 + 4
 -12

 10001
 -16 + 1
 -15

 -12 + -15 = -27

Possible negative max: -16
possible positive max: 15

2^w = 2^5 = 32

-27 + 32 = 5

[11000]   [11000]

-2^4 + 2^3 =
-16 + 8 = -8

-8 + -8 = -16
within range



[10111]   [01000]

-2^4 + 2^2 + 2^1 + 2^0 =
-16 + 4 + 2 + 1
-9

8
-1

[00010]   [00101]

2 + 5 = 7

[01100]   [00100]

12 + 4 = 16

Possible positive max: 15

16 in binary:
-0 + 16
[010000]
take of extra bit:
[10000]
-2^4
-16

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.28:

We can represent a bit pattern of length w = 4 with a single hex digit. For an
unsigned interpretation of these digits, use Equation 2.12 to fill in the following
table giving the values and the bit representations (in hex) of the unsigned additive
inverses of the digits shown.

      x                   -u/4(x)
-------------------------------------
  Hex   Decimal       Decimal   Hex
-------------------------------------
   0
-------------------------------------
   5
-------------------------------------
   8
-------------------------------------
   D
-------------------------------------
   F
-------------------------------------


Work:

hex: 5
decimal: 5

2^w = 16
-u/4(x) = (2^w - x)
16 - 5 = 11

hex: 8
decimal: 8
16-8 = 8


Answers:

      x                   -u/4(x)
-------------------------------------
  Hex   Decimal       Decimal   Hex
-------------------------------------
   0       0             0       0
-------------------------------------
   5       5            11       B
-------------------------------------
   8       8             8       8
-------------------------------------
   D      13             3       3
-------------------------------------
   F      15             1       1
-------------------------------------