我只是发现了Nimrod并且有一个基本的问题(在文档中找不到答案)。
如何使用按位操作?我有以下代码,其中x被定义为int:
if x and 1:这并不能编译:
Error: type mismatch: got (range 0..1(int)) but expected 'bool'如果我试着:
if and(x, 1)我得到了
Error: type mismatch: got (tuple[int, int])
but expected one of:
system.and(x: int16, y: int16): int16
system.and(x: int64, y: int64): int64
system.and(x: int32, y: int32): int32
system.and(x: int, y: int): int
system.and(x: bool, y: bool): bool
system.and(x: int8, y: int8): int8诀窍是什么?
发布于 2013-11-01 18:47:50
and按位执行,问题是if需要的是bool,而不是整数。如果您想将类似C的比较添加到0,只需添加:
>>> if 1:
... echo("hello")
...
stdin(10, 4) Error: type mismatch: got (int literal(1)) but expected 'bool'
>>> if 1!=0:
... echo("hello")
...
hello发布于 2019-07-16 16:27:48
如果您想检查最后一位,可以从模块中使用testBit:
import bitops
if testBit(x, 0):
echo "Last bit is 1"https://stackoverflow.com/questions/19731492
复制相似问题