how to perform xor operation correctly?

java bit-manipulation xor parity bitwise-xor

415 观看

2回复

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.

的来源 发布者: 2019 年 11 月 4 日

回应 (2)


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 发布者: 11.05.2016 11:08

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.

作者: Gemini Keith 发布者: 11.05.2016 08:54
32x32