is (variable >> 32) undefined?

Discussion in 'C++' started by Davis King, Sep 27, 2003.

  1. Davis King

    Davis King Guest

    I just discovered that sometimes on some compilers it is true that
    (variable >> 32) == variable where variable is an unsigned long and on
    these platforms an unsigned long is 32 bits. Is bit shifting a
    variable in the manner undefined by the standard or is this just a bug
    in the compiler?




    examples:
    in borland 5.5.1 the following code prints 1, I would expect it to be 0
    unsigned long low = 1;
    low >>= 32;
    cout << low;

    I also found a similar problem in visual studio but weather or not
    (low>>32) == low or 0 is apparently dependent upon what (seeming
    independent) statements precede the shift.
     
    Davis King, Sep 27, 2003
    #1
    1. Advertising

  2. Davis King wrote :

    > I just discovered that sometimes on some compilers it is true that
    > (variable >> 32) == variable where variable is an unsigned long and on
    > these platforms an unsigned long is 32 bits. Is bit shifting a
    > variable in the manner undefined by the standard or is this just a bug
    > in the compiler?


    I believe this is in compliance with the Standard. As I recall, both
    negative and too large positive (when value goes beyond the size of a
    variable) shifts produce undefined results.

    Nikolai Borissov
     
    Nikolai Borissov, Sep 28, 2003
    #2
    1. Advertising

  3. Davis King

    Jack Klein Guest

    On Sat, 27 Sep 2003 12:45:04 -0700, Davis King <>
    wrote in comp.lang.c++:

    > I just discovered that sometimes on some compilers it is true that
    > (variable >> 32) == variable where variable is an unsigned long and on
    > these platforms an unsigned long is 32 bits. Is bit shifting a
    > variable in the manner undefined by the standard or is this just a bug
    > in the compiler?
    >
    >
    >
    >
    > examples:
    > in borland 5.5.1 the following code prints 1, I would expect it to be 0
    > unsigned long low = 1;
    > low >>= 32;
    > cout << low;
    >
    > I also found a similar problem in visual studio but weather or not
    > (low>>32) == low or 0 is apparently dependent upon what (seeming
    > independent) statements precede the shift.


    If the shift count for an integer type is negative or greater than or
    equal to the number of bits, the result is undefined. The compiler is
    absolutely correct.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 28, 2003
    #3
  4. Davis King

    Ron Natalie Guest

    "Davis King" <> wrote in message news:bl4paj$86h$-state.edu...
    > I just discovered that sometimes on some compilers it is true that
    > (variable >> 32) == variable where variable is an unsigned long and on
    > these platforms an unsigned long is 32 bits. Is bit shifting a
    > variable in the manner undefined by the standard or is this just a bug
    > in the compiler?


    It's undefined behavior if the number of bits in the shift is equal or greater
    than the number of bits in the size of the other operand. I have worked
    on one implementation where the above would be a no op rather than
    a store of zero.
     
    Ron Natalie, Sep 29, 2003
    #4
    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. Mantorok Redgormor
    Replies:
    70
    Views:
    1,844
    Dan Pop
    Feb 17, 2004
  2. mfglinux
    Replies:
    11
    Views:
    750
    Roberto Bonvallet
    Sep 12, 2007
  3. David Filmer
    Replies:
    19
    Views:
    279
    Kevin Collins
    May 21, 2004
  4. VK
    Replies:
    45
    Views:
    666
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    390
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page