views:

147

answers:

4
#define SwapByte4(ldata) \
   (((ldata & 0x000000FF) << 24) | \
   ((ldata & 0x0000FF00) << 8) | \
   ((ldata & 0x00FF0000) >> 8) | \
   ((ldata & 0xFF000000) >> 24))

What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24?

+3  A: 

You need to look at the 0x000000FF as a bitmask, i.e. where it's 1 the value of ldata will be taken and where it's 0 - 0 will be taken.

In order to understand the bitmask u need to convert it to binary, with hex it's very easy, every hex number is 4 binary digits, i.e.:

hex 0 = binary 0000 hex 1 = binary 0001 and so on.

Now to shifts: notice that the shift takes some data from the source, 8 bits exactly, and moves it to another location in the destination.

Now note that there's | i.e. OR operation on all the bitmask AND operations, i.e. zeroes will stay zeroes and in case there's '1' the result will contain one.

Hope it helps :)

Drakosha
or its used for wkb..
piyapiya
+10  A: 
Matthew Slattery
+1 for ASCII Art;
Billy ONeal
So, basically, this is a 32-bit-value Little-Endian <=> Big Endian converter.
Benoit
great thanks what will be the purpose of it by little Endian and big Endian
piyapiya
is this relate to machine epsilon..
piyapiya
is this used for WKB
piyapiya
"Machine epsilon" is a numerical property of a floating-point representation, and has nothing to do with bit shifting, masking, or endianness. I have no idea what you mean by "WKB".
Matthew Slattery
A: 

Let's say data is a 32 bit number represented as 0x12345678 (each number is 4 bits in hex)

Data & 0x000000FF means keep only the last 8 bits (called a bit mask) = 0x00000078

The << 24 means move this value to the left 24 bits (78 starts at position 24 [0 index]) = 0x78000000

The | means logical or which in this case will just be an addition

Final result = 0x78563412

Read on logical manipulations

David
thanks.... but what is the purpose of this all.
piyapiya
In this case, it's to swap endianess of a 32 bit number. I wouldn't write this as a #define but a function instead. Here's a good read and why you need these types of functions: http://en.wikipedia.org/wiki/Endianness
David
+1  A: 

This kind of code tends to be used to swap things between big endian and little endian format. There is also a little trick that will convert a word in some known format (lets say, little endian) into whatever endianness the current machine happens to be, and vice versa. That would go something like this:

unsigned long littleEndian;
unsigned char* littleBytes = &littleEndian;
unsigned long result = 0;
for (i = 0; i < 4; i++)
    result += unsigned long(littleBytes[i]) << (8 * i);

This works (assuming I haven't messed it up) because regardless of how bytes are actually stored, shift left is guaranteed to shift towards more significant bits. Converting to a char* allows you to access the bytes in the order they are actually stored in memory. Using this trick you don't need to detect the machine endianness to read/write stuff in a known format. Admittedly you could also just use the standard functions (hton etc.) :P

(Note: You have to be a little careful and cast the char before shifting, otherwise it just overflows all over your shoes. Also, += isn't the only option, |= would probably make more sense but might be less clear if you aren't used to it, I'm not sure)

David
can you tell me about significant number.. i have read about double and float point representation but i did not understand significant.
piyapiya
is this relate to machine epsilon?
piyapiya
by significant I just mean worth more, or bigger. If I have the number 123, the most significant digit is the 1, because it is "worth" 100. Same idea for bits and bytes in a number. Some machine store bytes in a different order, so the most significant byte of a number might be stored at the first memory address, or it might be the last.
David
WHAT IS MACHINE EPSILON. IS THIS RELATE TO THAT OR wWKB
piyapiya
oh i see OK can you tell me about significantly digit in float point representation in binary.. like float take 4B in memory and its represented as 0sign bit,8bit exponent and all others are fraction.. what will be signification digit here for storing a number.
piyapiya
and byte order is used for WKB or machine epsilon?
piyapiya
i mean when we need to use it? and why??
piyapiya