parsing #define in a code

Discussion in 'C Programming' started by rahul8143@gmail.com, Oct 4, 2005.

  1. Guest

    I have downlaoded one code from internet where i found one #define
    statement
    #define __fun32(x) ((_u32)( \
    (((_u32)(x) & (_u32)0x000000ffUL) << 24) | \
    (((_u32)(x) & (_u32)0x0000ff00UL) << 8) | \
    (((_u32)(x) & (_u32)0x00ff0000UL) >> 8) | \
    (((_u32)(x) & (_u32)0xff000000UL) >> 24) ))

    what is the funtioning of this define function.
    , Oct 4, 2005
    #1
    1. Advertising

  2. wrote:
    >
    > I have downlaoded one code from internet where i found one #define
    > statement
    > #define __fun32(x) ((_u32)( \
    > (((_u32)(x) & (_u32)0x000000ffUL) << 24) | \


    Takes 0x000000nn and gives 0xnn000000

    > (((_u32)(x) & (_u32)0x0000ff00UL) << 8) | \


    Takes 0x0000nn00 and gives 0x00nn0000

    > (((_u32)(x) & (_u32)0x00ff0000UL) >> 8) | \


    Takes 0x00nn0000 and gives 0x0000nn00

    > (((_u32)(x) & (_u32)0xff000000UL) >> 24) ))


    Takes 0xnn000000 and gives 0x000000nn

    >
    > what is the funtioning of this define function.


    It reverses the byte order in a 32-bit value. For example, it turns
    0xAABBCCDD into 0xDDCCBBAA.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 4, 2005
    #2
    1. Advertising

  3. Guest

    Kenneth Brody wrote:
    > wrote:
    > >
    > > I have downlaoded one code from internet where i found one #define
    > > statement
    > > #define __fun32(x) ((_u32)( \
    > > (((_u32)(x) & (_u32)0x000000ffUL) << 24) | \

    >
    > Takes 0x000000nn and gives 0xnn000000
    >
    > > (((_u32)(x) & (_u32)0x0000ff00UL) << 8) | \

    >
    > Takes 0x0000nn00 and gives 0x00nn0000
    >
    > > (((_u32)(x) & (_u32)0x00ff0000UL) >> 8) | \

    >
    > Takes 0x00nn0000 and gives 0x0000nn00
    >
    > > (((_u32)(x) & (_u32)0xff000000UL) >> 24) ))

    >
    > Takes 0xnn000000 and gives 0x000000nn
    >
    > >
    > > what is the funtioning of this define function.

    >
    > It reverses the byte order in a 32-bit value. For example, it turns
    > 0xAABBCCDD into 0xDDCCBBAA.

    Does that mean its the code for convert/reverse endianess? what is
    final output for your inputs in above example is it 0xnnnnnnnn?
    >
    > --
    > +-------------------------+--------------------+-----------------------------+
    > | Kenneth J. Brody | www.hvcomputer.com | |
    > | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    > +-------------------------+--------------------+-----------------------------+
    > Don't e-mail me at: <mailto:>
    , Oct 4, 2005
    #3
  4. wrote on 04/10/05 :
    > I have downlaoded one code from internet where i found one #define
    > statement
    > #define __fun32(x) ((_u32)( \
    > (((_u32)(x) & (_u32)0x000000ffUL) << 24) | \
    > (((_u32)(x) & (_u32)0x0000ff00UL) << 8) | \
    > (((_u32)(x) & (_u32)0x00ff0000UL) >> 8) | \
    > (((_u32)(x) & (_u32)0xff000000UL) >> 24) ))
    >
    > what is the funtioning of this define function.


    swap nibbles

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
    Emmanuel Delahaye, Oct 4, 2005
    #4
  5. writes:
    > Kenneth Brody wrote:
    >> wrote:
    >> >
    >> > I have downlaoded one code from internet where i found one #define
    >> > statement
    >> > #define __fun32(x) ((_u32)( \
    >> > (((_u32)(x) & (_u32)0x000000ffUL) << 24) | \

    >>
    >> Takes 0x000000nn and gives 0xnn000000
    >>
    >> > (((_u32)(x) & (_u32)0x0000ff00UL) << 8) | \

    >>
    >> Takes 0x0000nn00 and gives 0x00nn0000
    >>
    >> > (((_u32)(x) & (_u32)0x00ff0000UL) >> 8) | \

    >>
    >> Takes 0x00nn0000 and gives 0x0000nn00
    >>
    >> > (((_u32)(x) & (_u32)0xff000000UL) >> 24) ))

    >>
    >> Takes 0xnn000000 and gives 0x000000nn
    >>
    >> >
    >> > what is the funtioning of this define function.

    >>
    >> It reverses the byte order in a 32-bit value. For example, it turns
    >> 0xAABBCCDD into 0xDDCCBBAA.

    > Does that mean its the code for convert/reverse endianess? what is
    > final output for your inputs in above example is it 0xnnnnnnnn?


    Well, if the input is 0xnnnnnnn, the output is 0xnnnnnnnn (assuming n
    is a hexadecimal digit), but I don't think that's terribly useful.

    Yes, it converts a little-endian 32-bit unsigned integer to
    big-endian, or vice versa.

    Given an input of 0x12345678, the result is 0x78563412. More
    generally, given an input of 0xSTUVWXYZ, the result will be
    0xYZWXUVST, where each of STUVWXYZ represents some arbitrary
    hexadecimal digit.

    Note that there's some missing context. The macro assumes that _u32
    is a type name, presumably a typedef for a 32-bit unsigned integer.
    Also, the identifier _u32 is reserved for use as an identifier with
    file scope, and __fun32 is reserved for any use. If this macro is
    part of the implementation, that's ok; otherwise, the author should
    have chosen different names. (It's likely that this won't cause any
    visible problems, unless the implementation happens to use those
    names.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 4, 2005
    #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. GIMME
    Replies:
    2
    Views:
    873
    GIMME
    Feb 11, 2004
  2. theotyflos
    Replies:
    3
    Views:
    464
    Thomas Matthews
    Feb 19, 2004
  3. robin liu
    Replies:
    3
    Views:
    821
    Robin Liu
    Apr 21, 2006
  4. Replies:
    2
    Views:
    256
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    458
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page