KBh*_*KBh 5 c c++ embedded bitmap
我正在尝试在 中创建位图数据,这是我使用的代码,但我无法找出正确的逻辑。这是我的代码
bool a=1;
bool b=0;
bool c=1;
bool d=0;
uint8_t output = a|b|c|d;
printf("outupt = %X", output);
Run Code Online (Sandbox Code Playgroud)
我希望我的输出是“1010”,相当于十六进制“0x0A”。我该怎么做 ??
按位 or 运算符or s 每个位置的位。结果a|b|c|dwill 是1因为您在最低有效位置按位或0 和 1 。
您可以将 ( <<) 位移动到正确的位置,如下所示:
uint8_t output = a << 3 | b << 2 | c << 1 | d;
Run Code Online (Sandbox Code Playgroud)
这将导致
00001000 (a << 3)
00000000 (b << 2)
00000010 (c << 1)
| 00000000 (d; d << 0)
--------
00001010 (output)
Run Code Online (Sandbox Code Playgroud)
严格来说,计算发生在ints 中,中间结果有更多的前导零,但在这种情况下,我们不需要关心这一点。
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |