[docs] [issue29710] Incorrect representation caveat on bitwise operation docs
Tim Peters
report at bugs.python.org
Sun Jul 15 03:32:52 EDT 2018
Tim Peters <tim at python.org> added the comment:
Well, all 6 operations "are calculated as though carried out in two's complement with an infinite number of sign bits", so I'd float that part out of the footnote and into the main text. When, e.g., you're thinking of ints _as_ bitstrings, it's essentially useless to think of `n >> 13` as being equivalent to `n // 2**13`.
Quick: what's ~0 >> 13? Well, ~0 is an infinite string of 1 bits, so shifting it right by any finite number of bits doesn't change it. That, mathematically, floor(~0 / 8192) = -1 is only interesting if you're thinking of ~0 as being an integer instead.
And, if you are, _then_ you need to know that the infinite string of 1 bits ~0 represents is viewed as being a 2's-complement representation of -1. But so long as you're sticking to the bitstring view, the "2's complement" part is irrelevant to anything these 6 operations do.
Indeed, in the very earliest versions of Python, longs (but not ints!) were viewed as being 1's-complement infinite bitstrings, but "infinite string of sign bits" was just as applicable to what these operations did then. The meaning of what these operations compute _as bitstrings_ is independent of how bitstrings are mapped to and from integers. When longs changed from 1's-comp to 2's-comp only the latter changed; code using longs as bitstrings wasn't affected.
So, in all, there's quite a bit of background these telegraphic docs are glossing over. You (Nick) don't seem to ever think of them as being bitstrings, but what the "bitwise operators" do was driven by the bitstring view. That some of them can also be defined by arithmetic (+ - * /) is secondary. It may well take more text to get that all across than is suitable here, though.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue29710>
_______________________________________
More information about the docs
mailing list