3 ms·
Prolog is also like this: ?- x is -4 mod 2. X = 0. ?- X is -8 mod 2. X = 0. ?- X is -9 mod 2. X = 1. Using declarative integer arithm
by triska 8y ago
Prolog is also like this:
?- x is -4 mod 2.
X = 0.
?- X is -8 mod 2.
X = 0.
?- X is -9 mod 2.
X = 1.
Using declarative integer arithmetic, we can even express very generally that a number X is a multiple of 2:
?- X #= 2*_.
This is true iff there is any integer (indicated by an anonymous variable _) that, multiplied by 2, yields X. It works for positive and negative integers.
For example:
?- -4 #= 2*_.
true.
?- -9 #= 2*_.
false.
A very nice reference on the general topic of bitwise operations is Donald Knuth's Bitwise Tricks and Techniques in Volume 4 of The Art of Computer Programming.