Vineel Kumar Reddy
Vineel Kumar Reddy

Reputation: 4726

bitwise operation and stuffing bytes together

is there a easy way to form a long long variable from 8 consecutive bytes....

TotalSectors = sector[0x28]|
               sector[0x28+1] << 8 |
               sector[0x28+2] << 16 |
               sector[0x28+3] << 24 |
               sector[0x28+4] << 32 | 
               sector[0x28+5] << 40 |
               sector[0x28+6] << 48 |
               sector[0x28+7] << 56;

Where TotalSectors is a long long variable....

I am working on windows platform and win32 api is my main choice..... any existing macro for this work would be helpful....

Thanks in advance...

Upvotes: 0

Views: 183

Answers (3)

Hans Passant
Hans Passant

Reputation: 942177

Endianness is good. So just cast:

 TotalSectors = *(long long*)&sector[0x28];

Upvotes: 3

Jack
Jack

Reputation: 133619

what about a for loop?

for (int i = 0; i < 8; ++i)
  TotalSectors |= sector[0x28+i]<<(8*i);

Upvotes: 1

Schedler
Schedler

Reputation: 1423

I think you have already written the easy way out, so why not simply make it an inline function?

Upvotes: 0

Related Questions