Question

So I am working on porting a very small chunk of code from VC++ to .NET. I have always been a VB guy, dabbled a bit in C# but not much, and have little to no knowledge in VC++. Yeah, I know, it's probably a bad idea to try and port code from a language you know nothing about, but I don't see the point in taking unnecessary time to learn the in's and out's of a language for the sake of porting a couple hundred lines of code. I have managed to learn enough to get a lot of it ported over, but somethings I am still unclear on. Here is the VC++ code:

#define ES16(_val) \
((u16)(((((u16)_val) & 0xff00) >> 8) | \
       ((((u16)_val) & 0x00ff) << 8)))

#define ES32(_val) \
((u32)(((((u32)_val) & 0xff000000) >> 24) | \
       ((((u32)_val) & 0x00ff0000) >> 8 ) | \
       ((((u32)_val) & 0x0000ff00) << 8 ) | \
       ((((u32)_val) & 0x000000ff) << 24)))

#define ES64(_val) \
((u64)(((((u64)_val) & 0xff00000000000000ull) >> 56) | \
       ((((u64)_val) & 0x00ff000000000000ull) >> 40) | \
       ((((u64)_val) & 0x0000ff0000000000ull) >> 24) | \
       ((((u64)_val) & 0x000000ff00000000ull) >> 8 ) | \
       ((((u64)_val) & 0x00000000ff000000ull) << 8 ) | \
       ((((u64)_val) & 0x0000000000ff0000ull) << 24) | \
       ((((u64)_val) & 0x000000000000ff00ull) << 40) | \
       ((((u64)_val) & 0x00000000000000ffull) << 56)))

Can someone explain what is going on here? And maybe provide a little insight as to how to re-write this in VB.NET (although, if I knew what was going on, I could surely re-write it myself, haha).

As always, thanks in advance everyone.

Était-ce utile?

La solution

These macros reverse bytes in variables of integral types: short, unsigned short, int, unsigned int, long long and unsigned long long (provided that they have correspondingly sizes of 16, 32, and 64 bits). For example if there is some variable of type unsigned short that has value 0x1234 then after applying the first macro, ES16( 0x1234), the result will be 0x3412.

That it would be more clear consider step by steap the action of the macro

((u16)_val) & 0xff00) will be equal to 0x1200 then

0x1200 >> 8 will be equal to 0x0012

((u16)_val) & 0x00ff) will be equal to 0x0034

then

0x0034 << 8 will be equal to 0x3400

And at last

0x0012 | 0x3400 will result in 0x3412

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top