This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: bit shifting doesn't work as expected
- From: Ian Lance Taylor <ian at airs dot com>
- To: Constantin Greubel <constantin dot greubel at physik dot uni-muenchen dot de>
- Cc: gcc-help at gcc dot gnu dot org
- Date: 31 Jul 2005 15:11:37 -0700
- Subject: Re: bit shifting doesn't work as expected
- References: <42ED42A6.1030004@physik.uni-muenchen.de>
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