parsing #define in a code

R

rahul8143

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.
 
K

Kenneth Brody

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:[email protected]>
 
R

rahul8143

Kenneth said:
Takes 0x000000nn and gives 0xnn000000


Takes 0x0000nn00 and gives 0x00nn0000


Takes 0x00nn0000 and gives 0x0000nn00


Takes 0xnn000000 and gives 0x000000nn


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?
 
E

Emmanuel Delahaye

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
 
K

Keith Thompson

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top