在C中将两个数字转换为一个

Dan*_*iel 1 c numbers

新手问题; 我有两个二进制数; 比如说; 0b11和0b00.如何组合这两个数字以便得到0b1100(即将它们放在彼此旁边以形成新数字)

Som*_*ude 5

使用按位移位和或运算符:

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 */
Run Code Online (Sandbox Code Playgroud)