我一直在研究在数组中查找孤独整数的算法,这里是实现:
int arr[] = {10, 20, 30, 5, 20, 10, 30};
int LonelyInteger = 0;
for(int i=0; i< 7; i++)
{
LonelyInteger = LonelyInteger ^ arr[i];
}
Run Code Online (Sandbox Code Playgroud)
结果是5
.
我的问题是 - 据说整数(由XOR
操作产生)由于这个操作太大了:
LonelyInteger ^ arr[i]
Run Code Online (Sandbox Code Playgroud)
这导致一个潜在的大整数,int
在这种情况下无法用数据类型表示.我的问题是:
XOR
生成无法存储在int
类型中的如此大的整数值?我知道XOR的工作,
Console.WriteLine(1^1); // returns 0
Run Code Online (Sandbox Code Playgroud)
结果
00000001
00000001
--------
00000000
Run Code Online (Sandbox Code Playgroud)
但是这又如何回归?
Console.WriteLine(-(-1^1)); // returns 2
Run Code Online (Sandbox Code Playgroud) 给定一个数组,其中每个数字的出现次数是奇数,除了一个出现次数是偶数的数字.找到偶数出现的数字.
例如
1, 1, 2, 3, 1, 2, 5, 3, 3
Run Code Online (Sandbox Code Playgroud)
输出应该是:
2
Run Code Online (Sandbox Code Playgroud)
以下是限制:
由于上述限制,我的所有想法都失败了:基于比较的排序,计数排序,BST,散列,暴力.
我很想知道:XORing会在这里工作吗?如果有,怎么样?
我不完全理解这种计算奇偶校验位的算法.有人可以详细解释一下吗?
以下代码取自"Hacker's Delight"一书:
int parity(unsigned x) {
unsigned y;
y = x ^ (x >> 1);
y = y ^ (y >> 2);
y = y ^ (y >> 4);
y = y ^ (y >> 8);
y = y ^ (y >>16);
return y & 1;
}
Run Code Online (Sandbox Code Playgroud) 这是谷歌最近的采访问题:
我们将f(X,Y)定义为X和Y的二进制表示中的不同对应位的数量.例如,f(2,7)= 2,因为2和7的二进制表示分别是010和111.第一和第三位不同,因此f(2,7)= 2.
你得到一个N正整数的数组,A1,A2,...,AN.求所有对(i,j)的f(Ai,Aj)之和,使得1≤i,j≤N
例如:
A = [1,3,5]
我们回来
f(1,1)+ f(1,3)+ f(1,5)+ f(3,1)+ f(3,3)+ f(3,5)+ f(5,1)+ f (5,3)+ f(5,5)=
0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8
我能想到这个解决方案是O(n ^ 2)
int numSetBits(unsigned int A) {
int count = 0;
while(A != 0) {
A = A & (A-1);
count++;
}
return count;
}
int count_diff_bits(int a, int b)
{
int x = a ^ b;
return numSetBits(x);
} …
Run Code Online (Sandbox Code Playgroud) 我是Xor加密的新手,我在使用以下代码时遇到了一些问题:
function xor_this($string) {
// Let's define our key here
$key = ('magic_key');
// Our plaintext/ciphertext
$text =$string;
// Our output text
$outText = '';
// Iterate through each character
for($i=0;$i<strlen($text);)
{
for($j=0;$j<strlen($key);$j++,$i++)
{
$outText .= $text{$i} ^ $key{$j};
//echo 'i='.$i.', '.'j='.$j.', '.$outText{$i}.'<br />'; //for debugging
}
}
return $outText;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它适用于普通字符串,如'dog',但它只适用于包含数字的字符串,如'12345'.
展示...
xor_this('dog')
='UYV'
xor_this('123')
=''
值得注意的是xor_this( xor_this('123') )
='123',正如我所期望的那样.我很确定问题存在于我对位运算符的不稳定理解中,或者可能是PHP处理包含数字的字符串的方式.我敢打赌那里有一个聪明的人知道这里究竟出了什么问题.谢谢.
编辑#1:它不是真正的'加密'.我猜混淆是正确的术语,这就是我正在做的事情.我需要从用户传递包含不重要数据的代码,而不能轻易篡改它.他们正在离线完成定时活动,并通过此代码将时间提交到在线记分板.离线活动将模糊他们的时间(以毫秒为单位).我需要编写一个脚本来接收此代码并将其转回包含其时间的字符串.
这是原始代码:
public static String reverseString(String s){
if(s == null) return "";
char[] rev = s.toCharArray();
int i = 0, j = s.length() - 1;
while(i < j) {
rev[i] ^= rev[j];
rev[j] ^= rev[i];
rev[i++] ^= rev[j--];
}
return String.valueOf(rev);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是Xor如何在这里交换字符值,为什么需要rev [i ++] ^ = rev [j--]?
有谁可以用一个很好的例子解释这个算子?
我知道这个运营商是什么.我的意思是一个真实的例子.
我想我了解 python 字节对象,但支持字节字符串的按位操作似乎是一个如此明显的功能。我不明白为什么它不受支持。
>>>'abcdefg'.encode('ascii')
b'abcdefg'
Run Code Online (Sandbox Code Playgroud)
好的。我从一个字符串变成了像我的字符串在 ascii 中的字节表示。
所以当我尝试:
>>> a = 'abcdefg'.encode('ascii')
>>> a ^ a
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: 'bytes' and 'bytes'
Run Code Online (Sandbox Code Playgroud)
为什么?为什么python不支持这个?关于字节对象,我有什么不明白的地方使这不可行或不明确吗?
我在产品代码中看到了以下代码段.它使用按位XOR进行字符串比较.这比String.equals(Object o)
方法好吗?作者试图在这里实现什么?
private static boolean compareSecure(String a, String b)
{
if ((a == null) || (b == null)) {
return (a == null) && (b == null);
}
int len = a.length();
if (len != b.length()) {
return false;
}
if (len == 0) {
return true;
}
int bits = 0;
for (int i = 0; i < len; i++) {
bits |= a.charAt(i) ^ b.charAt(i);
}
return bits == 0;
}
Run Code Online (Sandbox Code Playgroud)
对于上下文,等同的字符串是身份验证令牌.