This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Bitwise shift operator on 8-byte integers
- From: Ankit Jain <ankitjain1580 at yahoo dot com>
- To: Sisyphus <kalinabears at iinet dot net dot au>
- Cc: gcc <gcc-help at gcc dot gnu dot org>
- Date: Wed, 6 Oct 2004 08:01:47 +0100 (BST)
- Subject: Re: Bitwise shift operator on 8-byte integers
thanks a lot
so its basically a problem of type casting
well i have 1 more ques
i have a signed 32bit integer var
var=1;
var<<=31;
var>>=31;
var displays -1 here
and after this if i do var>>=1;
it still diplays -1. i could not understand why on
right shift the sign bit is not changed to 0
whats happening here?
thanks
ankit
--- Sisyphus <kalinabears@iinet.net.au> wrote:
> Ankit Jain wrote:
> > hi Eljay,
> >
> > I understand the solution given by u and its
> workign
> > is also fine . but could not understand why this
> is
> > not working
> >
> > thanks
> >
> > ankit
> > --- Krzysztof.Wisniowski@siemens.com wrote:
> >
> >>Hallo *,
> >>Here's the problem:
> >>
> >>unsigned int ui = 4294967295; //2^16-1
>
> Actually, that's 2^32-1.
>
> >>unsigned long long uL; //8-byte variable
> >>
> >>uL = ui << 16;
>
> You're asking that a 32 bit value (ui) be left
> shifted 16 places. This
> means that the 16 high bits will be discarded. The
> result of that left
> shift will then be assigned to uL.
>
> I believe that another solution would be to write it
> as:
> uL = (unsigned long long)ui << 16;
>
> this casts ui to an unsigned long long - so we're
> now asking that a 64
> bit value be left shifted 16 places - and no bits
> will be lost.
> Consequently uL will contain the value you expect.
>
> Get the picture ?
>
> Cheers,
> Rob
>
>
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html