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

You need to determine what each function is doing For each function, can you wri

ID: 3748589 • Letter: Y

Question

 You need to determine what each function is doing 
 For each function, can you write a simpler equivalent function in C (using any C operators) that produces the same result.  By simpler, we are interested in fewer operations in C but also more readable code
 int ques(int x) {      int mask1, mask2, mask4, mask8, mask16;      mask2 = 0x33 + (0x33 << 8);     mask2 += mask2 << 16;     mask1 = mask2 ^ (mask2 << 1);     mask4 = 0x0F + (0x0F << 8);     mask4 += mask4 << 16;     mask8 = 0xFF + (0xFF << 16);     mask16 = 0xFF + (0xFF << 8);      x = (x & mask1) + ((x >> 1) & mask1);     x = (x & mask2) + ((x >> 2) & mask2);     x = (x & mask4) + ((x >> 4) & mask4);     x = (x & mask8) + ((x >> 8) & mask8);     x = (x & mask16) + ((x >> 16) & mask16);      return x; }

Explanation / Answer

Explanation: This function calculates number of ones in the given binary representation of x


If x = 1 , Output = 1 because binary representation of number 1 =0001 -> Number of ones is 1
If x = 2 , Output = 1 because binary representation of number 2= 0010 -> Number of ones is 1
If x = 3 , Output = 2 because binary representation of number 3 = 0011 -> Number of ones is 2


-----------------------------------------------------------
Simpler Implementation



Below function also does the same thing. It uses less number of operation unline the above



int ques(int x) {

int count = 0;

while (x != 0){

count++;

x = x&(x-1);

}

return count;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote