In Nimrod, what is the syntax for bitwise operations?

I just open Nimrod and ask the main question (I can not find the answer in the documentation).

How do you use bitwise operations? I have the following code where x is defined as int:

if x and 1: 

This will 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 trick?

+6
source share
2 answers

and bitwise; the problem is that if expects a bool , not an integer. If you want to compare C with 0, just 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 
+7
source

If you want to check the last bit, you can use testBit from the bitops module:

 import bitops if testBit(x, 0): echo "Last bit is 1" 
0
source

Source: https://habr.com/ru/post/957201/


All Articles