【发布时间】:2012-02-23 07:40:26
【问题描述】:
我需要一个函数,它将四个 unsigned char 变量作为参数并将它们组合成一个 unsigned int。第一个 char 变量是 int 的第一个字节,第二个 char 是第二个字节,依此类推。这是我到目前为止所拥有的,它无法正常工作,在弄乱它并在谷歌上搜索了几个小时后,我无法弄清楚为什么。
uint32_t combineChar(unsigned char one, unsigned char two, unsigned char three, unsigned char four){
uint32_t com;
com = (uint32_t)one;
com = com << 8 | (uint32_t)two;
com = com << 8 | (uint32_t)three;
com = com << 8 | (uint32_t)four;
return com;
}
【问题讨论】:
-
您的代码结果与您的预期有何不同?
标签: c bit-manipulation