gpt4 book ai didi

java - 确保对严格正多头的异或运算仅产生严格正多头

转载 作者:行者123 更新时间:2023-12-01 22:05:08 24 4
gpt4 key购买 nike

我想确保对严格正多头进行的异或运算只会产生严格正多头。

我的问题基于以下java代码:

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;

public class Test {

static Random r = new Random();

static class Data {
long id = Math.abs(r.nextLong());

@Override
public String toString() {
return "Data {" + "id=" + id + '}';
}
}

public static void main(String[] args) {
List<Data> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
data.add(new Data());
}

final String password = "don't you ever tell them";

byte[] passwordBytes = password.getBytes();
long[] passwordLongs = new long[passwordBytes.length / 8];

for (int i = 0; i < passwordLongs.length; i++) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
byte[] chunk = new byte[Long.BYTES];
System.arraycopy(passwordBytes, i * Long.BYTES, chunk, 0, Long.BYTES);
buffer.put(chunk);
buffer.flip();//need flip
passwordLongs[i] = buffer.getLong();
}

System.out.println(data);

ListIterator<Data> encryptIterator = data.listIterator();
while (encryptIterator.hasNext()) {
Data next = encryptIterator.next();
next.id = next.id ^ passwordLongs[(encryptIterator.nextIndex() - 1) % passwordLongs.length];//XOR here
}

System.out.println(data);
}
}

有人可以通过一些理论提供答案吗?

最佳答案

  • 不变式 1:正整数的最高有效位为零。

  • 不变式 2:0 异或 0 = 0。

结论:正整数异或正整数=正整数。

关于java - 确保对严格正多头的异或运算仅产生严格正多头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32931867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com