Re: Fastest way to byte address ints?

Discussion in 'C++' started by Joshua Maurice, Mar 5, 2010.

  1. On Mar 4, 8:24 am, "Peter Olcott" <> wrote:
    > I had always been using:
    >
    > union {
    >    uint8   Bytes[4];
    >    uint32 Integer;
    >
    > };
    >
    > because it worked on my platform up to now.


    Has it stopped working? Note that this is technically undefined
    behavior according to the C and C++ standard, but it's such a common
    extension to allow type punning through a union that it might as well
    be part of the standard. However, anyone using this should read up on
    it and understand the caveats and restrictions on doing such things.

    Note that there is a bug in the C and C++ standards related to unions
    and strict aliasing. If you let pointers (or references) to two
    different union members escape the scope, and the compiler does
    optimization using the strict aliasing allowance, then your code might
    break as the compiler does not know that the two pointers point to a
    union, aka alias, as it's assuming that the pointers do not alias.


    > Is this the right syntax for addressing the lowest order
    > three bytes of an int?
    >
    > unsigned int  Integer;
    > unsigned char 1stByte  =  *( ( (unsigned char*) &Integer) +
    > 0 )
    > unsigned char 2ndByte =  *( ( (unsigned char*) &Integer) +
    > 1 )
    > unsigned char 3rdByte =  *( ( (unsigned char*) &Integer) +
    > 2 )


    Now, this is actually allowed by the standard, assuming that
    sizeof(Integer) >= 3 * sizeof(unsigned char), (presumably 4, but you
    only posted code for the first 3 bytes), and this is not true for all
    platforms. Note that most people I talk to on this subject acknowledge
    that the standard is somewhat vague on this point, and that it's
    possible to read it both ways, but most people seem to think that the
    standard's intent is to allow reading or writing any POD structure
    through a char or unsigned char lvalue as you're doing here. (Note
    that simply because writing it allowed, you could still write a trap
    value or some other bogus value for that POD type, and that could blow
    up in your face.)

    Also, is this the correct syntax to address the lowest order 3 bytes?
    Depends on what you mean by lowest order. If by lowest hardware
    address, then yes. If you mean lowest significant bits of the value of
    the integer, then no. Google "little endian" vs "big endian". It could
    also be stored in a bizarre format other than two's compliment or
    one's compliment. It could place the first significant bit at the
    highest bit address, and the next most significant bit at the lowest
    hardware address. This probably would never happen, but it just goes
    to show that this is implementation specific, and you should really
    learn more / lookup answers to these basic questions, and realize what
    code is portable or not, and portable to what extent (ex: maybe only
    to POSIX systems).


    > Is there any other way that would execute faster?


    Depends on what you're trying to do. I don't know offhand. However, I
    expect type punning through a union to produce equivalent assembly to
    what you're doing with the cast to unsigned char. (Not that this
    expectation is supported by any firsthand evidence I've seen, just
    hearsay and conjecture. It's just that I would be greatly saddened
    that there's a commercial compiler which fails to optimize such a
    basic example.)


    Finally, for such low level bit hackery, I strongly suggest looking at
    the produced assembly to make sure it's actually doing what you want
    it to do.
    Joshua Maurice, Mar 5, 2010
    #1
    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. Chris
    Replies:
    5
    Views:
    389
    Randolf Richardson
    Jan 28, 2007
  2. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    816
    Jack Klein
    Jul 10, 2004
  3. Laszlo Nagy
    Replies:
    2
    Views:
    259
    Matthew Woodcraft
    Aug 9, 2008
  4. M.-A. Lemburg
    Replies:
    3
    Views:
    269
    castironpi
    Aug 10, 2008
  5. Marcel Müller

    Re: Fastest way to byte address ints?

    Marcel Müller, Mar 4, 2010, in forum: C++
    Replies:
    2
    Views:
    327
    James Kanze
    Mar 5, 2010
Loading...

Share This Page