简体   繁体   English

C-将字节的las三位与一个字节合并

[英]C - Combine las three bits of a byte with a byte

Lets say I have this byte in binary: f= 01010111, then I'll do this: f<<3 so Im expecting: 10111000, at this point Im going to have another byte, lets say the other one is 10111001 and I want to attach them so the result is 1011110111001. Basiclly, what I want is that the last three bits from the first byte to get swap for the second byte. 可以说我在二进制中有这个字节:f = 01010111,那么我将这样做:f << 3所以我期望:10111000,此时我将拥有另一个字节,可以说另一个是10111001,我想要附加它们,所以结果是1011110111001。基本上,我想要的是第一个字节的最后三位交换第二个字节。

I have no idea how can I do this, can you help me please? 我不知道该怎么办,请您能帮我吗?

Thanks! 谢谢!

How about this? 这个怎么样?

uint8_t f = 0x57; // 0b01010111
f <<= 3;          // now 10111000
uint8_t g = 0xB9; // 0b10111001
uint16_t out = (f << 5) | g;
printf("0x%04X\n", out);

> 0x17B9          // 0b1011110111001

I don't understand the intermediate << 3 but I'm assuming it's part of your application. 我不理解中间的<< 3但是我假设它是您应用程序的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM