Possible improvement to slice opperations.

Bengt Richter bokr at oz.net
Mon Sep 5 21:33:23 EDT 2005


On Mon, 05 Sep 2005 17:10:05 -0400, Steve Holden <steve at holdenweb.com> wrote:

>Paul Rubin wrote:
>> Steve Holden <steve at holdenweb.com> writes:
>> 
>>>Given that Python has a 1's-complement operator already I don;t see
>>>why you can't just leave Python alone and use it,
>> 
>> 
>> What's the meaning of the 1's complement operator (for example, what
>> is ~1), when ints and longs are the same?
>
>Python 2.2.1 (#1, Aug 25 2004, 16:56:05)
>[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
> >>> ~1L
>-2L
> >>> ~1
>-2
> >>> import sys
> >>> sys.maxint*4
>8589934588L
> >>> ~(sys.maxint*4)
>-8589934589L
> >>>
>
>$ python
>Python 2.4.1 (#1, May 27 2005, 18:02:40)
>[GCC 3.3.3 (cygwin special)] on cygwin
>Type "help", "copyright", "credits" or "license" for more information.
>  >>> ~1L
>-2L
>  >>> ~1
>-2
>  >>> sys.maxint*4
>8589934588L
>  >>> ~(sys.maxint*4)
>-8589934589L
>  >>>
>
>What's going to change when ints and longs are finally integrated?
>
I suspect that Paul may be worrying that bit operations on longs will be
bit operations on signs and positive magnitudes rather than signed numbers
of arbitrary width. This may be promoted by the fact the so far we have no
builting format for showing the bits of negative numbers the way hex used to do.

I scratched the itch this way:

 >>> from ut.basecompl import basecompl as bc
 >>> import sys
 >>> bc(sys.maxint*4, 16)
 '01fffffffc'
 >>> bc(~sys.maxint*4, 16)
 'fe00000000'
oops, precendence ...
 >>> bc(~(sys.maxint*4), 16)
 'fe00000003'

vs. the useless
 >>> hex(~(sys.maxint*4))
 '-0x1FFFFFFFDL'

(at least for looking at bit operation results ;-)

BTW, note base complementing, with base-1 or 0 as sign:

 >>> bc(~(sys.maxint*4), 8)
 '700000000003'
 >>> bc( (sys.maxint*4), 8)
 '077777777774'
 >>> bc(~(sys.maxint*4), 10)
 '91410065411'
 >>> bc( (sys.maxint*4), 10)
 '08589934588'
 >>> bc(~(sys.maxint*4), 2)
 '1000000000000000000000000000000011'
 >>> bc( (sys.maxint*4), 2)
 '0111111111111111111111111111111100'

Regards,
Bengt Richter



More information about the Python-list mailing list