So basically I'm creating a program that prompts the user to enter a character like 'A' and it returns their binary value according to the ASCII chart which in this case is 01000001. What should be outputted is a 12-bit hamming code sequence with 8 data bits and 4 parity bits. What I'm having trouble with is coming up with a way to insert the correct data bits into every array position that is basically not a power of 2. I should mention that the type of array is char.
So like it should print out: _ _ 0 _ 1 0 0 _ 0 0 0 1 with the blank spaces representing where the parity bits will go. Right now my code just prints out 000001000001 (without the parity values being determined so far) but I can't figure out a way to position each of the 8 data bits into a non-parity bit location.
I assumed I would have to edit my for loop which currently involves for(int i = 12; i >= 0; i--) but I can't seem to figure out a mathematical pattern I could use. I'm not sure if my current approach is possible either. Any hints or help would be appreciated.
Below is basically just a really rough outline of the part of code I'm struggling with:
for (int i=12; i>=0; i--) {
if ((int)(n/pow(2,i)) > 0) {
//index = 1
n = n - pow(2,i);
}
else
//index = 0
}
x & 7
clears all the bits except for the 3 LSBs. – user3386109