This course will introduce you to C++ 11 and teach you about syntax fundamentals.

I have attached the code below, and don't understand what this line of code means:

error_bit = error_bit + (current_parity ^ (hamming [hamming_length-i ] - '0' )) * i;

Specifically, I don't understand what it means to subtract the '0' in the check_hamming_code function.

By the way, the user specifies:

hamming_length = 7

parity_type = 0

hamming = 1000110

error_bit = error_bit + (current_parity ^ (hamming [hamming_length-i ] - '0' )) * i;

Specifically, I don't understand what it means to subtract the '0' in the check_hamming_code function.

By the way, the user specifies:

hamming_length = 7

parity_type = 0

hamming = 1000110

```
void params() {
printf("Enter the length of the Hamming code: ");
scanf("%d", &hamming_length);
printf("Enter the parity (o=even, 1=odd): ");
scanf("%d", &parity_type);
hamming = (char *) malloc (hamming_length * sizeof(char));
return;
}
/* Option #2 */
void check_hamming_code() {
/* declare local var's */
int i,j,k;
/* prompt for hamming code string */
printf("Enter the Hamming code: ");
scanf("%s",hamming);
for(i=1; i < hamming_length; i*=2) {
/* init. something in i */
int current_parity = parity_type;
for(j=i; j <= hamming_length; j+=2*i) {
/* init. something in j */
for(k=j; (k <= hamming_length) && (k < i+j); k++) {
if(k != i)
current_parity = current_parity ^ hamming[hamming_length-k];
} /* end of k loop */
} /* end of j loop */
error_bit = error_bit + (current_parity ^ (hamming[hamming_length-i]-'0'))*i;
} /* end i loop */
/* correct error bit */
printf("%d",error_bit);
return;
}
```

Each 0x31's and 0x30's should represent a bit, but they are actually ASCII characters. In order to execute a bit operation, it is needed to convert each of them to bits, and it is done by subtracting '0' in the function. The '0' character value is 0x30.

Enter the Hamming Code: 1000110

There is an error in bit: 6 (error_bit)

The corrected Hamming code is: 1100110

My results show error_bit = 342

Can anyone see why?

Here is the part right above the code I attached at the begginning of this post:

#include <stdio.h>

#include <stdlib.h>

/* declare global var's */

int hamming_length=0, parity_type=-1, error_bit=0;

/* declare & initialize hamming string */

char *hamming = NULL;

/* Option #1 */

Line 36 had subtracted the '0', but line 33 did not.

Thanks everyone for your help. I might be back here soon, but for now closing this.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

>>check_hamming_code function.

The user input is given is ASCII (http://en.wikipedia.org/wiki/ASCII) and placed in a char array. Subtracting the ASCII value of '0' as done in the line in question converts the character at 'hamming[hamming_length-i]