Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中,如何检查是否使用XOR设置了位?_C_Bit Manipulation - Fatal编程技术网

在C语言中,如何检查是否使用XOR设置了位?

在C语言中,如何检查是否使用XOR设置了位?,c,bit-manipulation,C,Bit Manipulation,我在面试中被问到这个问题。 假设我必须检查第3位: a=0x9004; 我说过 if((a<<13>>15)^1==1) printf("bit 3 is not set"); else printf("bit 3 is set"); 如果((a15)^1==1) printf(“未设置位3”); 其他的 printf(“设置了位3”); 但我觉得这不是他们想要的。if((unsigned int)a^(0x4)

我在面试中被问到这个问题。 假设我必须检查第3位:

 a=0x9004;
我说过

 if((a<<13>>15)^1==1)
     printf("bit 3 is not set");
 else
     printf("bit 3 is set");
如果((a15)^1==1)
printf(“未设置位3”);
其他的
printf(“设置了位3”);
但我觉得这不是他们想要的。

if((unsigned int)a^(0x4)<(unsigned int)a)
if ((unsigned int )a ^ (0x4) < (unsigned int )a) 
    printf("bit 3 is set");
else
    printf("bit 3 is not set");
printf(“设置了位3”); 其他的 printf(“未设置位3”);

如果设置了位3(
0x4
),则
a^0x4
将是较小的算术值,而
a

如果((a |)(1)仅使用异或请求。位计数从0开始到位31

if((a | (1<<2)) ^ a)
   printf("3rd bit is not set");
else printf("3rd bit is set");
#include <stdio.h>
//assuming you count from bit 0, bit 1,bit 2 up to bit 31..
int main(void){

int a = 0x7FFFFFFF;
int check = (1<<3);
check = (a - (check^a) )>0 ? 1:0;
printf("bit 3 of %x is set to %d",a,check);
return 0;
}
#包括
//假设从第0位、第1位、第2位到第31位进行计数。。
内部主(空){
int a=0x7FFFFFFF;

int check=(1为了清晰易读,请使用括号。这是一个相当愚蠢的问题。