ENع
→Two's complement
الدرس 03 · الأعداد في الحواسيب

Negative numbers: two's complement

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةBeginner
عن هذا الدرس

A byte is just 8 bits, with no room for a minus sign. So how does a computer store −5? Follow 5 as its bits are flipped and 1 is added to make −5, check the answer with place values, then add 5 and −5 to watch the carry fall off and leave exactly 0.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 3 · تفاعلي
01 / 10

A byte holds 256 patterns

A byte is 8 bits, so it has 2 to the power 8 = 256 patterns. Read as plain binary, they mean 0 to 255. The back row holds 5: 00000101, which is the 4 bit plus the 1 bit.

>>> 2**8                  # patterns in one byte
256
>>> 0b00000101
5
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A byte holds 256 patterns

    A byte is 8 bits, so it has 2 to the power 8 = 256 patterns. Read as plain binary, they mean 0 to 255. The back row holds 5: 00000101, which is the 4 bit plus the 1 bit.

    >>> 2**8                  # patterns in one byte
    256
    >>> 0b00000101
    5
  2. A sign bit is not enough

    First idea: let the top bit be a minus sign, so 10000101 means −5. This is called sign and magnitude. But it has two zeros, 00000000 and 10000000, and plain binary addition gives wrong sums. Computers use a cleverer rule.

    >>> bin(0b00000101 + 0b10000101)   # 5 + "-5" in sign and magnitude
    '0b10001010'
    >>> 0b0001010                  # that answer would read as -10
    10
  3. The top bit is worth −128

    Two's complement keeps every place the same except the top one: it is worth −128 instead of 128. So 10000000 is −128, and 11111111 is −128 + 127 = −1. Small positive numbers keep their usual bits.

    >>> -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1     # 11111111
    -1
    >>> -128 + 0                                # 10000000
    -128
  4. Step 1: flip every bit

    To turn 5 into −5, first flip every bit: each 0 becomes 1 and each 1 becomes 0. 00000101 becomes 11111010, which is −6. Python's ~ operator does this flip: ~5 gives −6.

    >>> ~5
    -6
    >>> bin(~5 & 0xFF)          # the 8 flipped bits
    '0b11111010'
  5. Step 2: add one

    Then add 1: 11111010 + 1 = 11111011, and that is −5. Why? A number plus its flipped copy is always 11111111, which is −1. So the flip is one short, and adding 1 fixes it.

    >>> ~5 + 1
    -5
    >>> 5 + ~5                   # a number plus its flip
    -1
    >>> bin(-5 & 0xFF)
    '0b11111011'
  6. Check with place values

    Check it with the place values. The 1 bits are worth −128, 64, 32, 16, 8, 2 and 1. Adding them gives −128 + 123 = −5. The top bit says negative; the rest say how far above −128.

    >>> -128 + 64 + 32 + 16 + 8 + 2 + 1
    -5
    >>> 64 + 32 + 16 + 8 + 2 + 1
    123
  7. Add 5 and −5

    Now add 5 and −5 column by column, right to left, as on paper. 1 + 1 = 10 in binary: write 0, carry 1. Every column after that also makes 10, so the carry ripples out of the left end.

    >>> bin(0b00000101 + 0b11111011)
    '0b100000000'
    >>> len("100000000")         # one bit too many
    9
  8. The extra carry falls off

    The full answer, 100000000, needs 9 bits, but a byte has only 8. The extra carry falls off and is lost, leaving 00000000, which is 0. So 5 + (−5) = 0, using the same adder as for positive numbers.

    >>> 5 + 251                  # 251 is 11111011 read as plain binary
    256
    >>> (5 + 251) & 0xFF          # keep only the low 8 bits
    0
  9. From −128 to 127

    Eight bits in two's complement hold −128 to 127: still 256 patterns, now half of them negative. The largest is 01111111 = 127, the smallest is 10000000 = −128. A top bit of 1 always means negative.

    >>> -2**7, 2**7 - 1
    (-128, 127)
    >>> (-5).to_bytes(1, "big", signed=True)
    b'\xfb'
    >>> int.from_bytes(b"\xfb", "big", signed=True)
    -5
  10. Seeing the bits in Python

    Python integers have no fixed size, so −5 shows with a minus sign. To see its 8-bit pattern, keep the low 8 bits with & 0xFF: −5 & 0xFF is 251 (0b11111011). If it is 128 or more, subtract 256 to read it back.

    >>> -5 & 0xFF
    251
    >>> bin(-5 & 0xFF)
    '0b11111011'
    >>> 251 - 256
    -5

اختبار يتاح بعد الخطوة الأخيرة.