use*_*971 2 c algorithm bit-manipulation bit-shift bit
我试图仅在设置所有低 7 位而不引入分支时才设置字节值中的最高位。
例如,给定以下输入:
input: 0b_0010_1100 -> return same value
input: 0b_0101_0101 -> return same value
input: 0b_0111_1111 -> all bits set except MSB, return 0xff
input: 0b_1010_1100 -> MSB is already set, return same value
Run Code Online (Sandbox Code Playgroud)
这仅需要适用于 8 位大小的值。
我尝试了几次尝试,popcount但这不适用于所有输入。
怎么样:
return x | ((x+1) & 0x80);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |