right shifting

C

crookie

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 gcc compiler gives me a warning.)
 
W

Walter Roberson

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 gcc compiler gives me a warning.)

Because the standard says so.

C89 3.3.7 Bitwise Shift Operators

If the value of the right operand is negative or is greater than
or equal to the width in bits of the promoted left operand, the
behaviour is undefined.
 
A

Andrew Poelstra

if I have the foolowing code,

"foolowing" is right: this is undefined, and you risk anything happening,
including random numbers, nasal demons, and Chris singing at you.
 
K

Keith Thompson

Because the standard says so.

C89 3.3.7 Bitwise Shift Operators

If the value of the right operand is negative or is greater than
or equal to the width in bits of the promoted left operand, the
behaviour is undefined.

It's undefined if unsigned int is no wider than 32 bits.
 
R

RAKHE

crookie said:
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 gcc compiler gives me a warning.)


hi crookie
Try
like this u will find answer
main()
{
unsigned int i,d=34;
i=32>>34;
printf("answer:%d",i);
}
output::
answer : 0
 
K

Keith Thompson

RAKHE said:
Try
like this u will find answer
main()
{
unsigned int i,d=34;
i=32>>34;
printf("answer:%d",i);
}
output::
answer : 0

No, that doesn't answer anything. It's undefined behavior in both
cases (assuming unsigned int is 32 bits).
 
N

newbie

Keith said:
No, that doesn't answer anything. It's undefined behavior in both
cases (assuming unsigned int is 32 bits).

Keith is correct.

I think some people misunderstand the following statement:
C89 3.3.7 Bitwise Shift Operators

If the value of the right operand is negative or is greater than
or equal to the width in bits of the promoted left operand, the
behaviour is undefined.

As given above. It does not mean that the number of bits that make up
the right operand must be the less than the number of bits that make up
the left operand, but that the value of the right operand must be less
than the number of bits that make up the left operand.

Since, in the code above, Rakhe has assumed unsigned int to be 32 bits
the above code is undefined as the value of the right operand is 34.
 
C

Chris Dollin

Andrew said:
"foolowing" is right: this is undefined, and you risk anything happening,
including random numbers, nasal demons, and Chris singing at you.

Self: "Somewhere deep inside a dream there's a song that I can siiin-g"

Other (bad paraphrase from memory): "Doesn't sound like it."
 
C

Chris Dollin

Andrew said:
"foolowing" is right: this is undefined, and you risk anything happening,
including random numbers, nasal demons, and Chris singing at you.

Um, there are multiple Chris's in this newsgroup, and I don't think
you should saddle the rest of them with my singing unreputation.
"Chris Dollin" or "Chris D" or "CJD" [1] or "Hedgehog Chris" or
something.

[1] Not only do I sing badly, I can rot your brain.
 
E

Eric Sosman

Chris said:
Andrew Poelstra wrote:

"foolowing" is right: this is undefined, and you risk anything happening,
including random numbers, nasal demons, and Chris singing at you.


Um, there are multiple Chris's in this newsgroup, and I don't think
you should saddle the rest of them with my singing unreputation.
"Chris Dollin" or "Chris D" or "CJD" [1] or "Hedgehog Chris" or
something.

[1] Not only do I sing badly, I can rot your brain.

You probably sing in C#.
 
D

Dik T. Winter

>
> No, that doesn't answer anything. It's undefined behavior in both
> cases (assuming unsigned int is 32 bits).

Indeed. Also I have here a box where the answer is 8.
 
P

pete

Dik said:
Indeed. Also I have here a box where the answer is 8.

This is what I get from new.c:

answer:0
answer:8

/* BEGIN new.c */

#include<stdio.h>

int main(void)
{
unsigned int i, d = 34;

i = 32 >> 34;
printf("answer:%d\n", i);
i = 32 >> d;
printf("answer:%d\n", i);
return 0;
}

/* END new.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top