c - Strange behaviour when using pointers -


i have file binary data follows.

aa 55 aa 55 aa 55 aa 55 aa 55 aa 55 aa 55 aa 55          aa 55 aa 55 36 65 fb 5f 1e 92 d8 1b 55 f7 fb 5f          1e 92 d8 1b 

i want extract values 55 36 65 fb 5f 1e.

if use following code.

temp_1 = (data_ptr[offset]); val = temp_1 << 40;  temp_1 = (data_ptr[offset + 1]); val |= temp_1 << 32;  temp_1 = (data_ptr[offset + 2]); val |= temp_1 << 24;  temp_1 = (data_ptr[ts_offset + 3]); val |= temp_1 << 16;  temp_1 = (data_ptr[ts_offset + 4]); val |= temp_1 << 8;  temp_1 = (data_ptr[ts_offset + 5]); val |= temp << 0;  printf("read value %"prix64" \n",val); 

the outupt of printf

3665fb5f1e92 

now try calculate same value using single 64-bit ptr cast operation.

val = *(uint64_t*)(data_ptr + ts_offset); printf("read value %"prix64" \n",val); 

the output of above code

1bd8921e5ffb6536  

if check least significant 48-bits here 921e5ffb6536

it inverted compared first result.i using normal intel processor.why behaviour? expected.

in code shifts value, reading highest byte lowest address (bits 40-47 offset 0). i'm guessing second case running on x86 machine, reading lowest byte lowest address (because that's how x86 defined, many other processors, called "little-endian" machines).

if want bytes in particular order, on little-endian machine, have bytes 1 one , shift (if wanted whole 64-bit value, may have been possible use byteswap instruction on x86, doubt it's enough value in when need "undo" shifting , masking 16 bits didn't need).


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -