[lugip] Little endian and big endian in the kernel - why no conversion for u8 and s8
Luis R. Rodriguez
mcgrof at gmail.com
Thu Jan 10 16:20:07 EST 2008
So yesterday someone had asked why we don't have conversion routines
for a u8 to cpu and from cpu to u8 as we do with other bit ordering
such as u16, and u32 Examples:
return_type CPU-->LE LE-->CPU CPU-->BE BE-->CPU
u16 cpu_to_le16(u16) le16_to_cpu(u16) cpu_to_be16(u16) be16_to_cpu(u16)
u32 cpu_to_le32(u32) le32_to_cpu(u32) cpu_to_be32(u32) be32_to_cpu(u32)
u64 cpu_to_le64(u64) le64_to_cpu(u64) cpu_to_be64(u64) be64_to_cpu(u64)
But we don't have cpu_to_le8(), le8_to_cpu(), cpu_to_be8(), or be8_to_cpu().
The reason is the ordering is for bytes and not for bits, as I
incorrectly had started with. Apologies for the confusion. LSB stands
for Least Significant *Byte* and MSB Most Significant *Byte*, not bit,
and this indicates which byte goes first in a word. A 'word' here
being the amount of data that a processor can process at a time.
Because of this you don't need to covert one single byte as the bytes
are always the same. An example should suffice:
Number 2008 (this year) in binary is: (assume a word size of 32-bits)
00000000 00000000 00000111 11011000
In Little endian architecture (example x86) this would be saved in memory as:
Address | Byte in binary
0 | 11011000
1 | 00000111
2 | 00000000
3 | 00000000
On Big endian architecture (example is a 32-bit sparc) this would be
saved in memory as:
Address | Byte in binary
0 | 00000000
1 | 00000000
2 | 00000111
3 | 11011000
Luis
More information about the Lugip
mailing list