I'm just discovering Nimrod and have a basic question (couldn't find the answer in the documentation).
How do you use bitwise operations ? I have the following code, where x is defined as an int :
if x and 1:
This does not compile :
Error: type mismatch: got (range 0..1(int)) but expected 'bool'
And if I try:
if and(x, 1)
I get
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
What's the trick ?
The | (bitwise inclusive OR) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1 . If both of the bits are 0 , the result of that bit is 0 ; otherwise, the result is 1 .
Logical operators: Compare bits of the given object and always return a Boolean result. Bitwise operators: Perform operations on individual bits, and the result is also always a bit. Assignment operators: allow us to initialize an object with a value or perform specific operations on it.
and
does bitwise and; the issue is rather that if
expects a bool
, not an integer. If you want C-like comparison to 0, simply add it:
>>> if 1:
... echo("hello")
...
stdin(10, 4) Error: type mismatch: got (int literal(1)) but expected 'bool'
>>> if 1!=0:
... echo("hello")
...
hello
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With