Skip navigation

Tag Archives: chapter 2

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




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


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

Fill in the table below showing the effects of the different shift operations
on single-byte quantities. The best way to think about shift oeprations
is to work with binary representations. Convert the initial values to binary, 
perform shifts, and then convert back to hexadecimal. Each of the answers 
should be 8 binary digits or 2 hexadecimal digits.



-------------------------------------------------------------------------
       x                x << 2           x >> 2
                                        (Logical)       (Arithmetic)
-------------------------------------------------------------------------
  Hex     Binary    Hex     Binary    Hex     Binary    Hex     Binary
-------------------------------------------------------------------------
  0xF0
-------------------------------------------------------------------------
  0x0F
-------------------------------------------------------------------------
  0xCC
-------------------------------------------------------------------------
  0x55
-------------------------------------------------------------------------


0xF0

  0xF0
  11110000

  x << 2 (Logical)

  0x3C
  00111100

  x >> 2 (Arithmetic)

  0xFC
  11111100

0x0F

  0x0F
  00001111

  x << 2 (Logical)

  0x03
  00000011

  x >> 2 (Arithmetic)

  0x03
  00000011


0xCC

  0xCC
  11001100

  x << 2 (Logical)

  0x33
  00110011

  x >> 2 (Arithmetic)

  0xF3
  11110011

0x55

  0x55
  01010101

  x << 2 (Logical)

  0x15
  00010101

  x >> 2 (Arithmetic)

  0x15
  00010101

-------------------------------------------------------------------------
       x                x << 2           x >> 2
                                        (Logical)       (Arithmetic)
-------------------------------------------------------------------------
  Hex     Binary    Hex     Binary    Hex     Binary    Hex     Binary
-------------------------------------------------------------------------
  0xF0   11110000  0x80    10000000  0x3C    00111100  0xFC   11111100
-------------------------------------------------------------------------
  0x0F   00001111  0x78    01111000  0x03    00000011  0x03   00000011
-------------------------------------------------------------------------
  0xCC   11001100  0x60    01100000  0x33    00110011  0xF3   11110011
-------------------------------------------------------------------------
  0x55   01010101  0xA8    10101000  0x15    00010101  0x15   00010101
-------------------------------------------------------------------------


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.14 (this one was kind of fun!):

Using only bit-level and logical operations, write a C expression that
is equivalent to x == y. In other words, it will return 1 when x and y
are equal and 0 otherwise.


  bit operations:

  | with 1:   | with 0:
  1 | 1 = 1   1 | 0 = 1
  0 | 1 = 1   0 | 0 = 0

  & with 1:   & with 0:
  1 & 1 = 1   1 & 0 = 0
  0 & 1 = 0   1 & 0 = 0

  ^ with 1:   ^ with 0:
  1 ^ 1 = 0   1 ^ 0 = 1
  0 ^ 1 = 1   0 ^ 0 = 0

  !(x ^ y) should work

different:
  x = 1001001010
  y = 0010010111

      1001001010
   ^  0010010111
  --------------
      1011010101
     !1011010101 = 0x00


equal:
  x = 1000010101
  y = 1000010101

      1000010101
  ^   1000010101
  --------------
      0000000000
     !0000000000 = 0x01

Of course those aren’t bytes, too many bits. Just used some random amount of bits while thinking about it.

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

Suppose that x and y have byte values 0x66 and 0x93, respectively. Fill in the
following table indicating the byte values of the different C expressions:

Expression  Value  Expression   Value
-------------------------------------
  x & y              x && y
-------------------------------------
  x | y              x || y
-------------------------------------
 ~x | ~y            !x || !y
-------------------------------------
  x | !y             x && ~y
-------------------------------------


x & y

    0x66
 &  0x93
 -------

   01100110
 & 10010011
 ----------
   00000010

   0x02

x && y = 0x01

x | y

    0x66
 |  0x93
 -------

   01100110
 | 10010011
 ----------
   11110111

   0xF7

x || y = 0x01

~x | ~y

    ~0x66
 |  ~0x93
 --------

   ~01100110
 | ~10010011
 -----------

   10011001
 | 01101100
 ----------
   11111101

   0xFD

!x || !y = 0x00

x & !y

    0x66
 &  0x01
 -------

   01100110
 & 00000001
 ----------
   00000000

   0x00

 x && ~y

      0x66
 &&  ~0x93
 ---------

      0x66
 &&  ~0x93
 ---------

     01100110
 && ~10010011
 ------------


    01100110
 && 01101100
 -----------

      0x66
 &&   0x6C
 ---------
      0x01


