i have a binary string and i would like to perform a xor operation consequentially on several bits of that string. my string is :
011001100011100000000011
i am trying to perform the calculation using the next line of code:
private String ParityCalc(String str){
char[] cA = str.toCharArray();
int[] D = new int[6];
D[0] = D29^cA[0]^cA[1]^cA[2]^cA[4]^cA[5]^cA[9]^cA[10]^cA[11]^cA[12]^cA[13]^cA[16]^cA[17]^cA[19]^cA[22];
D[1] = D30^cA[1]^cA[2]^cA[3]^cA[5]^cA[6]^cA[10]^cA[11]^cA[12]^cA[13]^cA[14]^cA[17]^cA[18]^cA[20]^cA[23];
D[2] = D29^cA[0]^cA[2]^cA[3]^cA[4]^cA[6]^cA[7]^cA[11]^cA[12]^cA[13]^cA[14]^cA[15]^cA[18]^cA[19]^cA[21];
D[3] = D30^cA[1]^cA[3]^cA[4]^cA[5]^cA[7]^cA[8]^cA[12]^cA[13]^cA[14]^cA[15]^cA[16]^cA[19]^cA[20]^cA[22];
D[4] = D30^cA[0]^cA[2]^cA[4]^cA[5]^cA[6]^cA[8]^cA[9]^cA[13]^cA[14]^cA[15]^cA[16]^cA[17]^cA[20]^cA[21]^cA[23];
D[5] = D29^cA[2]^cA[4]^cA[5]^cA[7]^cA[8]^cA[9]^cA[10]^cA[12]^cA[14]^cA[18]^cA[21]^cA[22]^cA[23];
for (int i = 0; i < 6; i++){
if (D[i] == 48){
D[i] = 0;
} else if (D[i] == 49){
D[i] = 1;
}
}
StringBuilder parity = new StringBuilder();
parity.append(D[0]).append(D[1]).append(D[2]).append(D[3]).append(D[4]).append(D[5]);
D29 = D[4];
D30 = D[5];
return parity.toString();
}
the result that i am getting for the final parity is: 100000. the correct result should be: 001001.
the D29 and D30 are parity bits carried on from previous calculations, both are integers.
what am i doing wrong and how can i fix it? i should probably do it as a bitwise operation but i cant seem to figure it out. any help would be appreciated.
That would be my approach:
private String ParityCalc(String str){
int input = Integer.parseInt(str,2);
int[] D = new int[6];
D[0] = input & (int)0x4b3e37; // Mask for indices 0,1,2,4,5,9,10,11,12,13,16,17,19,22
D[0] = (Integer.bitCount(D[0])&0x1)^D29; // Parity of masked input XOR D29
// D[1-5] accordingly
StringBuilder parity = new StringBuilder();
parity.append(D[0]).append(D[1]).append(D[2]).append(D[3]).append(D[4]).append(D[5]);
D29 = D[4];
D30 = D[5];
return parity.toString();
}
Mask: 0,1,2,4,5,9,10,11,12,13,16,17,19,22
3 3 2 1 210987654321098765432109876543210 "Position" 000000000010010110011111000110111 BIN 0 0 4 B 3 E 3 7 Hex (4 digits bin = 1 Hex)