left shift then right shift an unsigned int

Discussion in 'C++' started by Wenjie, Jul 11, 2003.

  1. Wenjie

    Wenjie Guest

    Hello, for the following code:
    unsigned int var = 0xFF2277F0UL;
    unsigned char take2 = (var << 8) >> 24;
    Is there any pitfall about the shift? (get '22' i.e. 34 or '"' as wanted)


    Thanks and best regards,
    Wenjie
    Wenjie, Jul 11, 2003
    #1
    1. Advertising

  2. On 11 Jul 2003 04:07:43 -0700, (Wenjie) wrote:

    >Hello, for the following code:
    > unsigned int var = 0xFF2277F0UL;
    > unsigned char take2 = (var << 8) >> 24;
    >Is there any pitfall about the shift? (get '22' i.e. 34 or '"' as wanted)


    It looks OK to me...


    --
    Bob Hairgrove
    Bob Hairgrove, Jul 11, 2003
    #2
    1. Advertising

  3. Wenjie

    Ron Natalie Guest

    "Greg Schmidt" <> wrote in message news:...
    > On 11 Jul 2003 04:07:43 -0700, (Wenjie) wrote:
    >
    > >Hello, for the following code:
    > > unsigned int var = 0xFF2277F0UL;
    > > unsigned char take2 = (var << 8) >> 24;
    > >Is there any pitfall about the shift? (get '22' i.e. 34 or '"' as wanted)

    >
    > As others have said, it looks fine, but it seems to me that
    > unsigned int var = 0xFF2277F0UL;
    > unsigned char take2 = (var >> 16) & 0xFF;
    > might be clearer as to the purpose, and easier to modify down the road.


    I don't know what your assumptions are (since you hardcoded a lot of numbers
    here), but if char is an 8 bit quantity, even the & 0xFF is superfluous.
    Ron Natalie, Jul 11, 2003
    #3
  4. (Wenjie) wrote in message news:<>...
    > Hello, for the following code:
    > unsigned int var = 0xFF2277F0UL;
    > unsigned char take2 = (var << 8) >> 24;
    > Is there any pitfall about the shift? (get '22' i.e. 34 or '"' as wanted)


    One pitfall is that it is dependent on the size of an unsigned int to
    get the expected results. If unsigned int holds more then 32 bits,
    the << 8 won't do the intended stripping.

    Even worse, I think it isn't clear what this code is doing. I can
    picture someone accidentally changing the code to:

    var >> 16;

    If I were writing the code, I'd making the stripping more explicit.

    (var >> 16) & 0xFF

    or possibly:

    (var & 0xFF0000) >> 16

    samuel
    Ron Samuel Klatchko, Jul 11, 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. Timo Freiberger
    Replies:
    3
    Views:
    941
    Bob Hairgrove
    Oct 30, 2004
  2. Ehsan Khoddam mohammadi

    unsigned right shift casts to int automatically!!!

    Ehsan Khoddam mohammadi, Jan 4, 2007, in forum: Java
    Replies:
    2
    Views:
    413
    Ehsan Khoddam mohammadi
    Jan 4, 2007
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,446
    CBFalconer
    Nov 30, 2006
  4. pozz
    Replies:
    12
    Views:
    731
    Tim Rentsch
    Mar 20, 2011
  5. Sanny
    Replies:
    38
    Views:
    3,378
    Thomas Richter
    Apr 29, 2011
Loading...

Share This Page