Percy Flarge
Percy Flarge

Reputation: 437

How is this code assuming little endian is being used?

In an article explaining little versus big endian it said the folowing code was making the assumption that it was running on a little endian machine.

The reason it says for the assumption is "The switching of the bytes is being assumed in the 'C' structure." I don't understand where the assumption is.

struct { WORD y; WORD x; } POS;

lparam = (DWORD) POS;

Upvotes: 0

Views: 293

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208475

Think about it like this, x is (0x1234) and y is (0x5678) and the intention is to have lparam be (0x12345678).

The code from the example will cause lparam to be (0x78 0x56 0x34 0x12) on a little endian machine, which is (0x12345678) as intended.

However on a big endian machine lparam will be (0x56 0x78 0x12 0x34) which is (0x56781234). Therefore this code was written with the assumption that it was for little endian.

Upvotes: 1

Related Questions