Which code segment completes this C code?

In summary, the conversation involves a student discussing a test they took in which they had to deduce missing code segments in C programs. The student struggled with part a) but ultimately found the correct solution through trial and error. The teacher mentioned that the solution could have been seen without testing through knowledge of bitwise operators. The student was unable to solve part b) and the correct solution was c). The student discusses their thought process and reasoning for each possible answer. They also mention a typo in part b) and discuss potential errors in the code for part a).
  • #1
diredragon
323
15

Homework Statement


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.
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;
   //   nm1 = (a & ~mask) | (b & mask);
   //    nm2 = (b & ~mask) | (a & mask);

   //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;
   //   mask <<= x;
   //   nm1 = (b | ~mask) & (a | mask);
   //    nm2 = (a | ~mask) & (b | mask);
  
}
Part b)
Which code segment is correct in terms of defined declarations?
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);
}


Homework Equations


3. The Attempt at a Solution [/B]
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?
 
Physics news on Phys.org
  • #2
diredragon said:
Can you see it without trying and could you elaborate on what you saw?
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.
diredragon said:
The others seem fine.
How do you shift something by a double number of bits, especially if that double doesn't have an assigned value yet?
 
  • #3
mfb said:
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?
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
mask = 111100
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..

mfb said:
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?

Yes that was a typo. Let's see a) for example:
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) :
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) :
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
diredragon said:
mask |= 1 << 3 which is 100
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.
diredragon said:
and says me nothing about what this code does..
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?
diredragon said:
This gives me an error saying have double an int, i thought this was possible :/. Can you not use % when there are different types?
You can check it step by step. Does ++x work? Does x%b work?
diredragon said:
even though it includes a double leaves b as an int so nothing is wrong here.
The value of (b+=y) matters here. What is the value of "(b+=y)"? You can print it for example.
 
  • #5
mfb said:
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?
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.

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 can't have double%int, is it so?
mfb said:
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?
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
diredragon said:
The value of (b+=y) = 6. Why is it important?
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).
diredragon said:
++x does work and is 4.0000 while x%b does not work since we can't have double%int, is it so?
Did you test it?
diredragon said:
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.
Right.
 
  • #7
mfb said:
Did you test it?Right.

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
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.
 
  • Like
Likes diredragon

Related to Which code segment completes this C code?

1. What is the purpose of "Which code segment completes this C code?"

The purpose of this question is to test the understanding and knowledge of the syntax and structure of the C language, as well as problem-solving skills. It presents a partial code and asks the programmer to complete it in order to achieve a specific task or functionality.

2. How do I approach completing the code segment?

When completing a code segment, it is important to carefully read and understand the given code and what it is trying to achieve. Then, use your knowledge of C syntax and logic to fill in the missing parts and ensure that the code is functional and efficient.

3. Are there any specific rules or guidelines to follow when completing the code segment?

There are no specific rules or guidelines, but it is important to maintain consistency with the existing code and follow best practices. This includes proper indentation, appropriate use of data types, and clear and concise code.

4. Is it possible to have multiple correct solutions to complete the code segment?

Yes, there can be multiple ways to complete a code segment, depending on the problem and the programmer's approach. However, it is important to choose a solution that is efficient and follows best practices.

5. Can I use external resources or references to help me complete the code segment?

It depends on the context of the question. In a test or exam setting, it is usually not allowed to use external resources. However, in a real-world scenario, it is perfectly acceptable to use references or seek help from colleagues or online communities.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
925
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
Back
Top