Author rhettinger
Recipients mark.dickinson, rhettinger, talin
Date 2017-06-05.04:03:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1496635406.76.0.985439209964.issue30571@psf.upfronthosting.co.za>
In-reply-to
Content
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')
    '11110100'

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

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

or 

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

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'.
History
Date User Action Args
2017-06-05 04:03:26rhettingersetrecipients: + rhettinger, talin, mark.dickinson
2017-06-05 04:03:26rhettingersetmessageid: <1496635406.76.0.985439209964.issue30571@psf.upfronthosting.co.za>
2017-06-05 04:03:26rhettingerlinkissue30571 messages
2017-06-05 04:03:26rhettingercreate