# Homework Help: Which code segment completes this C code?

Tags:
1. Jun 3, 2017

### diredragon

1. The problem statement, all variables and given/known data
This is a question from the test i had today in which we had to look at certain C code segments and deduce which code part is missing in order for the program to do what we desire. Part b) was a short questionnaire to test our knowledge about c types and declarations.
Part a)
Which code segment should replace  in order for this program to successfully exchange the group of bits from position x to y in numbers a and b.
Code (C):

#include <stdio.h>

int main() {
unsigned int a, b, x, y, i, nm1, nm2, mask = 0;
scanf("%u %u %u %u", &a, &b, &x, &y);
//##. What goes here?
a = nm1; b = nm2;
return 0;

//a) for (i = x; i <= y; i++)
//       mask |= 1 << i;

//b) for (i = x; i <= y; i++)
//       mask |= 1 << i;
//   nm1 = ((mask & (~a << x)^ ^ (mask | b));
//    nm2 = ((mask & (~b << x)) ^ (mask | a));

//c) mask = ( 1 << y) - 1;

}

Part b)
Which code segment is correct in terms of defined declarations?
Code (C):

#include <stdio.h>

int main() {
int a, b; const charc = 'a'; double x, y;

//a)      a = (++x % b) >> a;
//b)      y = (b % a--) >> x;
//c)      x = (a % c) >> (b += y);
}

2. Relevant equations
3. The attempt at a solution

Part a) i was able to do by brute force. I took one example of my own choice and went through each of the codes to see which worked and a) worked for me.That was the correct solution. Then after the class the teacher told me that this could have been seen without any testing but only with the knowledge of what these bitwise operators do when placed like this. Can you see it without trying and could you elaborate on what you saw?
Part b) i couldn't figure out. Turns out it was c) but i could see why. ( a % c ) was i thought a tricky part but i reasoned that c had an int value in ASCII codes even though it was a char. The others seem fine. How was this the correct answer? What was wrong with the others?

2. Jun 3, 2017

### Staff: Mentor

The computer cannot do anything you can't do. You can "run" the program with pen and paper, if necessary. How does mask look like in each case? What happens to example inputs a and b?

For part (b), I think there is a space missing in the declaration of c.
How do you shift something by a double number of bits, especially if that double doesn't have an assigned value yet?

3. Jun 4, 2017

### diredragon

For the answer a) (the correct one) of the part a)
For example let's say that my a = 1101110 and my b = 1000111
The mask is 0 and is being or'd with the shifted 1 y-x times. Since i know that
If i choose x = 2 and y = 5 then mask looks like this:
i = 2
mask |= 1 << 3 which is 100 and the mask is also 100 for the first time
i = 3
i do the same but now i OR 100|1000 which is 1100
i = 4
the right side is 10000 and mask = 11100.
i = 5
When i continue to the line nm1 i get :
(a & ~mask) | (b & mask); which is (1101110 & 000011) | (1000111 & 111100) which altogether gives nm1 = 000110 but this shouldn't be right and says me nothing about what this code does..

Yes that was a typo. Lets see a) for example:
Code (C):

#include "stdio.h"
int main(void) {
int a = 3, b = 2; double x = 3;

a = (++x % b) >> a;

printf("%d ", a);
return 0;
}

This gives me an error saying have double an int, i thought this was possible :/. Can you not use % when there are different types?
For b) :
Code (C):

#include "stdio.h"
int main(void) {
int a = 3, b = 2; double x = 3, y = 4;

y = (b % a--) >> x;

printf("%d ", a);
return 0;
}

This error says i can't shift an int by a double
But for the c) :
Code (C):

#include "stdio.h"
int main(void) {
int a = 3, b = 2; const char c = 'a'; double x = 3, y = 4;

x = (a % c) >> (b += y);

printf("%d ", a);
return 0;
}

This runs fine and i think i can see why. We use % between two ints, a a declared int and c which has int value and then shift it int numbers to the left because (b += y) even though it includes a double leaves b as an int so nothing is wrong here. Is my reasoning ok?

4. Jun 4, 2017

### Staff: Mentor

1 << 2?

Your mask has more digits - as many as an integer. If mask = 111100, it is actually 00...000111100, which means ~mask is 11...111000011.
Why not?
What is (b & 00...000111100), for example? Well, all digits outside the swapping range are zero, and inside this range it just takes the digits from b.
What is (a & 11...111000011), for example? Well, all digits inside the swapping range are zero, and outside this range it just takes the digits from a.
What happens if you take the bitwise or of these two numbers? What happens inside the swapping range, what happens outside?
You can check it step by step. Does ++x work? Does x%b work?
The value of (b+=y) matters here. What is the value of "(b+=y)"? You can print it for example.

5. Jun 4, 2017

### diredragon

The value of (b+=y) = 6. Why is it important? Since it's an int it can shift by 6 places. The left side of >> is fine so that's why this is correct.
++x does work and is 4.0000 while x%b does not work since we cant have double%int, is it so?
Hmm, i didn't realize that we have 111... in front since there were all zeroes there. Regarding your question when we or those two numbers it will take all from a from outside the range since b's are zero and reverse for the inside. Hence we swapped the two numbers from the range. I get it now.

6. Jun 4, 2017

### Staff: Mentor

Different programming languages handle that differently. Using the new value of b should be common, but some languages will just return if the assignment was successful (as boolean).
Did you test it?
Right.

7. Jun 4, 2017

### diredragon

I did test ++x separately and x%b. The latter returned an error saying something about double and int while the former returned 4.0000.

8. Jun 4, 2017

### Staff: Mentor

Good.

Things like that are programming language specific. It is not something you can figure out on our own, you have to see and learn what the programming language does with such an expression.