f



right shifting

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.)

0
noob1 (3)
11/9/2006 11:25:04 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

12 Replies
356 Views

Similar Articles

[PageSpeed] 53

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
0
roberson2 (8602)
11/9/2006 11:35:27 PM
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;
> }
>
0
apoelstra (387)
11/9/2006 11:53:51 PM
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.
0
kst-u (21963)
11/10/2006 12:02:46 AM
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.)


0
noob1 (3)
11/10/2006 12:47:19 AM
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

0
11/10/2006 4:24:20 AM
"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.
0
kst-u (21963)
11/10/2006 7:17:00 AM
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.

0
11/10/2006 9:05:53 AM
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/

0
chris.dollin (1683)
11/10/2006 9:42:29 AM
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/

0
chris.dollin (1683)
11/10/2006 10:00:22 AM
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

0
esosman (1384)
11/10/2006 1:09:21 PM
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/
0
Dik.Winter (1625)
11/10/2006 1:49:22 PM
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
0
pfiland (6613)
11/11/2006 3:46:56 AM
Reply: