首页 > 解决方案 > 将 if 条件按位转换为通常 if 条件

问题描述

我发现这个 if 条件使用按位,我不知道如何将它转换为使用普通逻辑运算符。

if (out[currentState] & (1 << j))

如何提供不按位使用的等效条件?

标签: c++if-statementbit-manipulation

解决方案


位移基本上是乘以 2 和除以 2。上述条件等价于

if ((out[currentState] >> j) & 1)

可以转换为

if ((int)(out[currentState] / pow(2, j)) % 2)

推荐阅读