long int behaviour
tjg at exceptionalminds.com
Thu Mar 1 17:27:31 CET 2001
Yours is the second excellent explanation of what's going on
and I appreciate it very much. I don't necessarily believe that
ints are 32 bits long, however, IP addresses definitely are,
and IP addressing has been my first need to play with longs,
hence the 32 bitedness of my question.
All of my math was working out correctly regardless of the
representation I was seeing, it was just that when it came to
debugging in my head it was much easier to do when I saw what I
expected to see.
On Thu, Mar 01, 2001 at 12:09:25AM -0500, Tim Peters wrote:
> [Timothy Grant]
> > ...
> > Given the following experimenting...
> > >>> hex(4294965248L)
> > '0xFFFFF800L'
> > >>> hex(~4294965248L)
> > '-0xFFFFF801L'
> > >>>
> > The first output is what I would expect, however, I would have
> > expected the second output to be
> > 0x000007FFL
> > It appears to be something to do with two's complement storage,
> > but I'm at a loss as to why my expectations were so incorrect.
> Take heart! Your expectations weren't born incorrect, they were made
> incorrect, by a world of evil languages promulgating their blasphemous
> treachery that integers are finite <wink>.
> Python does store longs in 2's-comp form, but remember that Python longs are
> unbounded: a long less than 0 has a (conceptually) infinite string of
> leading 1 bits, just as a long >= 0 has an infinite string of leading 0 bits.
> It's easy to show you an infinite string of leading 0 bits: we just ignore
> them. That infinite string of leading 1 bits isn't so easy to hide, though!
> You've been further warped into believing that 32 bits is somehow a "natural"
> size for an int. If that's what you believe, you can force the issue:
> >>> hex(~4294965248L & (2L**32 - 1))
> That is, by masking your input with a string of 32 low-order 1-bits, the
> infinite string of leading 1 bits goes away, leaving the output you expected.
> Or, if you're lucky enough to believe that ints have 101 bits, similarly:
> >>> hex(~4294965248L & (2L**101 - 1))
> So that's the trick: to represent the infinite by the finite, you either
> need a convention, or you need to throw an infinite amount of info away. The
> "-" in '-0xFFFFF801L' is Python's convention for compressing the unbounded
> string of sign bits in such a way that no info is lost:
> >>> eval(hex(~4294965248L)) == ~4294965248L
> next-step:-infinite-memory-ly y'rs - tim
Timothy Grant tjg at exceptionalminds.com
Red Hat Certified Engineer www.exceptionalminds.com
Avalon Technology Group, Inc. <>< (503) 246-3630
>>>>>>>>>>>>>Linux, because rebooting is *NOT* normal<<<<<<<<<
>>>>This machine was last rebooted: 43 days 20:37 hours ago<<
More information about the Python-list