Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Hyphenate 8-bit etc when appropriate
  • Loading branch information
jb2170 committed May 16, 2025
commit d1e47e47f4bdfaa4a007c913a6bc9b887e107d8b
18 changes: 9 additions & 9 deletions peps/pep-0786.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ Observe that one can always extend a signed number's binary representation by ex

.. code-block:: text

45 (8 bit) 00101101
45 (9 bit) 000101101
-19 (8 bit) 11101101
-19 (9 bit) 111101101
45 (8-bit) 00101101
45 (9-bit) 000101101
-19 (8-bit) 11101101
-19 (9-bit) 111101101

For non-negative numbers this is obvious. For negative numbers this is because the erstwhile leading column of an ``n`` bit representation goes from having a value of ``-2 ** (n-1)``, to ``+2 ** (n-1)``, with a new ``n+1``\ th column of value ``-2 ** n`` prefixed on, the overall sum unaffected.
For non-negative numbers this is obvious. For negative numbers this is because the erstwhile leading column of an ``n``\ -bit representation goes from having a value of ``-2 ** (n-1)``, to ``+2 ** (n-1)``, with a new ``n+1``\ th column of value ``-2 ** n`` prefixed on, the overall sum unaffected.

This is what C's ``printf`` does, working with powers of two as the numbers of digits:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might note, that "b" format type is available since the C23. (Works for me with -std=c2x one recent gcc.)


Expand All @@ -113,10 +113,10 @@ Conversely it should be clear that one can losslessly truncate a signed number's

.. code-block:: text

45 (8 bit) 00101101
45 (7 bit) 0101101
-19 (8 bit) 11101101
-19 (7 bit) 1101101
45 (8-bit) 00101101
45 (7-bit) 0101101
-19 (8-bit) 11101101
-19 (7-bit) 1101101

If one were to truncate another digit off of these examples, then both would end up as ``101101``, 45 indistinguishable from -19 when using only 6 binary digits because they are both the same modulo ``2 ** 6 = 64``. Therefore to losslessly and unambiguously represent a signed integer ``x`` as a binary string which is rendered to the end user, we have a defacto 'minimal width' representation convention, using ``n`` digits, where ``n`` is the smallest integer such that ``x`` is in ``range(-2 ** (n-1), 2 ** (n-1))``.

Expand Down