Skip navigation

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;


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: