pack a three byte int

John Machin sjmachin at lexicon.net
Thu Nov 9 20:34:19 EST 2006


p.lavarre at ieee.org wrote:
> Speaking as the OP, perhaps I should mention:
>
> > > [-3:] to [1:] is a minor cosmetic improvement
>
> To my eye, that's Not an improvement.
>
> '\x08' '\x01\x23\x45' '\x80' '\0' is the correct pack of (0x08,
> 0x12345, 0x80, 0) because '\x01\x23\x45' are the significant low three
> bytes of a big-endian x12345, thus [-3:].
>
> The [1:] fact that we can keep the 3 significant bytes by tossing
> exactly 1 byte away after rounding the bit length of that digital
> number up to the nearest power of two which happens to be 4 = 3 + 1 is
> merely incidental - not of central significance.

I said *cosmetic* improvement and also said "obscures the underlying"
need-to-know that 4 - 3 == 1 (which I didn't contemplate needing 2
paragraphs of laborious explanation).




More information about the Python-list mailing list