On Thu, 13 Jan 2005 08:18:25 -0500, Peter Hansen <[EMAIL PROTECTED]> wrote:
>[EMAIL PROTECTED] wrote: >> In Mythical Future Python I would like to be able to use any base in >> integer literals, which would be better. Example random syntax: >> >> flags= 2x00011010101001 >> umask= 8x664 >> answer= 10x42 >> addr= 16x0E800004 # 16x == 0x >> gunk= 36x8H6Z9A0X > >I think I kinda like this idea. Allowing arbitrary values, >however, would probably be pointless, as there are very >few bases in common enough use that a language should make >it easy to write literals in any of them. So I think "36x" >is silly, and would suggest limiting this to 2, 8, 10, and >16. At the very least, a range of 2-16 should be used. >(It would be cute but pointless to allow 1x000000000. :-) > My concern is negative numbers when you are interested in the bits of a typical twos-complement number. (BTW, please don't tell me that's platform-specific hardware oriented stuff: Two's complement is a fine abstraction for interpreting a bit vector, which is another fine abstraction ;-) One way to do it consistently is to have a sign digit as the first digit after the x, which is either 0 or base-1 -- e.g., +3 and -3 would be 2x011 2x101 8x03 8x75 16x03 16xfd 10x03 10x97 Then the "sign digit" can be extended indefinitely to the left without changing the value, noting that -3 == 97-100 == 997-1000) and similarly for other bases: -3 == binary 101-1000 == 111101-1000000 etc. IOW, you just subtract base**<number of digits in representation> if the first digit is base-1, to get the negative value. This would let us have a %<width>.<base>b format to generate the digits part and would get around the ugly hack for writing hex literals of negative numbers. def __repr__(self): return '<%s object at %08.16b>' %(type(self).__name__, id(self)) and you could write based literals in the above formats with e.g., with '16x%.16b' % number or '2x%.2b' % number etc. Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list