Module

Bitwise


Description

Performs Bitwise operations on integers. The Bitwise class supports operations on 64-bit integers. However, you can still pass 32-bit integers and work with the result as a 32-bit integer.

Methods

Name

Parameters

Returns

Shared

BitAnd

value1 As UInt64, value2 As UInt64, [ParamArray MoreValues As UInt64]

UInt64

BitOr

value1 As UInt64, value2 As UInt64, [ParamArray MoreValues As UInt64]

UInt64

BitXor

value1 As UInt64, value2 As UInt64, [ParamArray MoreValues As UInt64]

UInt64

OnesComplement

value As UInt64

UInt64

ShiftLeft

value As UInt64, shift As Integer, [numBits As Integer]

UInt64

ShiftRight

value As UInt64, shift As Integer, [numBits As Integer]

UInt64

Method descriptions


Bitwise.BitAnd

BitAnd(value1 As UInt64, value2 As UInt64, [ParamArray MoreValues As UInt64]) As UInt64

Performs a Bitwise And on value1 and value2 (or as many values as you provide).

If any optional parameters value3 to valueN are passed, BitAnd returns the progressive results of each operation. Returns a UInt64.


Bitwise.BitOr

BitOr(value1 As UInt64, value2 As UInt64, [ParamArray MoreValues As UInt64]) As UInt64

Performs a Bitwise Or on value1 and value2.

If any optional parameters value3 to valueN are passed, BitOr returns the progressive results of each operation. Returns a UInt64.


Bitwise.BitXor

BitXor(value1 As UInt64, value2 As UInt64, [ParamArray MoreValues As UInt64]) As UInt64

Performs a Bitwise exclusive or on value1 and value2. See the Xor Operator for information on the Xor comparison.

If any optional parameters value3 to valueN are passed, BitXor returns the progressive results of each operation. Returns a UInt64.


Bitwise.OnesComplement

OnesComplement(value As UInt64) As UInt64

Computes the one's complement of value. Each bit of the number is inverted: Zeros replaced with ones and vice versa.

You can also compute the one's complement of value using the Xor operator.

Var X As UInt64 = 47 Xor &hffffffffffffffff

Bitwise.ShiftLeft

ShiftLeft(value As UInt64, shift As Integer, [numBits As Integer]) As UInt64

Shifts value to the left by shift.

ShiftLeft only shifts bits within NumBits field size. The optional parameter NumBits defaults to 64 bits.


Bitwise.ShiftRight

ShiftRight(value As UInt64, shift As Integer, [numBits As Integer]) As UInt64

Shifts value to the right by shift.

ShiftRight only shifts bits within NumBits field size. The optional parameter NumBits defaults to 64 bits.

Notes

You do not need to create an instance of the Bitwise class in order to access its methods. It is a special object, like System or DesktopApplication, that always exists.

The And, Not, Or, and Xor operators are overloaded. They can be passed either booleans or integers. If they are passed integers, they perform the corresponding Bitwise operators that are supported by the Bitwise class.


Bitand

The BitAnd method returns a UInt64 that is the result of comparing each bit of the two integers passed (or contiguous integers passed if passing three or more integers) and assigning 1 to the bit position in the integer returned if both bits in the same position in the integer passed are 1. Otherwise, 0 is assigned to the bit position.


Bitor

The BitOr method returns a UInt64 that is the result of comparing each bit of the two integers passed (or contiguous integers passed if passing three or more integers) and assigning 1 to the bit position in the integer returned if either of the bits in the same position in the integers passed are 1. Otherwise, 0 is assigned to the bit position.


Bitxor

The BitXor method returns a UInt64 that is the result of comparing each bit of the two integers passed (or contiguous integers passed if passing three or more integers) and assigning 1 to the bit position in the integer returned if both bits in the same position in the integers passed are not equal. Otherwise, 0 is assigned to the bit position.

The following table shows the results:

Integer1

Integer2

BitAnd

BitOr

BitXor

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

The And, Or, Xor, and Not operators are overloaded. They can perform both logical operations (when passed booleans) or Bitwise operations when passed integers. In the latter case, they perform the same functions as the BitAnd, BitOr, and BitXor methods of the Bitwise class.

Ones complement is sometimes used to represent positive and negative numbers. Positive numbers start with zeros and negative numbers start with ones. The only problem is that zero is represented two ways:

Decimal

Ones complement

Signed Decimal

0

000

0

1

001

1

2

010

2

3

011

3

4

100

-3

5

101

-2

6

110

-1

7

111

-0

Sample code

The following code performs BitAnd, BitOr, and BitXor operations on the passed integers.

Var i As Integer
i = Bitwise.BitAnd(5, 3) // returns 1
i = Bitwise.BitOr(5, 3) // returns 7
i = Bitwise.BitXor(5, 3) // returns 6

The following code illustrates how to re-express a bit expression that was written in C. The following expression in C:

would become:

Bitwise.BitOr(&hE0, Bitwise.BitAnd(Bitwise.ShiftRight(c, 12), &h0F))

Compatibility

All project types on all supported operating systems.

See also

And, Not, Or, Xor operators