6

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

トリックは何ですか?

4

2 に答える 2

7

andビットごとに行います。問題はむしろ、整数ではなく をif期待することです。boolC のような 0 との比較が必要な場合は、単純に次のように追加します。

>>> if 1:
...   echo("hello")
...
stdin(10, 4) Error: type mismatch: got (int literal(1)) but expected 'bool'
>>> if 1!=0:
...   echo("hello")
...
hello
于 2013-11-01T18:47:50.057 に答える