当前位置:网站首页>【Arduino】关于“&”和“|” 运算-----多个参数运算结果异常的问题解决
【Arduino】关于“&”和“|” 运算-----多个参数运算结果异常的问题解决
2022-08-03 05:14:00 【与太阳有关_】
一、记录原因
我在测我的红外接收器返回结果(结果是用数组来存的,下标与红外接收器的编号是一一对应的)。
出现的问题:不管我的红外接收口有无接收到信号, 我发现它一直会执行 if 下的语句。
即证明 if 里的语句不是我所想要的结果。所以我就做了个小测试。
解决:在Arduino中,多参数运算& 或 | 时,需要把结果赋值给新的变量,否则容易出现异常。


二、测试代码
a.三个及以下参数(正常)
int n,m,a,b;//运算数
void setup()
{
Serial.begin(9600); // The serial port is initialized with baud rate of 9600 bytes/SEC
}
void loop()
{
n = 1; m = 1; a = 0;
Serial.print("n & m & a & b = ");
Serial.println(n & m & a );
if(n & m & a == 0)
Serial.println("you are in here");
else
Serial.println("you are fail in here");
}

b.四个及以上参数(异常)
int n,m,a,b;//运算数
void setup()
{
Serial.begin(9600); // The serial port is initialized with baud rate of 9600 bytes/SEC
}
void loop()
{
n = 1; m = 1; a = 0; b = 1;
Serial.print("n & m & a & b = ");
Serial.println(n & m & a & b);
if(n & m & a & b == 0)
Serial.println("you are in here");
else
Serial.println("you are fail in here");
}

c.多个参数出现异常的解决方法
int n,m,a,b;//运算数
int result; //结果
void setup()
{
Serial.begin(9600); // The serial port is initialized with baud rate of 9600 bytes/SEC
}
void loop()
{
n = 1; m = 1; a = 0; b = 1;
Serial.print("n & m & a & b = ");
Serial.println(n & m & a & b);
result = n & m & a & b;
if(result == 0)
Serial.println("you are in here");
else
Serial.println("you are fail in here");
}

笔记仅供自学,用来回看复习,不一定适合你,如有错误请指出。
边栏推荐
猜你喜欢
随机推荐
NotImplementedError: file structure not yet supported
VSO Downloader Ultimate 5.0.1.45 中文多语免费版 在线视频下载工具
Redis6学习笔记
Go (一) 基础部分3 -- 数组,切片(append,copy),map,指针
Newifi路由器第三方固件玩机教程,这个路由比你想的更强大以及智能_Newifi y1刷机_smzdm
1058 选择题 (20 分)(C语言)
嵌入式-I2C-物理电路图
MySQL 一些函数
2017-06-11 Padavan 完美适配newifi mini【adbyby+SS+KP ...】youku L1 /小米mini
ss-2.子项目互相访问(order80 -> payment8001)
D-PHY
Djiango第三次培训
Flask Web 报错:
request.getParameter的结果为on
阿凡提的难题
7.24[C语言零基础 知识点总结]
Js学习笔记(四)
-飞机大战-
令人愉快的 Nuxt3 教程 (二): 快速轻松地搭建博客
Pr第三次培训笔记









