ENع
→Modular arithmetic
الدرس 05 · الأعداد في الحواسيب

Modular arithmetic

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

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

Modular arithmetic is counting that wraps around, like the hours on a clock. Walk a hand around a dial of 12 spots, see why 14 mod 12 is the remainder 2, and then watch a byte do the same thing with 256 spots: 250 + 10 leaves 4, because the ninth bit has no room.

ستتعلّم

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

شكل 5 · تفاعلي
01 / 09

Clock arithmetic

This dial counts 0 to 11, then starts again. Start at 9 and add 5 hours: 10, 11, 0, 1, 2. You land on 2, not 14. Counting that wraps around like this is called modular arithmetic.

>>> 9 + 5
14
>>> (9 + 5) % 12
2
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Clock arithmetic

    This dial counts 0 to 11, then starts again. Start at 9 and add 5 hours: 10, 11, 0, 1, 2. You land on 2, not 14. Counting that wraps around like this is called modular arithmetic.

    >>> 9 + 5
    14
    >>> (9 + 5) % 12
    2
  2. Mod is the remainder

    14 mod 12 means: divide 14 by 12 and keep the remainder. 14 ÷ 12 = 1, remainder 2, so 14 mod 12 = 2. The 12 is called the modulus. Python writes mod as %: 14 % 12 gives 2.

    >>> 14 % 12
    2
    >>> 14 // 12              # how many whole 12s fit
    1
  3. Laps and a leftover

    Any whole number splits into full laps of the dial plus a leftover: 14 = 1 × 12 + 2. The leftover is always 0 to 11, never 12 or more. divmod(14, 12) gives both parts: (1, 2).

    >>> divmod(14, 12)
    (1, 2)
    >>> 1 * 12 + 2
    14
  4. Same spot, same remainder

    2, 14 and 26 all land on the same spot, because each is 2 more than some full laps. We write 26 ≡ 2 (mod 12) and say 26 is congruent to 2, mod 12. They share the remainder 2.

    >>> [n % 12 for n in (2, 14, 26)]
    [2, 2, 2]
    >>> (26 - 2) % 12           # 12 divides the gap
    0
  5. Shrink big jumps first

    Big jumps can shrink first. 29 hours is 2 full laps plus 5, because 29 % 12 = 5. So 9 + 29 lands where 9 + 5 does: on 2. In code, (9 + 29) % 12 equals (9 + 5) % 12.

    >>> 29 % 12
    5
    >>> (9 + 29) % 12, (9 + 5) % 12
    (2, 2)
  6. Going backwards

    Subtracting walks the other way round. From 0, step back 3: 11, 10, 9. So -3 mod 12 is 9. Python agrees: -3 % 12 gives 9, because Python's % always answers 0 to 11 when the modulus is 12.

    >>> -3 % 12
    9
    >>> divmod(-3, 12)
    (-1, 9)
    >>> import math
    >>> math.fmod(-3, 12)       # the C-style answer
    -3.0
  7. A byte wraps at 256

    A byte has 8 bits, so it holds 0 to 255, then wraps like a dial with 256 spots. 250 + 10 = 260 needs a ninth bit, worth 256. A byte has no room for it, so it keeps 260 % 256 = 4.

    >>> 250 + 10
    260
    >>> bin(260)
    '0b100000100'
    >>> (250 + 10) % 256
    4
  8. Keep the low 8 bits

    Mod by a power of two is cheap. Because 256 = 2⁸, x % 256 is just the lowest 8 bits of x. The & operator keeps a bit only where both numbers have a 1, so 260 & 0xFF (eight 1s) gives 4.

    >>> 260 & 0xFF
    4
    >>> 260 % 256
    4
    >>> 173 % 2, 173 & 1        # the lowest bit
    (1, 1)
  9. Wrapping a list index

    Programs use % to cycle through slots forever. With 12 slots, i = (i + 1) % 12 counts 10, 11, 0, 1: it never runs past the last slot. Ring buffers (lists reused in a circle), clocks and calendars all work this way.

    >>> i = 10
    >>> for _ in range(3):
    ...     i = (i + 1) % 12
    ...     print(i)
    ...
    11
    0
    1

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