Talk:Bitwise IO: Difference between revisions

Confused endianness of bit encoding and endiannes of the machine
(→‎Real intention: no endianness issues I can see, + explanation)
(Confused endianness of bit encoding and endiannes of the machine)
Line 7:
:: You can object that a variable such <tt>d</tt> is stored into memory somewhere, so it is LE encoded. But all operations in a processor are rather endianness-logical (!)! So when I left-shift a 0x00FF that is stored into memory as 0xFF 0x00, the processor first load the datum, so that now you can think of it as 0x00FF, then perform a left shift, obtaining e.g. 0xFF00, then storing into memory, as 0x00 0xFF. If I'd read from memory byte by byte, loading and shifting to ''create'' a datum longer than one byte, I should have considered endianness seriously. But that's also why read and write operation are performed byte by byte rather than accumulating into a 16 or 32 bit word.
:: To say it briefly, I can't see any endianness issues. I am not interested how the processor stores the unsigned int I use to take the bits from; the fact is that when I do a datum << (32-4) for a 4 bit datum of 0xF, what I obtain is (luckly) 0xF0000000 (stored as 0x00 0x00 0x00 0xF0). When I shift again 1 bit left, I expect to have 0xE0000000, not 0xE0010000 (stored as 0x00 0x00 0x01 0xE0). I am telling it harder than it is. It's late for me... hopely tomorrow I will find better words. --[[User:ShinTakezou|ShinTakezou]] 01:07, 20 December 2008 (UTC)
 
:::Endianness is an issue here because you have to specify how a tuple of N bits is to be packed into an integral unit called byte, since your I/O deals with these units, not with bits. Note, this has nothing to do with the endianness of the machine. If N=8, then, to illustrate the case, let B<sub>i</sub>=(1,0,0,0,1,0,0,1), then B is 137<sub>10</sub> using big endian and 145<sub>10</sub> using little endian encoding, and there are N! variants of packing in total. The code you provided looks like big endian. If you don't like the term ''endian'', no problem. You can simply provide a formula, like byte = &Sigma; B<sub>i</sub>*2<sup>i-1</sup> (=little endian) or &Sigma; B<sub>i</sub>*2<sup>8-i</sup> (=big endian).