+ 3
Why is it so?
12 Antworten
+ 3
It works on bitwise operator,
Jusg check the post below you will understand it , otherwise there are lessons available on these on this app
https://www.sololearn.com/discuss/186546/?ref=app
https://www.sololearn.com/discuss/1422037/?ref=app
https://www.sololearn.com/discuss/157184/?ref=app
https://www.sololearn.com/discuss/1358560/?ref=app
+ 3
Yes, you are right but it works on same principle let's suppose that the 8 bits are in circular form so it just shifts two value left or right as it does in other cases
+ 3
Okkk, right shift, shifts values to right side by two and left shifts , shifts values to left side by two units , that. '1 ' s are shifted in both the cases
+ 3
I>> 1 is
I=11000000
That's fine
But just
I<<1 should be
I=00000011
+ 3
okkk , it is something exception for me too , sorry for all above
+ 3
Raj Kalash Tiwari, in the C standard, left shift fills in vacated bits with 0. Right shift behavior depends on a couple factors.
Right shift of an UNSIGNED quantity fills vacated bits with 0.
Right shift of a SIGNED quantity is machine dependent. Some machines do an arithmetic shift (i.e. divide by 2) and retain the sign while others do a logical shift and fill in with 0.
Aside: In machine language rotate instructions (not shift), at least some (if not all) machines use the carry bit to shift into or out of the word as filler. But empirically I have been unable to make C do that low level behavior.
+ 2
As far as I'm aware....doing a right or left shift on a positive int introduces a '0' and bits are lost from the other end....but doing a right shift on a negative int introduces a '1' so as to preserve the sign bit.
0
Raj Kalash Tiwari Did you take a look at my code before referring me to these?
0
The thing what I did not understand is actually about the difference between right shift and left shift operator.
0
What I say is, consider this example
i = 1 0 0 0 0 0 0 1;
i = i<<1;
i = 0 0 0 0 0 0 1 0;
But,
i = i>>1;
i = 1 1 0 0 0 0 0 0;
0
Yea but it is not, that is what i was trying to point you out. When you run the code you'll see that,
i << 1 is not 00000011
Instead it is,
00000010
- 1
ei