Tag: 位操作

我如何按位XOR两个C char数组?

我觉得愚蠢无法解决这个问题,但我迷失了。 我试图XOR两个C字符串。 #include #include #include int main() { char plainone[16]; char plaintwo[16]; char xor[17]; strcpy(plainone, “PlainOne”); strcpy(plaintwo, “PlainTwo”); int i=0; for(i=0; i<strlen(plainone);i++) xor[i] ^= (char)(plainone[i] ^ plaintwo[i]); printf("PlainText One: %s\nPlainText Two: %s\n\none^two: %s\n", plainone, plaintwo, xor); return 0; } 我的输出是: $ ./a.out PlainText One: PlainOne PlainText Two: PlainTwo one^two: 为什么xor数组不能读作什么?

在C位中,乘以3除以16

我的一个伙伴有这些谜题,这是一个让我望而却步的谜题。 这是问题,你得到一个数字,你想要返回该数字乘以3并除以16舍入为0.应该很容易。 赶上? 你只能使用! 〜&^ | + <>运算符和它们只有12的组合。 int mult(int x){ //some code here… return y; } 我的尝试是: int hold = x + x + x; int hold1 = 8; hold1 = hold1 & hold; hold1 = hold1 >> 3; hold = hold >> 4; hold = hold + hold1; return hold; 但这似乎并没有起作用。 我想我有丢失的问题,但我似乎无法想出一种拯救它们的方法。 另一种观点是好的。 […]

C快速计算4的下一个倍数?

将unsigned int四舍五入为4的倍数的快速方法是什么? 4的倍数有两个最低有效位0,对吗? 所以我可以将它们屏蔽掉,然后执行一个switch语句,在给定的uint添加1,2或3。 这不是一个非常优雅的解决方案.. 还有算术综述: myint == 0 ? 0 : ((myint+3)/4)*4 可能有更好的方法包括一些位操作?

位操作:清除位范围

我准备采访Gayle Laakman McDowell撰写的文章“Cracking the Coding Interview”。 在涉及位操作的部分,提供了两个函数,但我不太明白它是如何工作的。 // To clear all bits from the most significant bit through i (inclusive), we do: int clearMSBthroughI(int num, int i) { int mask = (1 << i) – 1; return num & mask; } // To clear all bits from i through 0 (inclusive), we do: int clearBitsIthrough0(int […]

Bit Twiddling Hacks:交错位明显的方式

我对这个问题很感兴趣 交错位明显的方式 (来自http://graphics.stanford.edu/~seander/bithacks.html ) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; // bits of x are in the even positions and y in the odd; unsigned int z = 0; // z gets the resulting Morton Number. for (int i = 0; i < […]

C中〜0的值是多少?

我想获得值INT_MIN和INT_MAX ,并且我已经尝试了INT_MAX和~0 >> 1因为最左边的位是符号位。 我把它们都给了-1 。 令人困惑的是为什么不是0xffffffff结果是0xffffffff而0x7fffffff ~0 >> 1是0x7fffffff ?

我怎样才能有效地改变比特?

我需要以偶数索引落在低位字节中的方式对16位无符号整数进行混洗,并且奇数索引落在高位字节中。 input: fedcba98 76543210 (contiguously numbered) output: fdb97531 eca86420 (even and odd separated) 我的代码目前看起来像这样: typedef unsigned short u16; u16 segregate(u16 x) { u16 g = (x & 0x0001); u16 h = (x & 0x0004) >> 1; u16 i = (x & 0x0010) >> 2; u16 j = (x & 0x0040) >> 3; u16 k = […]

这段代码如何反转位数?

unsigned reverse_bits(unsigned input) { //works on 32-bit machine input = (input & 0x55555555) <> 1; input = (input & 0x33333333) <> 2; input = (input & 0x0F0F0F0F) <> 4; input = (input & 0x00FF00FF) <> 8; input = (input & 0x0000FFFF) <> 16; return input; } 这是如何运作的?

为什么按位运算比旧微处理器上的加/减操作稍快?

我今天看到了这段摘录: 在大多数较旧的微处理器上,按位运算比加法和减法运算稍快,并且通常比乘法和除法运算快得多。 在现代体系结构中,情况并非如此:按位运算通常与添加速度相同(尽管仍然比乘法更快)。 我很好奇为什么按位操作比旧微处理器上的加/减操作稍快一些。 我能想到的只会导致延迟的是,实现加/减的电路取决于几级逻辑门(并行加法器和诸如此类),而按位运算则具有更简单的电路实现。 这是什么原因? 我知道算术和按位运算都在现代处理器的一个时钟内执行,但纯粹谈到电路的传播时间,理论上现在处理器中的延迟是不是仍然存在? 最后,我有一个关于按位移位操作执行的概念C问题: unsigned x = 1; x <<= 5; unsigned y = 0; y += 32; x和y都应该保持值32 ,但是它需要5个单独的左移来获得x到该值(如通过管道实现的按位移位)? 为了澄清,我纯粹是在询问电路行为而不是时钟周期数。

为什么XOR与整数交换会触发警告?

我输入了以下程序: #include int main(void) { int a = 3; int b = 42; printf(“a = %d\nb = %d\n”, a, b); printf(“Exchanging values.\n”); a ^= b ^= a ^= b; printf(“a = %d\nb = %d\n”, a, b); return 0; } 没关系。 当我尝试编译它时,我得到了这个: $ gcc test.c -o test -Wall -Wextra -ansi -pedantic-errors test.c: In function ‘main’: test.c:11: […]