how to perform xor operation correctly?
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来自类别的问题 :
- java Java和C#中的int和Integer有什么区别?
- java 如何在Java中确定路由器/网关的IP?
- java 根据XSD文件验证XML文件的最佳方法是什么?
- java 如何整理整数除法的结果?
- bit-manipulation 你如何设置,清除和切换一个位?
- bit-manipulation 枚举上最常见的C#按位操作
- bit-manipulation 如何计算32位整数中的设置位数?
- bit-manipulation 什么是按位移位(bit-shift)运算符,它们如何工作?
- xor XOR变量交换如何工作?
- xor 在Java中创建“逻辑独占或”运算符
- xor 为什么C / C ++中没有^^运算符?
- xor 为什么JavaScript中没有逻辑xor?
- parity 如何计算纵向冗余校验(LRC)?
- parity 如何计算以下位序列的奇偶校验位?
- parity 使用Pyserial时的字符转换错误
- parity 正则表达式平价
- bitwise-xor 给出两个数字的XOR和SUM。如何找到这些数字?
- bitwise-xor 按位添加相反的符号
- bitwise-xor TypeError:奇异长度字符串,同时解码十六进制字符串和iteratin,for循环仅限于列表中的一个点
- bitwise-xor how to perform xor operation correctly?