新手问题;我有两个二进制数;例如,0b11和0b00。如何组合这两个数字才能得到0b1100 (即将它们放在一起形成一个新的数字)
发布于 2012-10-09 18:01:17
使用按位移位和或运算符:
unsigned int a = 0x03; /* Binary 11 (actually binary 00000000000000000000000000000011) */
unsigned int b = 0x00; /* Binary 00 */
/* Shift `a` two steps so the number becomes `1100` */
/* Or with the second number to get the two lower bits */
unsigned int c = (a << 2) | b;
/* End result: `c` is now `1100` binary, or `0x0c` hex, or `12` decimal */发布于 2012-10-09 18:03:07
左移<<和按位OR |
int a = 0; /* 0b00 */
int b = 3; /* 0b11 */
int c = (b << 2) | a; /* 0b1100 */备注:
但是不需要
a是0。2表示“向左移位两位”。右移的是>>运算符。https://stackoverflow.com/questions/12797479
复制相似问题