En 1847, le britannique George Boole inventa un formalisme
permettant d'écrire des raisonnements logiques : l'algèbre de Boole.
Cette algèbre ne contient que deux valeurs et 3 opérations de base.
En 1938, l'américain Claude Shannon prouva dans sa thèse que
des circuits électriques peuvent résoudre tous les problèmes que l'algèbre de Boole
peut elle-même résoudre.
La route était ouverte à l'apparition des premiers ordinateurs électroniques.
Les booléens ne possèdent que deux valeurs, codés sur un seul bit :
False, false …True, true …not a (Python)| a | \lnot a |
|---|---|
| 0 | |
| 1 |
a and b (Python)a & b (bit à bit)| a | b | a \wedge b |
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
| a | b | a \wedge b |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
>>> (1 + 2 == 3) and (7 + 1 == 8) >>> (1 + 2 == 3) and (1 + 1 == 3)
>>> (7 / 0 == 1) and (4 / 2 == 2) >>> (1 + 1 == 3) and (7 / 0 == 2)
>>> (1 + 2 == 3) and (7 + 1 == 8) # True and True → True >>> (1 + 2 == 3) and (1 + 1 == 3) # True and False → False
>>> (7 / 0 == 1) and (4 / 2 == 2) # ZeroDivisionError ! >>> (1 + 1 == 3) and (7 / 0 == 2) # False → False (court-circuit)
False, Python n'évalue pas la seconde.
a or b (Python)| a | b | a \lor b |
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
>>> n = 20 >>> (n % 10 == 0) or (n % 7 == 0) >>> (n % 4 == 0) or (n % 5 == 0) >>> (n % 7 == 0) or (n % 3 == 0)
>>> n = 20 >>> (n % 5 == 0) or (n % 0 == 0)
>>> (n % 10 == 0) or (n % 7 == 0) # True or False → True >>> (n % 4 == 0) or (n % 5 == 0) # True or True → True >>> (n % 7 == 0) or (n % 3 == 0) # False or False → False
>>> (n % 5 == 0) or (n % 0 == 0) # True → True (court-circuit)
True, Python n'évalue pas la seconde
(la division par zéro est évitée).
| a | \lnot a |
|---|---|
| 0 | |
| 1 |
>>> n = 20 >>> not(n % 10 == 0)
| a | \lnot a |
|---|---|
| 0 | 1 |
| 1 | 0 |
>>> not(n % 10 == 0) # not(20 % 10 == 0) = not(True) = False
a ^ b (Python, bit à bit)| a | b | a \wedge \lnot b | \lnot a \wedge b | a \oplus b |
|---|---|---|---|---|
| 0 | 0 | |||
| 0 | 1 | |||
| 1 | 0 | |||
| 1 | 1 |
| a | b | a \wedge \lnot b | \lnot a \wedge b | a \oplus b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 0 |
not(a and b) (Python)| a | b | a \wedge b | \lnot(a \wedge b) |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
not(a or b) (Python)| a | b | a \lor b | \lnot(a \lor b) |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
1011011 & 1101010 --------- ?
1011011 | 1010101 --------- ?
1011011 ^ 1010101 --------- ?
>>> 91 & 106 >>> 91 | 85 >>> 91 ^ 85
1011011 (91) & 1101010 (106) --------- 1001010 (74)
1011011 (91) | 1010101 (85) --------- 1011111 (95)
1011011 (91) ^ 1010101 (85) --------- 0001110 (14)
>>> 12 & 7 # → 4 >>> 12 | 7 # → 15 >>> 12 ^ 5 # → 9
Le chiffre XOR est une méthode de chiffrement symétrique :
on chiffre et déchiffre avec la même clé, en appliquant XOR deux fois.
i :
ord(message[i]) et ord(masque[i])chr()ord('B') = 66 → 66 - 64 = ?
ord('L') = 76 → 76 - 64 = ?
? ^ ? = ?
? + 64 = ? → chr(?) = ?
chiffrement(message, masque).chiffrement(chiffrement("BONJOUR","LPOSADA"),"LPOSADA") == "BONJOUR".
ord('B') = 66 → 2 ord('L') = 76 → 12
2 ^ 12 = 14 → 14 + 64 = 78 → chr(78) = 'N'
def chiffrement(message, masque):
resultat = ""
for i in range(len(message)):
code_msg = ord(message[i]) - 64
code_cle = ord(masque[i % len(masque)]) - 64
resultat += chr((code_msg ^ code_cle) + 64)
return resultat
chiffrement(chiffrement(m, k), k) == m
True ?
False and (True and False)False or (True and False)True and (True and False)True or (True and False)
True or (True and False)True or False (court-circuit : True or … → True)a = False et b = True,
que vaut not(a and b) ?
0
B — False
C — True
D — None
a and b = False and True = Falsenot(False) = True
not(a or b) vaut True,
quelles peuvent être les valeurs de a et b ?
not(a or b) = Truea or b = Falsea = False ET b = Falsea et b deux booléens.not(a and b) or a est équivalente à :
False
B — True
C — not(b)
D — not(a) or not(b)
not(a and b) = not(a) or not(b)[not(a) or not(b)] or anot(a) or a or not(b)True or not(b) (tiers-exclu)| a | b | a \lor b | \lnot(a \lor b) |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
| a | b | \lnot a | \lnot b | \lnot a \wedge \lnot b |
|---|---|---|---|---|
| 0 | 0 | |||
| 0 | 1 | |||
| 1 | 0 | |||
| 1 | 1 |
Message du popup !