Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this assignment is to give you practice using C bitwise and shift

ID: 3834422 • Letter: T

Question

The purpose of this assignment is to give you practice using C bitwise and shift operators. Your assignment is to write the following functions. The body of each function should consist of only 1 C statement. The statement may not be an if statement, switch statement, or a loop. You may only use the following C operators: =, * (dereferencing, not multiplication), & (bitwise and, not the address operator), |, ~, ^, <<, >>, and +. You may not need to use every operator. You are also restricted in the constants that you may use, as specified below for each problem.

The functions that you must write are described below. For each, its prototype is given, as well as a description of its proper behavior, and any limitations on the constants that you may use to write the function. Whenever a description refers to the nth bit of an integer, it refers to the bit which represents 2 n ; thus bit 0 is the rightmost (least significant) bit. The sign bit is bit 31.

gcc –o hw4 hw4_test.c hw4.c ./hw4

My test code calls each of your functions on 100 test cases, and reports the number of incorrect answers produced by each function.

Each function is passed a parameter xptr, which is a pointer to an integer. There may be second parameter for some of the functions. For the sake of brevity in my descriptions below, we will assume that xptr points to an integer variable named x, and the parameter yptr (in the last problem) points to an integer variable named y.

1. void zero(int *xptr);

This function sets x to 0. You are not allowed to use any constants.

2. void maximum_int(int *xptr);

This function sets x to the largest integer (2147483647). You may only use positive constants less than 32.

3. void minimum_int(int *xptr);

This function sets x to the smallest integer (-2147483648). You may only use positive constants less than 32.

4. void flip_bit(int *xptr, int n);

This function “flips” the nth bit of x. In other words, if the nth bit of x is 1, then it is changed to 0, and vice versa. In this function, the only constant you may use is 1 (and of course n, since it is a variable).

For example:

int x = 12, y = 100;

flip_bit(&x, 3);

flip_bit(&y, 4) printf("%d %d ", x, y);

The output is 4 116.

5. void zero_bit(int *xptr, int n);

This function sets the nth bit of x to 0. Again, we will refer to the n th bit of an integer as the bit which represents 2n ; thus bit 0 is the rightmost (least significant) bit. The sign bit is bit 31. In this function, the only constant you may use is 0 (and of course n, since it is a variable).

For example:

int x = 12, y = 100, z = 6;

zero_bit(&x, 2);

zero_bit(&y, 5);

zero_bit(&z, 0);

printf("%d %d ", x, y);

The output is 8 68 6

6. void get_bit(int *xptr, int n);

This function sets x to either 0 or 1, depending on the value of the nth bit of x. You are not allowed to use any constants (you may use n, since it is a variable).

For example:

int x = 22, y = 35;

get_bit(&x, 3);

get_bit(&y, 5);

printf("%d %d ", x, y);

The output is 0 1.

7. void negative(int *xptr);

This function sets x to –x. The only constant you may use is 1.

For example:

int x = 12;

negative(&x);

printf("%d ", x);

The output is -12.

8. subtract(int *xptr, int *yptr);

This function performs the equivalent of x -= y. The only constant you may use is 1. For example:

int x = 1, y=2;

subtract(&x, &y);

printf("%d %d ", x, y);

x = 6; subtract(&x, &y);

printf("%d %d ", x, y);

The output is -1 2 4 2.

Explanation / Answer

void zero(int *xptr){
  
   *xptr=0;
  
}

void maximum_int(int *xptr){
  
   const unsigned int max=2147483647;
   *xptr=max;
  
}

void minimum_int(int *xptr){
   const unsigned int max=2147483647;
   *xptr=-(max+1);
}

void flip_bit(int *xptr, int n){
  
   *xptr=*xptr^(1<<n);
}

void zero_bit(int *xptr, int n){
  
   *xptr=*xptr&~(1<<n);
}

void get_bit(int *xptr, int n){
   if(*xptr&(1<<n)){
       *xptr=1;
   }else{
       *xptr=0;
   }
  
}

void negative(int *xptr){
   *xptr=*xptr*-1;
}

void subtract(int *xptr, int *yptr){
   *xptr=*xptr-*yptr;
}