Skip navigation

Tag Archives: unsigned

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.27 (skipped 2.25 & 2.26 since they were qualitative and the answer is the back of the chapter):

Write a function with the following prototype:

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

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

Answer:

int uadd_ok (unsigned x, unsigned y) {

  return (x + y) >= x;

}

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.18:

Assuming the expressions are evaluated on a 32-bit machine that uses two's
complement arithmetic, fill in the following table describing the effect
of casting and relational operations, in the style of Figure 2.13:


Expression                                  Type      Evaluation
------------------------------------------------------------------
-2147483647-1 == 2147483648U                unsigned    true
------------------------------------------------------------------
-2147483647-1 < -2147483647                 signed       true
------------------------------------------------------------------
(unsigned) (-2147483647-1) < -2147483647    unsigned     true
------------------------------------------------------------------
-2147483647-1 < 2147483647                  signed       true
------------------------------------------------------------------
(unsigned) (-2147483647-1) < 2147483647     unsigned     false

when two's complement negative is converted to unsigned its value
inreases + 2w, would be much larger than when second value is
converted to unsigned

------------------------------------------------------------------

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.16:

Assuming w = 4, we can assign a numeric value to each possible hexadecimal
digit, assuming either an unsigned or two's-complement interpretation. Fill in
the following table according to these interpretations by writing out the nonzero
powers of two in the summations shown in Equation 2.1 and 2.2 (in the book pg 52).



  Hexadecimal  Binary     Unsigned         Signed
  -----------------------------------------------------
       A        1010   2^3 + 2^1 = 10  -2^3 + 2^1 = -6
  -----------------------------------------------------
       0
  -----------------------------------------------------
       3
  -----------------------------------------------------
       8
  -----------------------------------------------------
       C
  -----------------------------------------------------
       F
  -----------------------------------------------------


Answers:

  Hexadecimal  Binary                    Unsigned         Signed
  -------------------------------------------------------------------------------
       A        1010               2^3 + 2^1 = 10                 -2^3 + 2^1 = -6
  -------------------------------------------------------------------------------
       0        0000                       0 =  0                          0 =  0
  -------------------------------------------------------------------------------
       3        0011               2^2 + 2^0 =  3                  2^2 + 2^0 =  3
  -------------------------------------------------------------------------------
       8        1000                     2^3 =  8                       -2^3 = -8
  -------------------------------------------------------------------------------
       C        1100               2^3 + 2^2 = 12                 -2^3 + 2^2 = -4
  -------------------------------------------------------------------------------
       F        1111   2^3 + 2^2 + 2^1 + 2^0 = 15    -2^3 + 2^2 + 2^1 + 2^0 =  -1
  -------------------------------------------------------------------------------