Skip navigation

Tag Archives: two’s complement

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.33 (skipped publishing 2.31 & 2.32 as they were thought exercises):

We can represent a bit pattern of length w = 4 with a single hex digit. For a two's-
complement interpretation of these digits, fill in the following table to determine
the additive inverses of the digits shown

     x               -t 4 x
-------------------------------
Hex   Decimal   Decimal   Hex
-------------------------------
 0      0          0       0
-------------------------------
 5      5         -5       B
-------------------------------
 8     -8         -8       8
-------------------------------
 D     -3          3       3
-------------------------------
 F     -1          1       1
-------------------------------

Work:

 8 = 1000 = -8
 D = 1101 = -8 + 4 + 1 = -3

 -5
 -8 + 2 + 1 = 1011 = B




Advertisements

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

Suppose we truncate a 4-bit value (represented by hex digits 0 through F) to a 3-bit
value (represented as hex digits 0 through 7). Fill in the table below showing
the efffect of this truncation for some cases, in terms of the unsigned and two's
complement interpretations of those bit patterns.

        Hex                   Unsigned           Two's complment
---------------------- ----------------------- -------------------
Original    Truncated   Original    Truncated   Original Truncated
------------------------------------------------------------------
   0            0          0            0          0         0
------------------------------------------------------------------
   2            2          2            2          2         2
------------------------------------------------------------------
   9            1          9            1         -7         1
------------------------------------------------------------------
   B            3         11            3         -5         3
------------------------------------------------------------------
   F            7         15            7         -1        -1
------------------------------------------------------------------

WorK:


2U
0010
 010 = 2

2
0010
 010 = 2

9U
1001
 001 = 1
9 mod 8 = 1

-7 = -8 + 1
1001
 001 = 1
-7 mod 8 = 1
U2T(1) = 1

11U
1011
 011 = 3
11 mod 8 = 3

-5 = -8 + 4 + 1
1011
 011 = 3
-5 mod 8 = 3
U2T(3) = 3

15U
1111
 111 = 4 + 2 + 1 = 7
15 mod 8 = 7

-1 = -8 + 4 + 2 + 1
1111
 111 = -4 + 2 + 1 = -1
-1 mod 8 = 7
U2T(7) = -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.23:

Consider the following C functions:

