ishenglx 发表于 2016-8-18 09:31:17

求大神帮忙看一下arduino位运算问题

代码如下

void intToBytes(int value,byte (&src))
{
    src = (byte) ((value>>24) & 0xFF);
    src = (byte) ((value>>16)& 0xFF);
    src = (byte) ((value>>8)&0xFF);   
    src = (byte) (value & 0xFF);
}

int bytesToInt(byte src[], int offset) {
   int value;   
    value = (int) ( ((src & 0xFF)<<24)
            |((src & 0xFF)<<16)
            |((src & 0xFF)<<8)
            |(src & 0xFF));
    return value;
}

byte group = 1;
byte member = 10;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
byte b = {group,member,255,26};
int t = bytesToInt(b,0);
Serial.println(t);
byte src;
intToBytes(t,src);
Serial.println((int)src);
}

void loop() {
delay(1000);
}
这个代码写到nodemcu计算结果正确的
但是写道uno r3 或者nano这样的芯片上结果貌似溢出了,变量t变成了-230了,不知道哪里出了问题~~~~~

maidoo 发表于 2016-8-18 10:12:54

nodeMCU的int类型是32位,AVR的int是16位。知道问题乐吧?

pumpitup 发表于 2016-8-19 11:26:26

可能.
使用sizeof看看吧

xinhoujue 发表于 2016-8-21 09:47:37

数据类型的问题!

ishenglx 发表于 2016-8-22 08:55:25

已经解决了,谢谢大家~
页: [1]
查看完整版本: 求大神帮忙看一下arduino位运算问题