C 级联位

C 级联位,c,bits,C,Bits,我必须连接int的位。例如: unsigned char byte1 = 0x0F; // 00001111 unsigned char byte2 = 0xCC; // 11001100 unsigned char byte3 = 0x55; // 01010101 unsigned char byte4 = 0x04; // 00000100 连接后,结果应为: 00000100010101011100110000001111 我试着做smth,比如: unsigned

我必须连接int的位。例如:

unsigned char byte1 = 0x0F;   // 00001111
unsigned char byte2 = 0xCC;   // 11001100
unsigned char byte3 = 0x55;   // 01010101
unsigned char byte4 = 0x04;   // 00000100
连接后,结果应为:

00000100010101011100110000001111 
我试着做smth,比如:

unsigned int temp = 0;
temp = temp | byte1; //the result should be 00001111 for now

temp = temp >> 8;
byte2 = byte2 << 8;
temp = temp | byte2; //the result should be 1100110000001111 for now

temp = temp >> 8;
byte3 = byte3 << 8;
temp = temp | byte3; //the result should be 010101011100110000001111 for now

temp = temp >> 8;
byte4 = byte4 << 8;
temp = temp | byte4; //the result should be 00000100010101011100110000001111

事实上,在我看来

temp = temp >> 8;

位会转移整个临时值。

似乎您误解了数据的布局。
temp=temp>>8
始终将您刚才添加的内容移出:

unsigned int byte1 = 0x0F; // 00001111; //byte1
unsigned int byte2 = 0xCC; // 11001100; //byte2
unsigned int byte3 = 0x55; // 01010101; //byte3
unsigned int byte4 = 0x04; // 00000100; //byte4

unsigned int temp = (byte1) | (byte2 << 8) | (byte3 << 16) | (byte4 << 24);
printf("%u", temp);
uint32_t temp = 0; // 00000000 00000000 00000000 00000000
temp |= byte1;     // 00000000 00000000 00000000 00001111
temp = temp >> 8;  // 00000000 00000000 00000000 00000000
// and so on...
当字节与较大的数字组合时,在左侧(而不是右侧)进行零扩展,因此将它们向右移动将再次删除它们

有两种明显的写作方式:

第一种方法是在将字节转换为结果之前将字节左移到右移:

uint32_t temp = 0;     // 00000000 00000000 00000000 00000000
temp |= byte1;         // 00000000 00000000 00000000 00001111
temp |= (byte2 <<  8); // 00000000 00000000 11001100 00001111
temp |= (byte3 << 16); // 00000000 01010101 11001100 00001111
temp |= (byte4 << 24); // 00000100 01010101 11001100 00001111
两者都可以简洁地书写,如:

uint32_t temp = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1;

uint32\u t temp=(byte4@RNar,你是什么意思?对不起,我误读了你的代码你应该使用
unsigned int
(或者
uint32\u t
)并确保您的字节也是
无符号字符
,而不是普通字符或有符号字符。否则您将遇到问题,因为在C中,
更新问题以显示如何声明和初始化
字节1
等。例如,如果它是
无符号字符字节2;
则行
字节2=字节2是的,我查把它改成unsigned int temp和byte1,byte2..都是unsigned char类型。但是它仍然给我0@M.M很可能是的,如果它接近Java的位运算符OP应该
当移到左边时,例如byte2@Maximillan本质上
x你能解释一下(byte2我只做
byte2吗
uint32_t temp = 0;     // 00000000 00000000 00000000 00000000
temp |= byte1;         // 00000000 00000000 00000000 00001111
temp |= (byte2 <<  8); // 00000000 00000000 11001100 00001111
temp |= (byte3 << 16); // 00000000 01010101 11001100 00001111
temp |= (byte4 << 24); // 00000100 01010101 11001100 00001111
uint32_t temp = 0; // 00000000 00000000 00000000 00000000
temp |= byte4;     // 00000000 00000000 00000000 00000100
temp = temp << 8;  // 00000000 00000000 00000100 00000000
temp |= byte3;     // 00000000 00000000 00000100 01010101
temp = temp << 8;  // 00000000 00000100 01010101 00000000
temp |= byte2;     // 00000000 00000100 01010101 11001100
temp = temp << 8;  // 00000100 01010101 11001100 00000000
temp |= byte1;     // 00000100 01010101 11001100 00001111
uint32_t temp = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1;
uint32_t temp = ((byte4 << 8 | byte3) << 8 | byte2) << 8 | byte1;