Question About Casting

Discussion in 'C++' started by David Vestal, Dec 11, 2003.

  1. David Vestal

    David Vestal Guest

    I'm reading a 24-bit address from a char* named i_msg, and I expected these
    two snippets to be equivalent. They aren't; the first doesn't work. What
    gives?

    SNIPPET 1:

    unsigned long address;
    address = ((unsigned long) i_msg[2]) << 16
    + ((unsigned short) i_msg[3]) << 8
    + i_msg[4];

    SNIPPET 2:

    unsigned long address;
    address = i_msg[2];
    address <<= 8;
    address += i_msg[3];
    address <<= 8;
    address += i_msg[4];
     
    David Vestal, Dec 11, 2003
    #1
    1. Advertising

  2. David Vestal wrote:

    > I'm reading a 24-bit address from a char* named i_msg, and I expected these
    > two snippets to be equivalent. They aren't; the first doesn't work. What
    > gives?
    >
    > SNIPPET 1:
    >
    > unsigned long address;
    > address = ((unsigned long) i_msg[2]) << 16
    > + ((unsigned short) i_msg[3]) << 8
    > + i_msg[4];
    >


    The addition is done before the shifting.

    --
    Thomas.
     
    Thomas Stegen, Dec 11, 2003
    #2
    1. Advertising

  3. David Vestal

    David Vestal Guest

    Thomas Stegen <> wrote in
    news:3fd8a7db$:

    > David Vestal wrote:
    >
    >> I'm reading a 24-bit address from a char* named i_msg, and I expected
    >> these two snippets to be equivalent. They aren't; the first doesn't
    >> work. What gives?
    >>
    >> SNIPPET 1:
    >>
    >> unsigned long address;
    >> address = ((unsigned long) i_msg[2]) << 16
    >> + ((unsigned short) i_msg[3]) << 8
    >> + i_msg[4];
    >>

    >
    > The addition is done before the shifting.
    >


    *groan* Thanks. I'm too old to be making mistakes like that.
     
    David Vestal, Dec 11, 2003
    #3
  4. David Vestal

    Dan Pop Guest

    In <Xns944E7B4ABF634someoggetridofthisma@130.133.1.4> David Vestal <> writes:

    >I'm reading a 24-bit address from a char* named i_msg, and I expected these
    >two snippets to be equivalent. They aren't; the first doesn't work. What
    >gives?


    You have to throw more parentheses at the problem: the compiler is
    blissfully ignoring your nice formatting of the expression and uses the
    C precedence rules instead ;-)

    >SNIPPET 1:
    >
    >unsigned long address;
    >address = ((unsigned long) i_msg[2]) << 16
    > + ((unsigned short) i_msg[3]) << 8
    > + i_msg[4];


    This is interpreted as:

    (((unsigned long) i_msg[2]) << (16 + ((unsigned short) i_msg[3]))) <<
    (8 + i_msg[4])

    which probably not what you want. Try the following instead:

    address = (((unsigned long) i_msg[2]) << 16)
    + (((unsigned short) i_msg[3]) << 8)
    + (unsigned char) i_msg[4];

    Note that, unless plain char is unsigned, the last cast I have added is
    needed, too.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Dec 11, 2003
    #4
  5. David Vestal <> wrote in
    news:Xns944E7ED546523someoggetridofthisma@130.133.1.4:

    >>> I'm reading a 24-bit address from a char* named i_msg, and I expected
    >>> these two snippets to be equivalent. They aren't; the first doesn't
    >>> work. What gives?
    >>>
    >>> SNIPPET 1:
    >>>
    >>> unsigned long address;
    >>> address = ((unsigned long) i_msg[2]) << 16
    >>> + ((unsigned short) i_msg[3]) << 8
    >>> + i_msg[4];
    >>>

    >>
    >> The addition is done before the shifting.
    >>

    >
    > *groan* Thanks. I'm too old to be making mistakes like that.


    Using | instead of + would have worked. Were you thinking of that?

    --
    - Mark ->
    --
     
    Mark A. Odell, Dec 11, 2003
    #5
    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. Remco
    Replies:
    1
    Views:
    361
    Alvin Bruney [MVP]
    Apr 11, 2004
  2. David Eynon

    casting question

    David Eynon, Jul 1, 2003, in forum: Java
    Replies:
    2
    Views:
    868
    Roedy Green
    Jul 1, 2003
  3. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    575
    James Kanze
    Dec 20, 2009
Loading...

Share This Page