[Python-Dev] Octal literals

Bengt Richter bokr at oz.net
Wed Feb 1 16:35:55 CET 2006

On Wed, 01 Feb 2006 12:33:36 +0000, "Gustavo J. A. M. Carneiro" <gjc at inescporto.pt> wrote:
>  Hmm.. I'm beginning to think 13r16 or 16r13 look too cryptic to the
>casual observer; perhaps a suffix letter is more readable, since we
>don't need arbitrary radix support anyway.
>/me thinks of some examples:
>  644o # I _think_ the small 'o' cannot be easily confused with 0 or O,
>  10h  # hex.. hm.. but we already have 0x10
>  101b # binary
>  Another possility is to extend the 0x syntax to non-hex,
>   0xff   # hex
>   0o644  # octal
>   0b1101 # binary
>  I'm unsure which one I like better.
Sorry if I seem to be picking nits, but IMO there's more than a nit here:

The trouble with all of these is that they are all literals
for integers, but integers are signed, and there is no way
to represent the sign bit (wherever it is for a particular platform)
along with the others, without triggering a promotion to positive long.

So you get stuff like

 >>> def i32(i): return int(-(i&0x80000000))+int(i&0x7fffffff)
 >>> MYCONST = i32(0x87654321)
 >>> type(MYCONST)
 <type 'int'>
 >>> hex(MYCONST)
Oops ;-/
 >>> hex(MYCONST&0xffffffff)

instead of

    MYCONST = 16cf87654321

Hm... maybe an explicit ordinary sign _after_ the prefix would be more mnemonic
instead of indicating it with the radix-complement (f or 0 for hex). E.g.,

    MYCONST = 16r-87654321  # all bits above the 8 are ones


    MYCONST = 16r+87654321   # explicitly positive, all bits above 8 (none for 32 bits) are zeroes
    MYCONST = 16r87654321    # implicitly positive, ditto

or the above in binary

    MYCONST = 2r-10000111011001010100001100100001  # leading bits are ones (here all are specified for 32-bit int, but
                                                   # effect would be noticeable for smaller numbers or wider ints)
    MYCONST = 2r+10000111011001010100001100100001  # leading bits are zeroes (ditto)
    MYCONST = 2r10000111011001010100001100100001   # ditto

This could also be done as alternative 0x syntax, e.g. using 0h, 0o, and 0b,
but I sure don't like that '0o' ;-)

BTW, for non-power-of-two radices(?), it should be remembered that the '-'
is mnemonic for the symbol for (radix-1), and '+' or no sign is mnemonic for
a prefixed 0 (which is 0 in any allowable radix) in order to have this notation
have general radix expressivity for free ;-)

Bengt Richter

More information about the Python-Dev mailing list