~ 1 = -2 ???

Tim Peters tim_one at email.msn.com
Fri Nov 5 02:14:25 EST 1999


[Les Schaffer]
> ...
> so for my code, i will do
>
> def not4bit(x):
>       return ~x & 15
>
> >>> not4bit(1)
> 14

Les, if x is always in range(16), simply subtract it from 15.  That is,

def not4bit(x):
    assert 0 <= x < 16
    return 15 - x

It's twice as fast <wink>.

or-15-times-as-slow-it's-hard-to-tell-ly y'rs  - tim






More information about the Python-list mailing list