@@ -382,7 +382,7 @@ modules.
382
382
.. _bitstring-ops :
383
383
384
384
Bitwise Operations on Integer Types
385
- --------------------------------------
385
+ -----------------------------------
386
386
387
387
.. index ::
388
388
triple: operations on; integer; types
@@ -396,9 +396,9 @@ Bitwise Operations on Integer Types
396
396
operator: >>
397
397
operator: ~
398
398
399
- Bitwise operations only make sense for integers. Negative numbers are treated
400
- as their 2's complement value (this assumes that there are enough bits so that
401
- no overflow occurs during the operation) .
399
+ Bitwise operations only make sense for integers. The result of bitwise
400
+ operations is calculated as though carried out in two's complement with an
401
+ infinite number of sign bits .
402
402
403
403
The priorities of the binary bitwise operations are all lower than the numeric
404
404
operations and higher than the comparisons; the unary operation ``~ `` has the
@@ -409,13 +409,13 @@ This table lists the bitwise operations sorted in ascending priority:
409
409
+------------+--------------------------------+----------+
410
410
| Operation | Result | Notes |
411
411
+============+================================+==========+
412
- | ``x | y`` | bitwise :dfn: `or ` of *x * and | |
412
+ | ``x | y`` | bitwise :dfn: `or ` of *x * and | (4) |
413
413
| | *y * | |
414
414
+------------+--------------------------------+----------+
415
- | ``x ^ y `` | bitwise :dfn: `exclusive or ` of | |
415
+ | ``x ^ y `` | bitwise :dfn: `exclusive or ` of | (4) |
416
416
| | *x * and *y * | |
417
417
+------------+--------------------------------+----------+
418
- | ``x & y `` | bitwise :dfn: `and ` of *x * and | |
418
+ | ``x & y `` | bitwise :dfn: `and ` of *x * and | (4) |
419
419
| | *y * | |
420
420
+------------+--------------------------------+----------+
421
421
| ``x << n `` | *x * shifted left by *n * bits | (1)(2) |
@@ -438,6 +438,12 @@ Notes:
438
438
A right shift by *n * bits is equivalent to division by ``pow(2, n) `` without
439
439
overflow check.
440
440
441
+ (4)
442
+ Performing these calculations with at least one extra sign extension bit in
443
+ a finite two's complement representation (a working bit-width of
444
+ ``1 + max(x.bit_length(), y.bit_length() `` or more) is sufficient to get the
445
+ same result as if there were an infinite number of sign bits.
446
+
441
447
442
448
Additional Methods on Integer Types
443
449
-----------------------------------
0 commit comments