Bit-Level Operations

Visual Prolog provides six predicates for bit-level operations; bitor, bitand, bitnot, bitxor, bitleft, and bitright. These predicates have one flow variant each, operate on unsigned integers, and must be used in prefix notation.

(1) bitnot/2

bitnot performs a bit-wise logical NOT.

    bitnot(X, Z)                                          /* (i,o) */

With X bound to some integral value, Z will be bound to the bit-wise negation of X.

Operator

X

Z

bitnot

1

0

0

1

(2) bitand/3

bitand performs a bit-wise AND.

    bitand(X, Y, Z)                                     /* (i,i,o) */

With X and Y bound to some integral values, Z will be bound to the result of bit-wise ANDing the corresponding bits of X and Y.

Operator

X

Y

Z

bitand

1

1

0

0

1

0

1

0

1

0

0

0

(3) bitor/3

bitor performs a bit-wise OR.

    bitor(X, Y, Z)                                      /* (i,i,o) */

With X and Y bound to some integral values, Z will be bound to the result of bit-wise ORing the corresponding bits of X and Y.

Operator

X

Y

Z

bitor

1

1

0

0

1

0

1

0

1

1

1

0

(4) bitxor/3

bitxor performs a bit-wise XOR.

    bitxor(X, Y, Z)                                     /* (i,i,o) */

With X and Y bound to some integral values, Z will be bound to the result of bit-wise XORing the corresponding bits of X and Y.

Operator

X

Y

Z

bitxor

1

1

0

0

1

0

1

0

0

1

1

0

(5) bitleft/3

bitleft performs a bit-wise left shift.

    bitleft(X, N, Y)                                    /* (i,i,o) */

With X and N are bound to some integral values, Y is bound to the result of shifting the bit-wise representation of X N places to the left. The new bits will be zero-filled.

(6) bitright/3

bitright performs a bit-wise right shift.

    bitright(X, N, Y)                                   /* (i,i,o) */

With X and N are bound to some integral values, Y is bound to the result of shifting the bit-wise representation of X N places to the right. The new bits will be zero-filled.

(7) Exercise

Write a Visual Prolog program to test the theory that

    myxor(A, B, Result) :-
        bitnot(B, NotB), bitand(A, NotB, AandNotB),    
        bitnot(A, NotA), bitand(NotA, B, NotAandB),
        bitor(AandNotB, NotAandB, Result).
        behaves like
        bitxor(A, B, Result)