top of page

Chapitre 2 : Encodage des caractères et expressions booléennes 

  1. Les fonctions et portes logiques
  • Exemples en Python :

Exemple 1 :

def fonction1(x,y): 

     if x!=0 and y!=0:

          return 1/(x*y)

     else:

          return None

print(fonction1(0,3))

Exemple 2 :

def fonction2(x,y):

     if x==0 or y==0:

          return None

     else:

          return 1/(x*y)

print(fonction2(x,y))

2. Addition de deux nombres binaires
  • Algorithme Python

n=[1,0,1,0,1,0,1,0,1,0]
p=[1,0,1,0,1,0,1,0,1,0]
r=[0,0,0,0,0,0,0,0,0,0,0]
c=0

for i in range(9,-1,-1):
  a=n[i]
  b=p[i]
  print(i)
  r[i+1]=(a and not b and not c) or (not a and b and not c) or (not a and not b and c) or (a and b and c)
  c=(a and b) or (b and c) or (a and c)
r[0]=c
print(r)

a="1010101010"
b="1010101010"
sum=bin(int(a,2)+int(b,2))
print(sum)

bottom of page