Reputation: 4251
How would you go about converting the following C #define into c#.
#define get16bits(d) (*((const uint16_t *) (d)))
#if !defined (get16bits)
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
+(uint32_t)(((const uint8_t *)(d))[0]) )
#endif
I know you probably replace the uint32_t which with UInt32 change the other types to c# equivalent, but how proceed from making the above a static method. Would that be the best way of going about it.
Bob.
Upvotes: 0
Views: 1715
Reputation: 700312
Getting the lower 16 bits of an integer is quite easy in C#:
int x = 0x12345678;
short y = (short)x; // gets 0x5678
If you want a static method for doing it, it's just as simple:
public static short Get16Bits(int value) {
return (short)value;
}
Upvotes: 0
Reputation: 15934
I do not know why you are checking to see if get16bits
is defined immediately after you define it, as the only way it would not be is a preprocessor error which would stop your compile.
Now, that said, here's how you translate that godawful macro to C#:
aNumber & 0xFFFF;
In fact, here's how you translate that macro to C:
a_number & 0xFFFF;
You don't need all this casting wizardry just to get the lower 16 bits of a number. Here's more C defines to show you what I'm talking about:
#define getbyte(d) (d & 0xFF)
#define getword(d) (d & 0xFFFF)
#define getdword(d) (d & 0xFFFFFFFF)
#define gethighword(d) ((d & 0xFFFF0000) >> 16)
#define gethighbyte(d) ((d & 0xFF00) >> 8)
Have you really used that macro in production code?
Upvotes: 2