bit shifting doesn't work as expected

Ian Lance Taylor ian@airs.com
Sun Jul 31 22:11:00 GMT 2005


Constantin Greubel <constantin.greubel@physik.uni-muenchen.de> writes:

> I hope this is at least the correct address to ask such questions.
> I have the following small programm, and I don't understand why the
> number is feeded with ones from the right, but not with zeros. And the
> next question would be: How can I manage it to get zeros, instead of
> ones?

Your question would be easier to understand if you showed the output
of your program, and also showed the expected output.

However, I suspect that the answer to your question is that doing a
right shift of a variable with a signed type which happens to hold a
negative value is implementation defined.  If you want to reliably and
portably do a right shift of a value with the high bit set, use
'unsigned int' instead of 'int'.

Ian



More information about the Gcc-help mailing list