Bitwise Package

Overview

This package contains a number of functions which perform bitwise logical operations on numbers.

To perform these operations, each number is converted to binary and the corresponding bits are compared according to the following table:

A B A AND B A OR B A XOR B NOT A
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 0 1 1 0 0

The resultant bits form the answer returned by the function.

All the functions defined here have the necessary “purity level” to be used in SQL SELECT and WHERE clauses as well as PL/SQL.

Function Definitions

AND_fn (a, b)
Performs a bitwise AND between a and b.
OR_fn (a, b)
Performs a bitwise OR between a and b.
XOR_fn (a, b)
Performs a bitwise exclusive OR between a and b.
NOT_fn (a)
Performs a bitwise NOT on a.

Examples

bitwise_pkg.AND_fn (11,13)
Returns a value of 9 (as 1011 AND 1101 = 1001).
bitwise_pkg.OR_fn (11,13)
Returns a value of 15 (as 1011 OR 1101 = 1111).
bitwise_pkg.XOR_fn (11,13)
Returns a value of 6 (as 1011 XOR 1101 = 0110).
bitwise_pkg.NOT_fn (11)
Returns a value of 4 (as NOT 1011 = 0100).

Download

If the above has whetted your interest, you can download the package building script, bitwise.zip (1,982 bytes). I hope you find it useful.

To convert to and from binary numbers (and other bases for that matter) you can use my Converter package.

This code is made available for use in both private and commercial applications under a Creative Commons Licence.

Whilst every effort has been made to ensure these routines operate accurately and properly, no liability will be accepted for any problems they may cause on your system.

Leave a Comment