给定2个16位整数,我可以交织这些位形成一个单一的32位int? [英] Given 2 16-bit ints, can I interleave those bits to form a single 32 bit int?
本文介绍了给定2个16位整数,我可以交织这些位形成一个单一的32位int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是什么正确的方法?假设我有ABCD和abcd,输出位应该是像AaBbCcDd。
Whats the proper way about going about this? Lets say I have ABCD and abcd and the output bits should be something like AaBbCcDd.
unsigned int JoinBits(unsigned short a, unsigned short b) { }
推荐答案
#include <stdint.h>
uint32_t JoinBits(uint16_t a, uint16_t b) {
uint32_t result = 0;
for(int8_t ii = 15; ii >= 0; ii--){
result |= (a >> ii) & 1;
result <<= 1;
result |= (b >> ii) & 1;
if(ii != 0){
result <<= 1;
}
}
return result;
}
也在ideone上进行测试: http://ideone.com/lXTqB 。
also tested on ideone here: http://ideone.com/lXTqB.
这篇关于给定2个16位整数,我可以交织这些位形成一个单一的32位int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文