标签: bitwise-xor

两个整数的XOR可以超出界限吗?

我一直在研究在数组中查找孤独整数的算法,这里是实现:

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在这种情况下无法用数据类型表示.我的问题是:

  1. 是否有可能XOR生成无法存储在int类型中的如此大的整数值?
  2. 如果不可能发生这种情况,那么有证据吗?

c c++ bit-manipulation integer-overflow bitwise-xor

52
推荐指数
8
解决办法
1万
查看次数

为什么在对1进行异或时,否定值会改变结果?

我知道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)

c# bit-manipulation xor bitwise-operators bitwise-xor

16
推荐指数
3
解决办法
1745
查看次数

找到偶数次出现的数字

给定一个数组,其中每个数字的出现次数是奇数,除了一个出现次数是偶数的数字.找到偶数出现的数字.

例如

1, 1, 2, 3, 1, 2, 5, 3, 3
Run Code Online (Sandbox Code Playgroud)

输出应该是:

2
Run Code Online (Sandbox Code Playgroud)

以下是限制:

  1. 数字不在范围内.
  2. 就地做.
  3. 所需的时间复杂度为O(N).
  4. 数组可能包含负数.
  5. 数组未排序.

由于上述限制,我的所有想法都失败了:基于比较的排序,计数排序,BST,散列,暴力.

我很想知道:XORing会在这里工作吗?如果有,怎么样?

algorithm time-complexity space-complexity bitwise-xor

14
推荐指数
1
解决办法
2569
查看次数

计算奇偶校验

我不完全理解这种计算奇偶校验位的算法.有人可以详细解释一下吗?

以下代码取自"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)

bit-manipulation parity bitwise-xor

10
推荐指数
2
解决办法
1万
查看次数

最近谷歌采访按位操作拼图

这是谷歌最近的采访问题:

我们将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)

algorithm bitwise-operators bitwise-xor

10
推荐指数
1
解决办法
1692
查看次数

PHP中的Xor加密

我是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:它不是真正的'加密'.我猜混淆是正确的术语,这就是我正在做的事情.我需要从用户传递包含不重要数据的代码,而不能轻易篡改它.他们正在离线完成定时活动,并通过此代码将时间提交到在线记分板.离线活动将模糊他们的时间(以毫秒为单位).我需要编写一个脚本来接收此代码并将其转回包含其时间的字符串.

php xor bitwise-operators bitwise-xor

8
推荐指数
1
解决办法
9169
查看次数

Xor如何交换价值?

这是原始代码:

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--]?

java algorithm bitwise-xor

8
推荐指数
1
解决办法
386
查看次数

什么是C#独占或`^`用法?

有谁可以用一个很好的例子解释这个算子?

我知道这个运营商是什么.我的意思是一个真实的例子.

c# operators bitwise-operators bitwise-xor

7
推荐指数
3
解决办法
2万
查看次数

为什么你不能在 python 中对字节对象进行异或?

我想我了解 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不支持这个?关于字节对象,我有什么不明白的地方使这不可行或不明确吗?

python byte bit-manipulation bytearray bitwise-xor

7
推荐指数
1
解决办法
4174
查看次数

使用按位xor进行Java字符串比较

我在产品代码中看到了以下代码段.它使用按位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)

对于上下文,等同的字符串是身份验证令牌.

java bitwise-operators bitwise-xor

7
推荐指数
1
解决办法
1322
查看次数