在Nimrod中,按位操作的语法是什么?
问题描述:
我只是发现Nimrod并有一个基本问题(在文档中找不到答案)。在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
诀窍是什么?
答
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
不,我真的需要执行一个按位和我的变量;更确切地说,在这里,我想检查最后一位是否设置。 – Fabien
那么使用'(x和1)!= 0'? –
是的,那工作,谢谢。 – Fabien