gpt4 book ai didi

编码 : Can anyone explain me this why there are different outputs when comparing Least significant bits of two numbers?

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:16 25 4
gpt4 key购买 nike

谁能解释一下为什么在比较两个数字的最低有效位时会出现不同的输出?
在第一种情况下,我直接比较它们
在第二种情况下,我将 LSB 分配给其他变量

        //Program to check Least significant bits of two numbers 
#include<stdio.h>
int main(){

//LSB means least significant bit of the binary number
//(i.e.,Unit place digit in binary number)
//Example..... 2 = 10(in binary) and 9 = 1001(in binary)
//so least significant bit is 0 for 2 and 1 for 9

//In binary M =101 and LSB of M = 1
int M = 5;
//In binary P = 011 and LSB of P = 1
int P = 3;

//printing LSB values
printf("\nLeast significant bits are for M : %d and for P : %d",M&1,P&1);

//Comparing LSB of M and LSB of P
if(M&1 != P&1) {
printf("\nLeast significant bits are not equal");
}
else printf("\nLeast significant bits are equal");

//Assigning Least significant bit of M to MLSB
int MLSB = M&1;
//Assigning Least significant bit of P to PLSB
int PLSB = P&1;

//printing LSB values
printf("\nValue in MLSB : %d and Value in PLSB : %d",MLSB,PLSB);

//Comparing MLSB and PLSB
if(MLSB != PLSB) {
printf("\nLeast significant bits are not equal");
}
else printf("\nLeast significant bits are equal");


}

输出:

最低有效位用于 M : 1 和 P : 1
最低有效位不相等
MLSB 中的值:1 和 PLSB 中的值:1
最低有效位相等

最佳答案

尝试改变这个

if(M&1 != P&1) {

进入

if((M&1) != (P&1)) {

然后检查运算符的优先级,例如这里https://en.cppreference.com/w/c/language/operator_precedence

关于编码 : Can anyone explain me this why there are different outputs when comparing Least significant bits of two numbers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51779845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com