[Nottingham] A Little Coding Gem

Martin martin at ml1.co.uk
Fri Nov 16 12:34:44 UTC 2012


On 15/11/12 23:37, Mike Cardwell wrote:
> On 15/11/12 23:35, Mike Cardwell wrote:
> 
>> Also, big/little endian tends to apply to the order of bytes,
>> whereas this bit of code changed the order of the bits inside the
>> bytes too.
> 
> Hmm. Now I've said this, I'm not 100% sure I'm right. Somebody correct
> me if I'm wrong please. I think my confusion came from the way the
> shift operators in C behave the same way regardless of the endianness
> of the system.

OK, I'm going to jump in on this with both concrete boots on because it
is something that I *should* know... Corrections greatly welcomed...


We have the usual convention whereby "D0" or "bit 0" is the LSB, however
that has not always been the case.

We now normally have machines operating in binary, although in the past
we have had machines operating in BCD and even 1's compliment arithmetic
rather than today's 2's compliment. (The 1's compliment is fun in that
you get two binary numbers for zero!)

Most modern machines offer 8-bit byte addressing of memory and storage,
which is where at the byte level (chunks of 8 bits) you can expect the
bit order to be maintained correct by the hardware.

For 'manually' assembling multiple bytes, /that/ is where you need to be
aware of endian-ness and word boundaries/alignment. Similarly so for if
you are serial "bit sniffing" individual bits.

And for those blind majority that are unaware, great care has been taken
in the compilers to just simply make it all work for all of us!


Comments and elaborations welcomed...

Cheers,
Martin


LSB: least significant bit, 2^0
MSB: most significant bit, 2^n
BCD: binary coded decimal


-- 
- ------------------ - ----------------------------------------
-    Martin Lomas    - OpenPGP (GPG/PGP) Public Key: 0xCEE1D3B7
- martin @ ml1 co uk - Import from   hkp://subkeys.pgp.net   or
- ------------------ - http:// ml1 .co .uk/martin_ml1_co_uk.gpg



More information about the Nottingham mailing list