zero shift

Discussion in 'C++' started by Mike - EMAIL IGNORED, Mar 30, 2007.

  1. Given:
    unsigned x = 123;
    unsigned y = 0;
    unsigned a = x << y;
    unsigned b = x >> y;

    I would hope that: a == 123 && b == 123;

    but I don't see this in section 5.8 of ISO/IEC 14882.
    Is it true? Chapter & verse?

    Thanks,
    Mike.
     
    Mike - EMAIL IGNORED, Mar 30, 2007
    #1
    1. Advertising

  2. Mike - EMAIL IGNORED

    Kai-Uwe Bux Guest

    Mike - EMAIL IGNORED wrote:

    > Given:
    > unsigned x = 123;
    > unsigned y = 0;
    > unsigned a = x << y;
    > unsigned b = x >> y;
    >
    > I would hope that: a == 123 && b == 123;
    >
    > but I don't see this in section 5.8 of ISO/IEC 14882.
    > Is it true? Chapter & verse?


    I think[5.8] covers that case: the right operand is neither negative nor
    greater than or equal to the length of the promoted left operand. Thus,
    [5.8/1] does not say it's undefined.

    For x << y, now [5.8/2] kicks in. We are told that the result is x * 2^y.
    Since 2^0 = 1, we find a = x = 123.

    For x >> y, [5.8/3] says b = x / ( 2^y) = x /1 = 123.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 30, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,264
    Thomas Schodt
    Jan 27, 2004
  2. Wenjie
    Replies:
    3
    Views:
    1,102
    Ron Samuel Klatchko
    Jul 11, 2003
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,511
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,590
    Thomas Richter
    Apr 29, 2011
  5. devphylosoff

    what "shift" does, if not "$_ = shift;" ?

    devphylosoff, Nov 29, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    381
    Michele Dondi
    Dec 4, 2007
Loading...

Share This Page