将循环值移至前四位
Shift loop value to first four bit
我有一个关于非特定编程语言的问题。我可以使用移位运算符和按位运算符
MyValue 例如 0xF5 (1111.0101)。现在我想计算前四位,比如从 0 到 15(每个位组合)。
- 0001.0101
- 0010.0101
- 0011.0101
- 0100.0101
这是正确的吗?
for-Loop (counting up to 15, loop variable is LoopVariable)
{
// Set first four bits to 0 e.g 1111.0101 to 0000.0101
MyValue = MyValue | (0 << 4)
// Set first four bits according to current loop e.g. 0000.0101 to 0001.0101
MyValue = MyValue | (LoopVariable << 4)
}
第二个操作正确
MyValue = MyValue | (LoopVariable << 4)
它将高位设置为LoopVariable
的位。
第一个不是:
MyValue = MyValue | (0 << 4)
不清除任何位,是空操作。要清除位,请使用按位 and 而不是 or,并使用按位 not[=24= 应用反转掩码].
我有一个关于非特定编程语言的问题。我可以使用移位运算符和按位运算符
MyValue 例如 0xF5 (1111.0101)。现在我想计算前四位,比如从 0 到 15(每个位组合)。
- 0001.0101
- 0010.0101
- 0011.0101
- 0100.0101
这是正确的吗?
for-Loop (counting up to 15, loop variable is LoopVariable)
{
// Set first four bits to 0 e.g 1111.0101 to 0000.0101
MyValue = MyValue | (0 << 4)
// Set first four bits according to current loop e.g. 0000.0101 to 0001.0101
MyValue = MyValue | (LoopVariable << 4)
}
第二个操作正确
MyValue = MyValue | (LoopVariable << 4)
它将高位设置为LoopVariable
的位。
第一个不是:
MyValue = MyValue | (0 << 4)
不清除任何位,是空操作。要清除位,请使用按位 and 而不是 or,并使用按位 not[=24= 应用反转掩码].