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.)
In article <1163114704.824290@sj-nntpcache-1.cisco.com>, crookie <noob@nairco.com> 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 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. -- All is vanity. -- Ecclesiastes
On 2006-11-09, crookie <noob@nairco.com> wrote: > 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. > main() > { > unsigned int i=34; > i>>=32; > } >
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes: > In article <1163114704.824290@sj-nntpcache-1.cisco.com>, > crookie <noob@nairco.com> 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 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. It's undefined if unsigned int is no wider than 32 bits. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
Thanks, that answered my question 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 gcc compiler gives me a warning.)
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 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
"RAKHE" <t.sathyanarayanan@gmail.com> writes: > 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 gcc compiler gives me a warning.) > > 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). -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote: > "RAKHE" <t.sathyanarayanan@gmail.com> writes: > > 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 gcc compiler gives me a warning.) > > > > 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). > > -- > Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> > We must do something. This is something. Therefore, we must do this. 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.
Andrew Poelstra wrote: > On 2006-11-09, crookie <noob@nairco.com> wrote: >> 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. 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." -- Chris "disassociated" Dollin "- born in the lab under strict supervision -", - Magenta, /Genetesis/
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. -- Chris ".enable proofreading" Dollin "Who do you serve, and who do you trust?" /Crusade/
Chris Dollin wrote: > 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#. -- Eric Sosman esosman@acm-dot-org.invalid
In article <lnodrf7ous.fsf@nuthaus.mib.org> Keith Thompson <kst-u@mib.org> writes: > "RAKHE" <t.sathyanarayanan@gmail.com> writes: .... > > 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). Indeed. Also I have here a box where the answer is 8. -- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131 home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Dik T. Winter wrote: > > In article <lnodrf7ous.fsf@nuthaus.mib.org> Keith Thompson <kst-u@mib.org> writes: > > "RAKHE" <t.sathyanarayanan@gmail.com> writes: > ... > > > 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). > > 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 */ -- pete