-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparity_bit.py
More file actions
49 lines (45 loc) · 1.64 KB
/
parity_bit.py
File metadata and controls
49 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Ejercicio de examen nivel 3 - #1: Mecanismo simple bit de paridad.
@author: zejiran.
"""
def calcular_bit_paridad(grupos_bits: list, paridad: int) -> dict:
"""
Función que obtiene un diccionario con el valor de bit de paridad correspondiente
a cada grupo de bit de la lista recibida por parámetro.
Parámetros:
grupo_bits: list que contiene grupos de 8 bits representados como strings.
paridad: int paridad a usar, ya sea 0-par o 1-impar.
Retorno:
paridad_de_grupos: dict {'grupo_8_bits': bit_paridad_correspondiente}.
"""
# Se define el diccionario de retorno.
paridad_de_grupos = dict()
i = 0
# Inicio de recorrido
while i < len(grupos_bits):
# Inicializar variable de conteo de bits-1.
bits_1 = 0
# Contar número de bits-1 en grupo.
for cada_bit in grupos_bits[i]:
if cada_bit == '1':
bits_1 += 1
# Verificar paridad seleccionada.
# Paridad par.
if paridad % 2 == 0:
# Agregar bit de paridad de acuerdo al total de bits_1.
if bits_1 % 2 == 0:
paridad_de_grupos[grupos_bits[i]] = '0'
else:
paridad_de_grupos[grupos_bits[i]] = '1'
# Paridad impar.
else:
# Agregar bit de paridad de acuerdo al total de bits_1.
if bits_1 % 2 != 0:
paridad_de_grupos[grupos_bits[i]] = '0'
else:
paridad_de_grupos[grupos_bits[i]] = '1'
i += 1
return paridad_de_grupos
# Prueba.
# print(calcular_bit_paridad(['01010000', '00000001', '00000000'], 1))