0

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.

  • 1
    Why not convert the string to integer and do it again? – Horsing May 11 '16 at 07:52
  • because the operation is sequential on several bits. how would you sujest to do it? –  May 11 '16 at 07:56
  • First of all, the original binary string could be converted to integer, specific bits to be extracted could also be constructed to another integer. It depends. – Horsing May 11 '16 at 07:59
  • To make it clear, suppose the original binary string is A, several bits of A construct B, now you can make the factor by shift `0x1` properly and do xor operation. – Horsing May 11 '16 at 08:03
  • lets say i will convert the string to integer using `Interger.parseInt(str)`, how will i perform the xor on different bits then? can you provide an example? –  May 11 '16 at 08:03
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Fildor May 11 '16 at 08:07
  • @Fildor i already read that page. did not help me much. i just cant figure it out, that is why i asked for help. –  May 11 '16 at 08:10
  • Yes, sorry. I wanted to write more then figured out, my understanding of what you want to do was wrong. – Fildor May 11 '16 at 08:22
  • i'll try that, thank you. –  May 11 '16 at 08:41
  • Just now I saw you are at the moment XORing not with 0 or 1 but with the char value of '0' and '1'. That makes it more complicated. – Fildor May 11 '16 at 08:44
  • [bitCount](https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#bitCount(int)) could be helpful ... You can mask the input int, count "1"s and XOR with D29/D30 ... – Fildor May 11 '16 at 10:00

2 Answers2

0

I've tried following code, it it what you want?

public static void xor () {
    final String a = "011001100011100000000011";
    final String b = a.substring(3, 7);
    final long ai = Long.parseLong(a, 2);
    final long bi = Long.parseLong(b, 2);
    final long la = Long.toBinaryString(ai).length();
    final long lb = Long.toBinaryString(bi).length();
    long i,j,fa,fb,fo,result = ai;
    for (i = 0; i < lb; ++ i) {
        // get most significant bit one by one; a
        fb = 1l & (bi >> (lb - i - 1l));
        for (j = 0; j < la; ++ j) {
            // get most significant bit one by one; b
            fa = 1l & (ai >> (la - j - 1l));
            // one ^ one
            fo = fa ^ fb;
            if (0 == fo) {
                // & 0
                result &= ((-1l << la - j) | ((1l << (la - j - 1)) - 1));
            } else {
                // | 1
                result |= (1l << (la - j - 1l));
            }
        }
    }
    System.out.println(result);
}

Solution:

Xor each bit of two binary string(will be converted to integer) and reset each bit back to original integer that converted from original binary string (can be a new integer, it depends).

Please let me know, if any problem.

Horsing
  • 1,070
  • 7
  • 22
  • i have try to run this code as is, and it does nothing. the result eventually equals to the ai parameter. which is very strange. –  May 11 '16 at 13:28
  • This example can help you understand bit operations. Enjoy yourself. – Horsing May 11 '16 at 14:18
0

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)
Fildor
  • 14,510
  • 4
  • 35
  • 67
  • how did you generate the mask? i don't quite understand what you did here: `D[0] = input & (int)0x4b3e37;`? can you explain? –  May 11 '16 at 13:18
  • The mask is just Hex for the number where in binary there would be 1s at 0,1,2,4,5,9, ... I "and" the input with that mask, so example input : 10101, Mask: 11100 , then input & mask = 10100 – Fildor May 11 '16 at 13:57
  • Then you count the 1s : 10100 = 2 "1"s ; 2 in binary = 10 (even) masked with 0x1 = 0 parity. If it was e.g. 11100 => 3 "1"s = 11 (odd) & 0x1 = 1 parity. – Fildor May 11 '16 at 13:59