(合同算術)

https://www.sw.it.aoyama.ac.jp/2019/Math1/lecture13.html

© 2005-20 Martin J. Dürst Aoyama Gakuin University

- Remaining schedule
- Homework and summary of last lecture
- Applications of bitwise operations
- Addition in different numeral systems
- Modular arithmetic
- Modulo operation
- Student Survey

- January 10: this lecture
- January 17: 14th lecture (makeup class)
- January 24: 15th lecture
- January 31,
**11:10**-12:35: Term final exam

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 now.

補講について: 補講の内容は期末試験の対象。補講が別の補講とぶつかる場合には今すぐ申し出ること。

By using formula manipulation, show that the Wolfram axiom of Boolean logic
(((` A`⊼

Hints: Simplify the left side to obtain the right side. There should be between 15 and 20 steps.

(`A`⊼`B`)⊼`C`)⊼(`A`⊼((`A`⊼`C`)⊼`A`)) [definition of ⊼, six times]

= ¬(¬(¬(` A`∧

= ¬¬(¬(

= ¬(

= (¬

= (¬

= (¬

= (¬

= (¬

= (¬

= (¬

= (¬

= ((¬

= (¬

= (T∨¬

= T∧

=

The left side of the Wolfram axiom can be transformed to the right side
using only the laws of Boolean logic. This means that the Wolfram axiom is a
tautology. `∴`

- Underlines indicate parts affected by applying a law. Underlines are not part of the solution.
- When indicating laws, give the law's name, not just some formula

(i.e. DeMorgan's law,**not**¬(∧`A``B`) = ¬∨¬`A``B`) - When using DeMorgan's law to push negations inwards, start with the outermost negation. This often makes it possible to remove double negations early.
- Indicate steps using associative/commutative laws explicitly.

Draw the Hasse diagram of a Boolean algebra of order 4 (16 elements). There will be a deduction if you use the same elements of the group as another student.

Solution: For an example, see handout of last lecture

- Boolean algebra is (an example of) an algebraic structure
- Boolean algebras have one set (
`B`), two special elements (**0**and**1**), and three operations (⫬, △, and ▽) - The axioms of boolean algebra are the same as the axioms of basic logic; there are various choices with tradeoffs between obviousness and compactness
- Boolean algebras are half-orders and lattices
- There are many examples of Boolean algebras: basic logic (with logic operations), bit strings (with bitwise logic operations), powersets (with set operations), certain sets of integers (with divisibility), gardens with flowers,...
- All finite Boolean algebras are of size 2
^{n}, and are isomorphic to an`n`-dimensional cube

(hierarchy of objects)

- Representation of sets and operations on sets

(finite universal set, each bit position represents one element in the universal set) - Storage and check of (binary) properties

(finite set of properties, each bit position represents one specific property)

(examples: CPU features, character conversion UPPER↔lower case) - Detailled operations on data

(example: data transformations, data compression)

Operations work on 8, 16, 32, 64 or more bits concurrently

- 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 on the right are set to 0- The leftmost
`n`

bits in`b`

disappear - If there is no overflow ('1' bits disappearing to the left),
`b << n`

is equivalent to`b`×2^{n} - Example: '101 1001' << 5 ⇒ '1011 0010 0000' (in actual
program:
`89 << 5`

⇒`2848`

)

- 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`/ 2^{n}(integer division)- Example: '101 1001' >> 3 ⇒ '1011' (in actual program:
`89 >> 3`

⇒`11`

)

- 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

Works the same as in the 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 | |||

sum in base 10 | 1 | 10 | 7 | 8 | 4 | 6 |

sum in base 7 | 1 | 13 | 10 | 11 | 4 | 6 |

Result | 1 | 3 | 0 | 1 | 4 | 6 |

0 | 1 | |
---|---|---|

0 | 0 | 1 |

1 | 1 | ^{1}0 |

- For inputs
`a`=`a`_{0}and`b`=`b`_{0}, calculate

the digit-wise sum (without carry) as`s`_{0}_{}=`a`_{0}`^`

`b`_{0},

and the digit-wise carry as`c`_{0}=`a`_{0}`&`

`b`_{0} - Setting
`a`_{1}=`s`_{0}and`b`_{1}=`c`_{0}`<<`

1, repeat until`c`=0

Example: `a`_{0} = 47 = 101111、`b`_{0} =
58 = 111010

x |
0 | 1 | 2 | |

a_{x} (s_{x-1}) |
101111 | 010101 | 1000001 | |

b_{x}
(c_{x-1}`<<1` ) |
111010 | 1010100 | 0101000 | |

s_{x}
(a_{0}`^` b_{0}) |
010101 | 1000001 | 1101001 | = 105 (result) |

c_{x}
(a_{0}`&` b_{0}) |
101010 | 010100 | 0000000 | = 0 (finished) |

c_{x}`<<1` |
1010100 | 0101000 | 00000000 |

- Discovered and published in 1801 by Gauss
- Universal set is ℤ (integers)
*Congruence*: Two integers are congruent modulo`n`if they have the same remainder when divided by`n`.

(attention: for negative numbers, depends on definition of modulo operation)- Congruence equation:
`a`≡_{(mod n)}`b`⇔`a`mod`n`=`b`mod`n`⇔`a`=`q`_{1}`n`+`r`∧`b`=`q`_{2}`n`+`r`∧ 0 ≦`r`<`n`⇔`a`-`b`=`q``n` `n`is called*modulus*(`n`≠0)`r`is called*residue*`a`≡_{(mod n)}`b`is also written`a`≡`b`(mod`n`) or`a`≡`b`(modulo`n`) or`a`≡_{n}`b`

- Each modulus
`n`creates a*congruence relation*on ℤ - Congruence relations are equivalence relations
- The equivalence classes are called
*congruence class*es or*residue class*es - The representative
`k`of a congruence class is usually choosen so that 0 ≦`k`<`n` - The representatives are the result of the modulo operation

`a`≡`b`∧`c`≡`d`⇒`a`+`c`≡`b`+`d`(mod`n`)`a`≡`b`∧`c`≡`d`⇒`a`-`c`≡`b`-`d`(mod`n`)`a`≡`b`∧`c`≡`d`⇒`a``c`≡`b``d`(mod`n`)`a`≡`b`⇒`a`^{m}≡`b`^{m}(mod`n`)- But:
`a`≡`b`∧`c`≡`d`**⇏**`a`/`c`≡`b`/`d`(mod`n`)

- (
`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

Where to use: `a` and `c` may be very large numbers, but
`a` mod `n` and `c` mod `n` may be much
smaller, so calculation becomes easier.

- 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 (dividend)``n (divisor)``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, Scheme, **mathematics, this lecture**Ruby, Python, Perl, Excel C (ISO 1999), Java, JavaScript, 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:
`q``n`+`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

Output some data, **three items on a line**.

A simple way:

int items = 0; for (i=0; i<count; i++) { /* print out data[i] */ items++; if (items==3) { printf("\n"); items = 0; } }

Using the modulo operation:

for (i=0; i<count; i++) { /* print out data[i] */ if (i%3 == 2) printf("\n"); }

- Addition modulo
`n`creates a group on the congruence classes (*cyclic group*) - If
`n`is prime, multiplication modulo`n`creates a group of size`n`-1 on the congruence classes except 0

(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

Division and inverse for rationals: `a`/`b` = `c`
⇔ `a`·1/`b` = `a` `b`^{-1} =
`c`

Condition for (*multiplicative*) *inverse*
`b`^{-1}: `b` `b`^{-1} = 1

Condition for *modular multiplicative inverse*:
`b``b`^{-1} ≡ 1

n |
b |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|---|---|---|---|---|---|---|---|---|---|

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 | ||

9 | - | 1 | 5 | - | 7 | 2 | - | 4 | 8 |

The modular multiplicative inverse is only defined if `n` and
`b` are coprime (i.e. GCD(`n`, `b`) = 1)

Various methods to calculate, very time-consuming

`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)

Example: 2^{16} mod 29 = ?

2^{16} = 2^{5} · 2^{5} · 2^{5} ·
2^{}

2^{16} = 2^{5} · 2^{5} · 2^{5} · 2 =
32 · 32 · 32 · 2 ≡_{(mod 29)} 3 · 3 · 3 · 2 = 54
≡_{(mod 29)} 25

3^{18} mod 7 = ?

3^{18} = (3^{3})^{6} =
27^{6} ≡_{(mod 7)}
(-1)^{6} = 1

41^{10} mod 37 = ?

41^{10} ≡_{(mod 37)}
4^{10} = 2^{20} = 32^{4} ≡_{(mod
37)} (-5)^{4} = 25^{2}
≡_{(mod 37)} (-12)^{2} = (6 · 2)^{2} = 36 ·
4 ≡_{(mod 37)} (-1) · 4 = -4 ≡_{(mod 37)}
33

Prepare for final exam

(授業改善のための学生アンケート)

WEB Survey

お願い: 自由記述に必ず良かった点、問題点を具体的に書きましょう

(悪い例: 発音が分かりにくい; 良い例: さ行が濁っているかどうか分かりにくい)

- 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
- モジュラー逆数
- coprime
- 互いに素
- operator
- 演算子
- dividend
- 被除数
- divisor
- 除数
- implementation
- 実装