By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

right shifting

P: n/a
if I have the foolowing code,

main()
{
unsigned int i=34;
i>>=32;
}

why does the value of i remain 34? I thought it should be a zero.
(the g++ compiler gives me a warning about shifting >= size of variable.)
Nov 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
crookie wrote:
if I have the foolowing code,

main()
{
unsigned int i=34;
i>>=32;
}

why does the value of i remain 34? I thought it should be a zero.
(the g++ compiler gives me a warning about shifting >= size of variable.)
The standard [5.8/1] says about the shift operators:

[...] The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left operand.

It would appear that on your platform sizeof(int)<=32, whence you have
undefined behavior: anything could happen.
Best

Kai-Uwe Bux
Nov 9 '06 #2

P: n/a
crookie wrote:
if I have the foolowing code,

main()
{
unsigned int i=34;
i>>=32;
}

why does the value of i remain 34? I thought it should be a zero.
(the g++ compiler gives me a warning about shifting >= size of variable.)
Because on your system, an unsigned int has 32 or fewer bits, and (as I
recently learned) shifting by any number greater than or equal to the
number of bits is Undefined Behavior. Which means that any value of i
is equally valid.

For more detail, see this thread from over the summer:

http://tinyurl.com/y382v3

Best regards,

Tom

Nov 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.