Modular Arithmetic
(合同算術)
Discrete Mathematics I
13th lecture, Dec. 18, 2015
http://www.sw.it.aoyama.ac.jp/2015/Math1/lecture13.html
Martin J. Dürst
© 2005-15 Martin
J. Dürst Aoyama Gakuin
University
Today's Schedule
- Remaining schedule
- Summary and homework for last lecture
- Applications of bitwise operations
- Addition in different numeral systems
- Modular Arithmetic
- Modulo operation
- Digit sums and digital roots
Remaining Schedule
- January 8: 14th lecture
- January 15: 15th lecture
- (January 22: makeup classes)
- January 26 to February 2: Final exams
About makeup classes: The material in the makeup class is part of the final
exam. If you have another makeup class at the same time, please inform the
teacher as soon as possible.
補講について:
補講の内容は期末試験の対象。補講が別の補講とぶつかる場合には事前に申し出ること。
Questions about Final Exam
Summary of Algebraic Structures
(hierarchy of objects)
Applications of Bitwise Operations
- Representation of sets and operations on sets
(finite universal set, each bit position represents one element in the
universal set)
- Storage and and check of properties
(finite set of properties, each bit position represents one specific
property)
- Detailled operations on data
(example: data transformations, data compression)
Operations work on 8, 16, 32, or 64 bits concurrently
Other Bitwise Operations
- Left shift:
b << n
in C and many other programming
languages
- Shifts each of the bits in
b
by n
positions
to the left (more significant direction)
n
bits its on the right are set to 0
- The leftmost
n
bits in b
disappear
- If there is no overflow (disappearing 1 bits),
b <<
n
is equivalent to b ×2n
- Right shift:
b >> n
in C and many other programming
languages
- Shifts each of the bits in
b
by n
positions
to the right (less significant direction)
n
bits on the left are set to 0 or to the value of the
leftmost bit in b
,
depending on programming lanugage and data type
- The rightmost
n
bits in b
disappear
b >> n
is equivalent to b /
2n
Applications of Bitwise Operations
- In bitstring a, set bits from mask m:
a | m
- In bitstring a, clear bits from mask m:
a & ~m
- In bitstring a, invert bits from mask m:
a ^ m
- In bitstring a, set bit number n:
a | (1<<n)
(rightmost bit is number 0)
- In bitstring a, clear bit number n:
a & ~(1<<n)
(rightmost bit is number 0)
- In bitstring a, invert bit number n:
a ^ (1<<n)
(rightmost bit is number 0)
For many more advanced examples, see Hacker's Delight, Henry S. Warren, Jr.,
Addison-Wesley, 2003
Addition in Different Numeral Systems
Works the same as in decimal system:
- Progress from least signifinant digit to more significant digits
- Carry over when base (e.g. 10) is reached
Example (base 7):
Operand 1 |
|
3 |
6 |
5 |
1 |
2 |
Operand 2 |
|
6 |
0 |
3 |
3 |
4 |
carry |
1 |
1 |
1 |
|
|
|
Result |
1 |
3 |
0 |
1 |
4 |
6 |
Addition using Bitwise Operations
Single Digit Addition
|
0 |
1 |
0 |
0 |
1 |
1 |
1 |
10 |
- For inputs a=a0 and
b=b0, calculate
the digit-wise sum (without carry) as s0 =
a0 ^
b0, and
the digit-wise carry as c0 = a0
&
b0
- Setting a1 = s0 and
b1 = c0
<<
1, repeat until c is 0
- Example: a0 = 1011101、b0 =
1101111
Modular Arithmetic
Congruence Relation
- Each modulus n creates a congruence relation on ℤ
- Congruence relations are equivalence relations
- The equivalence classes are called congruence classes or residue
class
- The representatives k of the congruence classes are usually 0
≦ k < n
- The representatives are the result of the modulo operation (Attention:
depends on definition for negative numbers)
Properties of Congruence Equations
- a ≡ b ∧ c ≡ d ⇒
a + c ≡ b + d (mod
n)
- a ≡ b ∧ c ≡ d ⇒
a - c ≡ b - d (mod
n)
- a ≡ b ∧ c ≡ d ⇒
ac ≡ bd (mod
n)
- a ≡ b ⇒ am ≡
bm (mod n)
- a ≡ b ∧ c ≡ d
⇏ a / c ≡ b /
d (mod n)
Properties of the Modulo Operation
- (a + c) mod n = (a mod
n + c mod n) mod n (addition
modulo n)
- (a - c) mod n = (a mod
n - c mod n) mod n
(substraction modulo n)
- (a · c) mod n = (a mod
n · c mod n) mod n
(multiplication modulo n)
Reason: a ≡(mod n) a mod
n, and so on
Congruence and Groups
- Addition modulo n creates a group on the congruence classes
(cyclic group)
- Multiplication modulo n creates a group of size n-1
on the congruence classes except 0 if n is prime
(used e.g. in Diffie-Hellman
encryption)
mod 5 |
1 |
2 |
3 |
4 |
1 |
1 |
2 |
3 |
4 |
2 |
2 |
4 |
1 |
3 |
3 |
3 |
1 |
4 |
2 |
4 |
4 |
3 |
2 |
1 |
Congruence and Division
Division for rationals: a/b = c
⇔a b-1 = c; b
b-1 = 1 (inverse)
Modular multiplicative inverse: bb-1 ≡
1
n |
b |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
2 |
- |
1 |
|
|
|
|
|
|
3 |
- |
1 |
2 |
|
|
|
|
|
4 |
- |
1 |
- |
3 |
|
|
|
|
5 |
- |
1 |
3 |
2 |
4 |
|
|
|
6 |
- |
1 |
- |
- |
- |
5 |
|
|
7 |
- |
1 |
4 |
5 |
2 |
3 |
6 |
|
8 |
- |
1 |
- |
3 |
- |
5 |
- |
7 |
Only defined if n and b are coprime (i.e. GCD is 1)
Various methods to calculate
a/b (mod n) is defined as a
b-1 (mod n)
Example: 3/4 (mod 7) = 3 · 4-1 (mod 7) = 3 · 2 (mod 7) = 6
(Check: 6 · 4 (mod 7) = 24 (mod 7) = 3)
Modulo Operation
- Operator:
%
(C, Ruby and many other programming
languages)、mod (Mathematics)
- Definitions for negative numbers:
|
remainder for negative operands |
operands |
always non-negative |
same sign as divisor |
same sign as dividend |
a |
n |
q (a/n) |
r (a%n) |
q (a/n) |
r (a%n) |
q (a/n) |
r (a%n) |
11 |
7 |
1 |
4 |
1 |
4 |
1 |
4 |
-11 |
7 |
-2 |
3 |
-2 |
3 |
-1 |
-4 |
11 |
-7 |
-1 |
4 |
-2 |
-3 |
1 |
4 |
-11 |
-7 |
2 |
3 |
1 |
-4 |
-1 |
-4 |
Programming languages,... |
Pascal, this lecture |
Ruby, Python, MS Excel |
C (ISO 1999), Java, JavaScript, Perl, PHP |
- In some programming languages (e.g. C before ISO 1999), the result is
implementation-defined
- Some programming languages offer more than one function
- Always true: qn+r=a ∧
|r|<|n|
- a ≡n b ⇔ a mod
n = b mod n only true for "always
non-negative"
See English Wikipedia article on Modulo Operation
An Example of Using the Modulo Operation
Output some data items, three items on a line.
A simple way:
int items = 0;
for (i=0; i<length; i++) {
/* print out data[i] */
items++;
if (items==3) {
printf("\n");
items = 0;
}
}
Using the modulo operation:
for (i=0; i<length; i++) {
/* print out data[i] */
if (i%3 == 2) printf("\n");
}
Application of Congruence: Simple Calculation of Remainder
Example: 216 mod 29 = ?
216 = 25 · 25 · 25 ·
2
216 = 25 · 25 · 25 · 2 = 32
· 32 · 32 · 2 ≡(mod 29) 3 · 3 · 3 · 2 = 54 ≡(mod
29) 25
Homework
Prepare for final exam
Glossary
- rotation
- 回転
- reflection
- 反射
- hierarchy
- 階層
- concurrent
- 同時
- shift
- シフト
- invert
- 逆転、反転
- modular arithmetic
- 合同算術
- congruence (equation)
- 合同式
- modulus
- 法
- residue
- 剰余
- modulo operation
- 剰余演算
- congruence relation
- 合同関係
- congruence class
- 合同類
- residue class
- 剰余類
- cyclic group
- 巡回群
- modular multiplicative inverse
- モジュラー逆数
- operator
- 演算子
- dividend
- 被除数
- divisor
- 除数
- implementation
- 実装