2013-11-01 47 views
6

我只是發現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 

訣竅是什麼?

回答

7

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 
+0

不,我真的需要執行一個按位和我的變量;更確切地說,在這裏,我想檢查最後一位是否設置。 – Fabien

+1

那麼使用'(x和1)!= 0'? –

+0

是的,那工作,謝謝。 – Fabien

相關問題