bit operations and sequence points

Discussion in 'C++' started by Divick, Sep 5, 2006.

  1. Divick

    Divick Guest

    Hi,
    I have written a function which converts from ARGB1555 to RGBA5551
    , i.e. changes the position of the msb to lsb by shifting the other
    bits. The function is shown below, but I have doubt about the
    correctness of this function.

    In the operation below, I am using variable ARGB twice in the statement
    and there is no sequence point in between those two uses. Thus the
    answer is dependent upon the evaluation order of the statement. If ARGB
    is shifted left and then used for the & operation then the answer will
    not be correct. On my machine and compiler the answer seems right but I
    still doubt this function.

    void ARGB1555_to_RGBA5551(unsigned short &RGBA, unsigned short ARGB)
    {
    RGBA = ( (ARGB & (1L << 15)) > 0 ) | (ARGB << 1);
    }

    Am I right in my reasoning?

    Thanks,
    Divick
     
    Divick, Sep 5, 2006
    #1
    1. Advertising

  2. Divick wrote:
    > Hi,
    > I have written a function which converts from ARGB1555 to RGBA5551
    > , i.e. changes the position of the msb to lsb by shifting the other
    > bits. The function is shown below, but I have doubt about the
    > correctness of this function.
    >
    > In the operation below, I am using variable ARGB twice in the statement
    > and there is no sequence point in between those two uses. Thus the
    > answer is dependent upon the evaluation order of the statement. If ARGB
    > is shifted left and then used for the & operation then the answer will


    It isn't. << yields a new value, ARGB is not altered. You're safe.

    > not be correct. On my machine and compiler the answer seems right but I
    > still doubt this function.
    >
    > void ARGB1555_to_RGBA5551(unsigned short &RGBA, unsigned short ARGB)
    > {
    > RGBA = ( (ARGB & (1L << 15)) > 0 ) | (ARGB << 1);
    > }
    >
    > Am I right in my reasoning?
    >
    > Thanks,
    > Divick
    >



    --
    Nils O. SelÄsdal
    www.utelsystems.com
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 5, 2006
    #2
    1. Advertising

  3. Divick

    Divick Guest


    > It isn't. << yields a new value, ARGB is not altered. You're safe.

    Oh yes, I forgot the basics. :(

    Thanks,
    Divick
     
    Divick, Sep 5, 2006
    #3
    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. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,280
    robert
    Feb 11, 2006
  2. John Smith

    sequence points and evaluation order

    John Smith, Sep 4, 2006, in forum: C Programming
    Replies:
    9
    Views:
    353
    Robert Gamble
    Sep 6, 2006
  3. Dave Rahardja
    Replies:
    8
    Views:
    417
    Dave Rahardja
    Jul 5, 2007
  4. ais523
    Replies:
    9
    Views:
    383
    Kaz Kylheku
    Feb 9, 2008
  5. George

    sequence points and the execution model

    George, Jan 17, 2009, in forum: C Programming
    Replies:
    7
    Views:
    325
    Richard
    Jan 20, 2009
Loading...

Share This Page