Author rhettinger
Recipients mark.dickinson, rhettinger, talin
Date 2017-06-05.04:03:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The current 'b' formatting code in inconvenient for emulating, demonstrating, and teaching two's complement arithmetic.  The problem is that negative inputs are always formatted with a minus sign.  I propose that some formatting code be provided for fixed-width display where the leading bit is a sign bit.

For example, if code were a capital 'B' and the total width were 8-bits:

    >>> x = -12
    >>> format(12, '08B')

Currently, to achieve the same effect, one of the following is used:

    >>> format(x if x >= 0 else x + 2**8, '08b')


    >>> format(x & (2**8 - 1), '08b')

For values outside the valid range, perhaps a ValueError could be raised:

    >>> format(-200, '08B')
    Traceback (most recent call last):
    ValueError:  Expected value in range -128 <= x < 128, not -200

I'm not sure what the right code should be.  The idea of capital 'B' is attractive, but we already have a different relationship between 'X' and 'x'.   There could also be a modifier symbol such as '!' in '!8b'.
Date User Action Args
2017-06-05 04:03:26rhettingersetrecipients: + rhettinger, talin, mark.dickinson
2017-06-05 04:03:26rhettingersetmessageid: <>
2017-06-05 04:03:26rhettingerlinkissue30571 messages
2017-06-05 04:03:26rhettingercreate