Help with C++ bit scrolling

Asked

Viewed 109 times

0

Well, in a programming exercise I was given the following program, and I asked what is your output

 using namespace std; 

 union U1 {    
    union U2{     
      unsigned a: 3;   
      unsigned b: 4;   
      unsigned c: 1;    
    }u2;    
    int d;    
    void Exibe(unsigned, unsigned, unsigned, int); 
}u1; 

int main(){    
   u1.u2.a=2;    
   u1.u2.a<<=1;    
   u1.u2.b=16;     
   u1.u2.b-=4;    
   u1.u2.c=(u1.u2.b)>>3;    
   u1.d=17; 
   cout << u1.u2.a << "   " << u1.u2.b << "   " << u1.u2.c << "   " << u1.d << endl; 
} 

After the execution of the code the output is '1 1 1 17' , my doubt is on why of this output, I researched on bit displacement and joining with the little that was given in class still could not understand, could help me to understand?

  • 4

    Did you execute the code? If yes, at least you have the output. Asking about the meaning/reason of the output is something quite different, there yes more valid. I’m sorry to comment, but the way the question looks like programming exercise + typing and compiling laziness. You are welcome here, but note that this site is not a forum. If not yet, do the [tour].

  • Yes, I ran, the output is '1 1 1 17' , is that I did not understand even what is the logic of this output, I researched about bit displacement but still I am lost. And yes, it is a question of a programming exercise where all the questions are more or less in this context, I asked for help on this to understand how to do.

  • Ok. Then edit the question, and add to it the information you gave me. Then I vote to reopen. ;)

1 answer

0

The bit offset moves the bits to the right or left and ends up meaning a multiplication or division by 2 of the value that was there, but it’s a little more complicated than that.

To help, we will follow your program line by line, showing the binary values:

int main(){    
   u1.u2.a=2;    
   u1.u2.a<<=1;    
   u1.u2.b=16;     
   u1.u2.b-=4;    
   u1.u2.c=(u1.u2.b)>>3;    
   u1.d=17; 
   cout << u1.u2.a << "   " << u1.u2.b << "   " << u1.u2.c << "   " << u1.d << endl; 
} 

1) The U1.U2.a variable in your case is started with the value 2 and has 3 bits.

a = 010

2) The variable is then shifted one bit to the left.

a = 100 (que é a mesma coisa que multiplicar por 2) e o valor vira 4

3) The variable b is started with 16 and has 4 bits. Which means that the entered value passes the value accepted by it... So this is already starting to give off the understanding of what is happening (note that this is a bug and should not be done).

16 = 10000 
b = 0

4) Subtracting 4 from b would leave the negative value, but it is unsigned, so you have another problem here, but the bits would be changed.

b = 1100

5) Then the bits of b are moved to c. 3 bits to the right.

b>>3 = 1
c = 1

6) Then when assigning 17 in d, all that was done is erased.

17 = 10001

However, since U2 is a Union, in doing so, you are also changing a, b and c:

a = 001 (três bits de 17)
b = 0001 (quatro bits de 17)
c = 1 (um bit de 17)
d = 17 (ele continua sendo um inteiro)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.