Expression  Value  Expression   Value
-------------------------------------
  x & y      0x02    x && y      0x01
-------------------------------------
  x | y      0xF7    x || y      0x01
-------------------------------------
 ~x | ~y     0xFD    !x || !y    0x00
-------------------------------------
  x | !y     0x00     x && ~y    0x01
-------------------------------------

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

`bis` is a bit set operation and `bic` is a bit clear operation. Both
instructions take a data word x and a mask word m. They generate a
result y consisting of the bits of x modified according to the
bits of m. With `bis`, the modification involves setting z to 1 at
each bit position where m is 1. With `bic`, the modification
involves setting z to 0 at each bit position where m is 1.

Fill in the missing expressions in the following code:


/* Bit Set */
int bis ( int x, int m ) {
  /* Write an expression in C that computes the effect of bit set */
  int result = ______________;
  return result;
}


/* Bit Clear */
int bic ( int x, int m ) {
  /* Write an expression in C that computes the effect of bit clear */
  int result = ______________;
  return result;
}

Boolean logic operations rundown:

  & with 1:    & with 0:
  1 & 1 = 1;   1 & 0 = 0;
  0 & 1 = 0;   0 & 0 = 0;

  | with 1:    | with 0:
  1 | 1 = 1    1 | 0 = 1
  1 | 0 = 1    0 | 0 = 0

  ^ with 1:    ^ with 0:
  1 ^ 1 = 0    1 ^ 0 = 1
  0 ^ 1 = 1    0 ^ 0 = 0

Solution:

for bis (sets result to 1 for all 1's in m):

  int result = x | m;

for bic (sets result to 0 for all 1's in m):

  int result = x & ~m;


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

Write C expressions for the following values, with the result for x = 0x98FDECBA
and a w 32-bit word size shown in square brackets:

A. The least signficant byte of x, with all other bits set to 1 [0xFFFFFFBA].
B. The complement of the least signficant byte of x, with all other bytes left
   unchanged [0x98FDEC45].
C. All but the least signficant byte of x, with the least signficant byte set to
   0 [0x98FDEC00].

Although our examples assume a 32-bit word size, your code should work for
any word size w >= 8.


A.
      0x98FDECBA
          ?
    ------------
      0xFFFFFFBA

      0x98FDECBA
    |      ~0xFF
    ------------
      0xFFFFFFBA

    x | ~0xFF = 0xFFFFFFBA

B.

      0x98FDECBA
          ?
    ------------
      0x98FDEC45

      complement:
      0^1 = 1
      1^1 = 0

      unchanged:
      1^0 = 1
      0^0 = 0

      0x98FDECBA
    ^       0xFF
    ------------
      0x98FDEC45

    x ^ 0xFF = 0x98FDEC45

C.

      0x98FDECBA
          ?
    ------------
      0x98FDEC00

      possible operations:

      | with 0:
      1|0 = 1 <-- want 0's so this doesn't work

      | with 1;
      1|1 = 1 <-- nope
      0|1 = 1 <-- nope

      & with 1:
      1&1 = 1 <-- no
      0&1 = 0
      & with 1 leaves existing values in place

      & with 0:
      1&0 = 0
      0&0 = 0
      ^^^ that's it, no matter what input, & with 0 will flip bits to 0

      ^ with 1:
      1^1 = 0
      0^1 = 1 <-- no

      ^ with 0:
      1^0 = 1 <-- no
      0^0 = 0

      0x98FDECBA
    &      ~0xFF
    ------------
      0x98FDEC00


  x & ~0xFF = 0x98FDEC00

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

To show how the ring properties of ^ can be useful, consider the following
program:
void implace_swap( int *x, int *y ) {
  *x = *x ^ *y; /* Step 1 */
  *y = *x ^ *y; /* Step 2 */
  *x = *x ^ *y; /* Step 3 */
}
Starting with values a and b in the locations pointed to by x and y,
respectively, fill in the table that follows giving the values stored
at the two locations after each step of the procedure. Use the ring
properties to show that the desired effect is achieved. Recall that
every element is its own additive inverse (that is, a ^ a = 0).


Step           *x                  *y
------------------------------------------------
Initially       a                   b
------------------------------------------------
Step 1         a^b                  b
------------------------------------------------
Step 2         a^b            (a^b)^b=(b^b)^a=a
------------------------------------------------
Step 3    (a^b)^a=(a^a)^b=b         a
------------------------------------------------