Question

In: Computer Science

please explain how does the following C code work. a) int function1(int num1, int num2) {...

please explain how does the following C code work.

a)

int function1(int num1, int num2) {
int num = num1 ^ num2;
int result = 0;
while(num > 0) {
result += (num & 1);
num >>= 1;
}
return result;
}

b)

int function2(unsigned int num) {
return num & ~(num - 1);
}

c)

int f1(unsigned int x) {
int count = 0;
while(x) {
count++; x = x&(x-1);
}
return count;
}

d) double ldexp(double x, int exponent) is a C math function that returns x multiplied
by 2 raised to the power of exponent. How many narrowing and how many promotions
happen automatically in the following code line? Name them one by one and explain each
one separately.
float f = 2.2f * ldexp(24.4, 3.0 * 2) - 4.4;

e)

int main() {
int a[] = {1, 2, 3, 4, 5};
int *b = a; int **c = &b;
printf("%d\n", **c*2);
printf("%d\n", **c-2*4);
printf("%d\n", **c+1-*b+1);
return 0;
}

Thanks!

Solutions

Expert Solution

a)

int function1(int num1, int num2) {
int num = num1 ^ num2; //so ^ sign means Bitwise XOR between num1 and num2 and stored in num
int result = 0;
while(num > 0) { //this loop will till num is greater than zero
result += (num & 1); // now here num is done Bitwise AND with 1 means basically it checks the last bit is 1 or 0 //and result is keeping count of 1
num >>= 1; //this shifts the num right by 1
}

return result;
}

//So basically this code counts the no. of 1's present in num which XOR of num1 and num2

****************************************************************************************************************

b.

int function2(unsigned int num) {
return num & ~(num - 1); //So here basically num in done Bitwise AND with complement of num-1
} //for exmaple if num = 9 return value will 1001 & 0111 therefore it will 0001

*******************************************************************************************************************

c.

int f1(unsigned int x) {
int count = 0;
while(x) {
count++;

x = x&(x-1); //Here x is done Bitwise AND with x-1 and saving it in x again and keeping the count of it
}
return count;
}


Related Solutions

Q4. Please write a function Calculate_integer_division(). Your function should accept two parameters num1 and num2 and...
Q4. Please write a function Calculate_integer_division(). Your function should accept two parameters num1 and num2 and return the result of integer division of num1 by num2. So if num1 is 9 and num2 is 2, the function should return 4. Call your function and print out the result. Q5. Please print out the following with the help of a range() function: 200, 400, 600, 800 Q6. Please print 'Go.' if the traffic light is green, 'Wait.' if it's yellow and...
Write a code fragment in C language that tests the value of an integer num1. If...
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This function inserts a vehicle into the specified file. • Determine the RBN using the driver's hash function. • Use readRec to read the record at that RBN. • If that location doesn't exist or the record at that location has a szVehicleId[0] == '\0': o Write this new vehicle record at that location using writeRec. • If that record exists and that vehicle's szVehicleId...
Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define...
Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define all timers as unsigned variables unsigned long timer1 = 0; // timer1 increments every 100ms = 0.1s const int USER_LED_OUTPUT = 13; const int USER_BUTTON_INPUT = 2; void setup() { // initialize the digital pin as an output. pinMode(USER_LED_OUTPUT, OUTPUT); pinMode(USER_BUTTON_INPUT, INPUT); Serial.begin(9600); } void loop() { // run loop forever static int state, old_state,counter; timers(); // logic for state change if(state == 0)...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT