Greg Ewing (using news.cis.dfn.de)
me at privacy.net
Thu Mar 27 04:25:43 CET 2003
Daniel Timothy Bentley wrote:
> It seems like so long as there's a distinction in the language between
> ints and longs, there should be a way to create ints reliably.
I think the intention is to eventually make the distinction
go away altogether at the language level. There might still
be int and long objects under the covers, but that would
just be an implementation detail, and you'd be able to
use them interchangeably everywhere.
In your case, it seems to me you don't actually care whether
you've got an int object or a long object, as long as the
arithmetic works right. I second the suggestion to mask
the results, but I'd add that you don't really need to
worry about keeping the sign right. Just keep all your
values internally as unsigned ints until you need to
do something which actually cares about the sign.
This is a better reflection of the way the real hardware
works, anyway -- the bit patterns in registers and memory
aren't inherently signed or unsigned, it all depends on
what you're doing with them.
So, e.g. an addition just becomes
c = (a + b) & 0xFFFFFFFFL
which will be considerably faster than using an if-statement
to sort out the sign every time.
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
More information about the Python-list