问题描述
由于异或放大器;两个数字的总和。如何找到号码?
例如x = A + B,Y = A ^ B;如果X,Y给出,如何让A,B?
如果不能,放弃的原因。
Given XOR & SUM of two numbers. How to find the numbers?For example, x = a+b, y = a^b; if x,y are given, how to get a, b?And if can't, give the reason.
推荐答案
无法完成。一个反例是0/100和4/96。这两项之和为100,异或为100。
Can't be done. One counter example is 0/100 and 4/96. Both these sum to 100 and xor to 100.
因此给予100之和的100的XOR结果,你可以不知道的其中的可能性的产生这两个数字。
Hence given a sum of 100 and an xor result of 100, you cannot know which of the possibilities generated those two numbers.
有关它的价值,这个程序会检查只数 0..255
的可能性:
For what it's worth, this program checks the possibilities with just the number 0..255
:
#include <stdio.h>
static void output (unsigned int a, unsigned int b) {
printf ("%u:%u=%u %u\n", a+b, a^b, a, b);
}
int main (void) {
unsigned int limit = 256;
unsigned int a, b;
output (0, 0);
for (b = 1; b != limit; b++)
output (0, b);
for (a = 1; a != limit; a++)
for (b = 1; b != limit; b++)
output (a, b);
return 0;
}
您可以然后采取输出和按摩它来给你所有的重复可能性:
You can then take that output and massage it to give you all the repeated possibilities:
testprog | sed 's/=.*$//' | sort | uniq -c | grep -v ' 1 '
这给:
7 100:100
2 100:16
4 100:18
2 100:2
4 100:20
4 100:24
8 100:26
2 100:4
2 100:64
4 100:66
4 100:68
4 100:80
8 100:82
8 100:84
8 100:88
16 100:90
等。
即使在减少集,还有一些产生相同的总和/异或相当多的组合,最糟糕的是大量产生的255/255总和/异或可能性,其中一些是:对>
Even in that reduced set, there are quite a few combinations which generate the same sum/xor, the worst being the large number of possibilities that generate a sum/xor of 255/255, some of which are:
255:255=0 255
255:255=1 254
255:255=2 253
255:255=3 252
255:255=4 251
255:255=5 250
255:255=6 249
255:255=7 248
255:255=8 247
255:255=9 246
255:255=10 245
255:255=11 244
255:255=12 243
255:255=13 242
255:255=14 241
255:255=15 240
255:255=16 239
255:255=17 238
255:255=18 237
这篇关于鉴于异或放大器;两个数字的总和。如何找到号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!