int fun1(unsigned word) {
  return (int) ((word << 24);
}

int fun2(unsigned word) {
  return ((int) word << 24;
}

Assume these are executed on a machine with a 32-bit word size that uses two's
complement aritmetic. Assume also that right shifts of signed values are
performed arithmetically, while right shifts of unsigned values are performed
logically.

A. Fill in the following table showing the effect of these functions for several
example arguments. You will find it more convienient to work with a
hexidecimal representation. Just remember that hex digits 8 through F have their
most significant bits equal to 1.

     w             fun1(w)       fun2(w)
-----------------------------------------------
0x00000076
-----------------------------------------------
0x87654321
-----------------------------------------------
0x000000C9
-----------------------------------------------
0xEDCBA987
-----------------------------------------------

B. Describe in words the useful computation each of these functions performs.

Answers

A.

word = 0x00000076

fun2(w)

(int) ((word << 24)

word << 24)

Still unsigned so performing logical right shift (adding 0's).

return value is 0x00000076

fun1(w)

((int) word << 24;

((int) word << 24)

current value is now a signed 0x76000000

Most signficant bit for this value is a 0.

Return value after right shift:

0x00000076

word = 0x87654321

fun1()

value is 0x21000000 after first operation
value is unsigned
returned value is 0x00000021

fun2()
value is 0x21000000 after first operation
value is signed
first signficant bit of 2 is 0
returned value is 0x00000021

word = 0x000000C9

fun1()
value is 0xC9000000 after first operation
value is unsigned
return value is 0x000000C9

fun2()
value is 0xC9000000 after first operation
value is signed
first signficant bit of C is 1
return value is 0xFFFFFFC9

word = 0xEDCBA987

fun1()
value is 0x87000000 after first operation
returned value is 0x00000087

fun2()
value is 0x87000000 after first operation
returned value is 0xFFFFFF87

     w             fun1(w)       fun2(w)
-----------------------------------------------
0x00000076       0x00000076     0x00000076
       118              118            118
-----------------------------------------------
0x87654321       0x00000021     0x00000021
2271560481               33             33
-----------------------------------------------
0x000000C9       0x000000C9     0xFFFFFFC9
       201              201            -55
-----------------------------------------------
0xEDCBA987       0x00000087     0xFFFFFF87
3989547399             135            -121
-----------------------------------------------

B)
fun1 reduces a number to its lower 8 bits and produces a value of 0-255
fun2 reduces a number the same but produces a value of -128-127

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

Show that each of the following bit vectors is a two's complement representation
of -5 by applying Equation 2.3

A. [1011]
B. [11011]
C. [111011]

Observe that the second and third bit vectors can be derived from the first by sign
extension.

Answers:

A.
 1011
 -(1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0)
 -8 + 0 + 2 + 1
 -8 + 3
 -5

B.
 11011
 -(1 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0)
 -16 + 8 + 0 + 2 + 1
 -8 + 3
 -5

c.
 111011
 -(1 * 2^5) + (1 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0)
 -32 + 16 + 8 + 0 + 2 + 1
 -16 + 8 + 3
 -8 + 3
 -5



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

Explain how Equation 2.4 applies to the entires in the table
you generated when solving Problem 2.18

You simply add 2^w where w = number of bits to convert to
the two's complement value to convert to unsigned if two's
complement of x is a negative value (most signficant
bit is flipped).


  x   T2U₄(x)
 -------------
 -8     8       1000    2^3        = 2^4 + -2^3
                          8        = 16 - 8

 -------------
 -6    10       1010    2^3 + 2^1  = x + -2^3 + 2^1
                           10      = x + -6
                           16      = x
                           2^4 = 2 * 2 * 2 * 2 = 16

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

 -------------
 -1    15       1111   2^3 + 2^2 + 2^1 + 2^0 = 2^4 + -2^3 + 2^2 + 2^1 + 2^0
                        8  +  4  +  2  +  1  =  16 +  -8  +  4  +  2  +  1
                          12     +     3     =     8      +       7
                                 15          =         15
 -------------
  0     0
 -------------
  3     4
 -------------




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:

Using the table you filled in when solving Problem 2.16, fill in the
following table describing the function T2U₄:


  x   T2U₄(x)
 -------------
 -8
 -------------
 -6
 -------------
 -4
 -------------
 -1
 -------------
  0
 -------------
  3
 -------------


(from 2.16):

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


  x   T2U₄(x)
 -------------
 -8     8
 -------------
 -6    10 
 -------------
 -4    12 
 -------------
 -1    15 
 -------------
  0     0 
 -------------
  3     4
 -------------



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.17: – this one was a lot of work :( – still fun to get it right though :P

For the lines labeled A-K in the following listing, convert the
hexadecimal values (two's complement) shown to the right of the instruction names
(not shown here, check your book, too much too type!) into their
decimal equivalents.

A. 0x184
B. 0x8
C. 0xc
D. 0x10
E. 0xfffffe94
F. 0x10
G. 0xfffffea0
H. 0xffffff10
I. 0x1c
J. 0xffffff7c
K. 0x18

A. 0x184

     1    8    4
  0001 1000 0100
  1098 7654 3210

     2^8 + 2^7 + 2^2 = 388

B. 0x8

   0x8 is 0x08 (hex binary values must have two digits to get 8 bits)

     0     8
  0000  1000 = 8


C. 0xc

   0xc = 0x0c (hex binary values must have two digits to get 8 bits)

     0    c
  0000 1100 =
       3210
       2^3 + 2^2 = 12

D. 0x10

     1    0
  0001 0000 = 2^4 = 16

E. 0xfffffe94

     f    f    f    f    f    e    9    4
  1111 1111 1111 1111 1111 1110 1001 0100
          0         0 5432 1098 7654 3210
  -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^7 + 2^4 + 2^3 = -364


F. 0x10

     1    0
  0001 0000 = 16

G. 0xfffffea0

     f    f    f    f    f    e    a    0
  1111 1111 1111 1111 1111 1110 1010 0000
          0         0 5432 1098 7654 3210
  -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^7 + 2^5 = -352

H. 0xffffff10

     f    f    f    f    f    f    1    0
  1111 1111 1111 1111 1111 1111 0001 0000
          0         0 5432 1098 7654 3210
   -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^8 + 2^4 = -240

I. 0x1c

     1    c
  0001 1100
     16 + 8 + 4 = 28

J. 0xffffff7c

     f    f    f    f    f    f    7    c
  1111 1111 1111 1111 1111 1111 0111 1100
          0         0 5432 1098 7654 3210
  -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^8 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 = -132

K. 0x18

     1    8
  0001 1000
     16 + 8 = 24

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