SQLite Forum

Endianness help
(Wondering why the topics least tied to reality generate the most posts ...)

As Mr. Damon said,

>The first thing to realize is that unless the processor has instructions to access specific bits in the bytes (and there are processors with this ability) then the numbering of the bits within the bytes is purely arbitrary and by convention.

The processors where bit numbering has some reality independent of yammering about it are even fewer, being those which permit bits specified by their number to be accessed or altered. Only those could be assigned a bit-endian attribute by somebody who dug up a working machine (or its physical design documents) and had to categorize it as little-bit-endian or big-bit-endian, without being able to point to documents merely labeling the bits as bigger or littler.

Regarding Mr. Brewis' claim,

> ... bits right to left ... bits left to right ...

Sorry, but that can only be something that might appear in documentation, (and would depend on whether it was held upside down or not), but is not an attribute of the machines themselves. As Mr. Warren said for RAM, the hardware is utterly agnostic about right or left, top or bottom, fore or aft.

Regarding Mr. Brewis' claim,

> ... bytes right to left ... bytes left to right ...

Urrghh. Another confusion factor. The usual criterion for byte endianality is the relation between byte address ordering and arithmetic weight of the bytes within larger words. It is only when such matters are committed to diagrams that notions of right or left can even be sensibly applied.

Furthermore, not every machine must be either little-endian or big-endian. For an example, see [PDP-endian](https://en.wikipedia.org/wiki/Endianness#PDP-endian). In the general case, if we compile something like this C program:<code>
\#include <stdio.h>
typedef long MachineWord;
union ExposeEndianality {
  MachineWord asMachineWord;
  unsigned char asBytes[ sizeof(MachineWord) ];
} endianality = {(MachineWord)0x0706050403020100L};
int main() {
  int i;
  for (i = 0; i < sizeof(MachineWord); ++i)
    printf("%1d\\n", endianality.asBytes[i]);
  return 0;
</code>, the small numbers comprising endianality can be emitted in any order, limited only by the degree to which the machine designer(s) wished to avoid perversity.  That PDP machine might have produced <code>
</code>. I defy anybody to write a proper C program which will expose bit-endianality in similar fashion.