## Hamming Code

### Instructions

In this activity, you must write a program in RISC-V assembly language that performs the encoding and decoding of a Hamming(7, 4) code.

#### Encoding

For the first part of this exercise, you will receive a sequence of 4 bits, and you have to encode these data bits using the Hamming Code. Assuming that the 4 bit input is given as:

**d _{1}d_{2}d_{3}d_{4}**

The output will be

**p _{1}p_{2}d_{1}p_{3}d_{2}d_{3}d_{4}**

The new inserted bits with radical **p** are **parity** bits. Each one of the 3 parity bits is responsible for reflecting the parity of a given subset of bits (subset of 3 elements from the 4 available input bits). A parity bit is **1** if the evaluated set of bits has an **odd** number of **1**s, or **0** otherwise. The following table can be used as reference:

Parity Bit | Subset of tested bits |
---|---|

p_{1} | d_{1}d_{2}d_{4} |

p_{2} | d_{1}d_{3}d_{4} |

p_{3} | d_{2}d_{3}d_{4} |

#### Decoding

On the second part of this exercise, you will receive a sequence of 7 bits that has been encoded. You have to extract the data field from this sequence, and also check if the data contains an error caused by a bit flip (there is no need for correcting the data if an error is detected). For this error checking, you have to verify the parity of each one of the 3 subsets.

The XOR operator can be used for a given subset of bits. For instance, to check the parity for which p_{1} is responsible, p_{1} XOR d_{1} XOR d_{2} XOR d_{4} must be equal to 0. Otherwise, there is an error on the encoded data. Do this for the 3 subsets of bits in order to check if you can trust the data encoded with Hamming(7, 4).

### Input

**Line 1**- a sequence of 4 bits that must be encoded in a Hamming code using 3 parity bits, followed by a newline character (`\n`

).**Line 2**- a sequence of 7 bits that is Hamming encoded, and must be decoded and checked, followed by a newline character (`\n`

).

### Output

**Line 1**- sequence of 7 bits that has been encoded using Hamming code, followed by a newline character (`\n`

).**Line 2**- sequence of 4 bits that has been decoded from the Hamming code, followed by a newline character (`\n`

).**Line 3**- 1 if an error was detected when decoding the Hamming code, 0 otherwise, followed by a newline character (`\n`

).

### Examples

Test Case | Input | Output |
---|---|---|

1 | 1001 0011001 | 0011001 1001 0 |

2 | 0000 0000000 | 0000000 0000 0 |

3 | 0001 0010001 | 1101001 1001 1 |

4 | 1111 1001001 | 1111111 0001 1 |

5 | 1010 1011010 | 1011010 1010 0 |

### Notes and Tips

- Exclusive OR (XOR) is a logic operator that facilitates the computation of parity bits
- AND instruction is useful to leave only a given group of bits set (masking).
- The decoded data doesn't need to be corrected, in case an error is detected.
- You can test your code using the simulator's assistant from this link.