转移字节,其余铸造到int类型(有兴趣补) [英] Shifting bytes and remaining values casted to int (interested in two's complement)
问题描述
为什么code,你见下文给出这些结果?
Why does the code that you see below give these results?
void loop() {
byte test = 00000101;
Serial.print("The byte test is: ");
Serial.println(test);
int x = (int)test;
int y = (int)test<<8;
Serial.print("The int x is: ");
Serial.println(x);
Serial.print("The int shifted y is: ");
Serial.println(y);
byte tost = 10000001;
int z = test + tost;
Serial.print("The int z is: ");
Serial.println(z);
delay(1000);
}
结果
字节测试:65
的int x为:65
The int x is: 65
的int转移y为:16640
The int shifted y is: 16640
的int z是:194
The int z is: 194
虽然如果我改变了测试,以00000001它执行很大的:
While if I change the test to 00000001 it performs great:
改code:
void loop() {
byte test = 00000001;
Serial.print("The byte test is: ");
Serial.println(test);
int x = (int)test;
int y = (int)test<<8;
Serial.print("The int x is: ");
Serial.println(x);
Serial.print("The int shifted y is: ");
Serial.println(y);
byte tost = 10000001;
int z = test + tost;
Serial.print("The int z is: ");
Serial.println(z);
delay(1000);
}
结果:
The byte test is: 1
The int x is: 1
The int shifted y is: 256
The int z is: 130
我的问题是,我有我想从数据寄存器从读取加速度计两个字节。该值存储在补所以我想既然检查如果我破坏值,因为我必须做一些移动wire.read
返回一个字节,有些人说是有符号和一些未签名。
My problem is that I have two bytes that I want to read from data registers from an accelerometer. The value is stored in two's complement so I wanted to check since wire.read
returns a byte that some people say that is signed and some unsigned if I destroy the values, because I have to do some shifting.
所以,我想检查,如果我有一个字节并且我推诿,并将其存放到一个int,我得到什么样的价值观做的,我想测试,如果不知何故,我可以存储一个字节数组内签署字节值。
So I wanted to check if I have a byte and I try to shift it and store it to an int, what values do I get, and I want to test if somehow I can store signed byte values inside a byte array.
推荐答案
上面显示的答案是正确的,因为阿列克西伏龙芝说从0开始编号为八进制。
The answer shown above is correct as Alexy Frunze said number starting with 0 is octal .
000000101 is 65 not 5
65 << 8 =16640
10000001 in decimal is 129
129+65 = 194
这篇关于转移字节,其余铸造到int类型(有兴趣补)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!