Which code segment completes this C code?

Click For Summary
SUMMARY

The forum discussion centers on completing C code segments for bit manipulation and variable declarations. For part a), the correct code segment is option a), which uses a loop to create a mask for swapping bits between two unsigned integers. The solution involves using bitwise operators effectively to manipulate the values of variables a and b. In part b), the correct declaration is option c), which correctly handles type compatibility between an integer and a character's ASCII value. The discussion emphasizes understanding bitwise operations and type handling in C programming.

PREREQUISITES
  • C programming language fundamentals
  • Bitwise operators in C
  • Variable type declarations and type compatibility
  • Understanding of masking techniques in bit manipulation
NEXT STEPS
  • Study C programming bitwise operations in detail
  • Learn about variable type conversions and type safety in C
  • Explore advanced masking techniques for bit manipulation
  • Practice writing and debugging C code involving bitwise operations
USEFUL FOR

C programmers, computer science students, and software developers looking to deepen their understanding of bit manipulation and type handling in C.

diredragon
Messages
321
Reaction score
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
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?
 
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?
 
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.
 
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.
 
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.
 
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.
 
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   Reactions: diredragon

Similar threads

Replies
7
Views
2K
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
9
Views
2K
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K