Module

# Bitwise

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Performs <span class="title-ref">Bitwise</span> operations on integers. The <span class="title-ref">Bitwise</span> class supports operations on 64-bit `integers</api/data_types/additional_types/uint64>`. However, you can still pass 32-bit `integers</api/data_types/integer>` and work with the result as a 32-bit `integer</api/data_types/integer>`.

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                     | Parameters                                                                                                                                                                                                                       | Returns                                           | Shared |
|------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|--------|
| `BitAnd<bitwise.bitand>`                 | value1 As `UInt64</api/data_types/additional_types/uint64>`, value2 As `UInt64</api/data_types/additional_types/uint64>`, `ParamArray</api/language/paramarray>` MoreValues As `UInt64</api/data_types/additional_types/uint64>` | `UInt64</api/data_types/additional_types/uint64>` |        |
| `BitOr<bitwise.bitor>`                   | value1 As `UInt64</api/data_types/additional_types/uint64>`, value2 As `UInt64</api/data_types/additional_types/uint64>`, `ParamArray</api/language/paramarray>` MoreValues As `UInt64</api/data_types/additional_types/uint64>` | `UInt64</api/data_types/additional_types/uint64>` |        |
| `BitXor<bitwise.bitxor>`                 | value1 As `UInt64</api/data_types/additional_types/uint64>`, value2 As `UInt64</api/data_types/additional_types/uint64>`, `ParamArray</api/language/paramarray>` MoreValues As `UInt64</api/data_types/additional_types/uint64>` | `UInt64</api/data_types/additional_types/uint64>` |        |
| `OnesComplement<bitwise.onescomplement>` | value As `UInt64</api/data_types/additional_types/uint64>`                                                                                                                                                                       | `UInt64</api/data_types/additional_types/uint64>` |        |
| `ShiftLeft<bitwise.shiftleft>`           | value As `UInt64</api/data_types/additional_types/uint64>`, shift As `Integer</api/data_types/integer>`, numBits As `Integer</api/data_types/integer>` = 64                                                                      | `UInt64</api/data_types/additional_types/uint64>` |        |
| `ShiftRight<bitwise.shiftright>`         | value As `UInt64</api/data_types/additional_types/uint64>`, shift As `Integer</api/data_types/integer>`, numBits As `Integer</api/data_types/integer>` = 64                                                                      | `UInt64</api/data_types/additional_types/uint64>` |        |

## Method descriptions

<div id="bitwise.bitand">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Bitwise.BitAnd

**BitAnd**(value1 As `UInt64</api/data_types/additional_types/uint64>`, value2 As `UInt64</api/data_types/additional_types/uint64>`, `ParamArray</api/language/paramarray>` MoreValues As `UInt64</api/data_types/additional_types/uint64>`) As `UInt64</api/data_types/additional_types/uint64>`

> Performs a <span class="title-ref">Bitwise</span> 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</api/data_types/additional_types/uint64>`.

<div id="bitwise.bitor">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Bitwise.BitOr

**BitOr**(value1 As `UInt64</api/data_types/additional_types/uint64>`, value2 As `UInt64</api/data_types/additional_types/uint64>`, `ParamArray</api/language/paramarray>` MoreValues As `UInt64</api/data_types/additional_types/uint64>`) As `UInt64</api/data_types/additional_types/uint64>`

> Performs a <span class="title-ref">Bitwise</span> 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</api/data_types/additional_types/uint64>`.

<div id="bitwise.bitxor">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Bitwise.BitXor

**BitXor**(value1 As `UInt64</api/data_types/additional_types/uint64>`, value2 As `UInt64</api/data_types/additional_types/uint64>`, `ParamArray</api/language/paramarray>` MoreValues As `UInt64</api/data_types/additional_types/uint64>`) As `UInt64</api/data_types/additional_types/uint64>`

> Performs a <span class="title-ref">Bitwise</span> exclusive or on *value1* and *value2*. See the `Xor</api/language/operators/bitwise_logical/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</api/data_types/additional_types/uint64>`.

<div id="bitwise.onescomplement">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Bitwise.OnesComplement

**OnesComplement**(value As `UInt64</api/data_types/additional_types/uint64>`) As `UInt64</api/data_types/additional_types/uint64>`

> Computes the [one's complement](http://en.wikipedia.org/wiki/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</api/language/operators/bitwise_logical/xor>` operator.
>
> ``` xojo
> Var X As UInt64 = 47 Xor &hffffffffffffffff
> ```

<div id="bitwise.shiftleft">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Bitwise.ShiftLeft

**ShiftLeft**(value As `UInt64</api/data_types/additional_types/uint64>`, shift As `Integer</api/data_types/integer>`, numBits As `Integer</api/data_types/integer>` = 64) As `UInt64</api/data_types/additional_types/uint64>`

> Shifts *value* to the left by *shift*.
>
> ShiftLeft only shifts bits within *NumBits* field size. The optional parameter *NumBits* defaults to 64 bits.

<div id="bitwise.shiftright">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Bitwise.ShiftRight

**ShiftRight**(value As `UInt64</api/data_types/additional_types/uint64>`, shift As `Integer</api/data_types/integer>`, numBits As `Integer</api/data_types/integer>` = 64) As `UInt64</api/data_types/additional_types/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 <span class="title-ref">Bitwise</span> class in order to access its methods. It is a special object, like `System</api/os/system>` or `DesktopApplication</api/user_interface/desktop/desktopapplication>`, that always exists.

The `And</api/language/operators/bitwise_logical/and>`, `Not</api/language/operators/bitwise_logical/not>`, `Or</api/language/operators/bitwise_logical/or>`, and `Xor</api/language/operators/bitwise_logical/xor>` operators are overloaded. They can be passed either `booleans</api/data_types/boolean>` or `integers</api/data_types/integer>`. If they are passed `integers</api/data_types/integer>`, they perform the corresponding <span class="title-ref">Bitwise</span> operators that are supported by the <span class="title-ref">Bitwise</span> class.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### BitAnd

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### BitOr

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### BitXor

The BitXor method returns a `UInt64</api/data_types/additional_types/uint64>` that is the result of comparing each bit of the two `integers</api/data_types/integer>` passed (or contiguous `integers</api/data_types/integer>` passed if passing three or more `integers</api/data_types/integer>`) and assigning 1 to the bit position in the `integer</api/data_types/integer>` returned if both bits in the same position in the `integers</api/data_types/integer>` 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</api/language/operators/bitwise_logical/and>`, `Or</api/language/operators/bitwise_logical/or>`, `Xor</api/language/operators/bitwise_logical/xor>`, and `Not</api/language/operators/bitwise_logical/not>` operators are overloaded. They can perform both logical operations (when passed booleans) or <span class="title-ref">Bitwise</span> operations when passed integers. In the latter case, they perform the same functions as the BitAnd, BitOr, and BitXor methods of the <span class="title-ref">Bitwise</span> 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.

``` xojo
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:

``` 
(0xE0 | ((c >> 12) & 0x0F))
```

would become:

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`And</api/language/operators/bitwise_logical/and>`, `Not</api/language/operators/bitwise_logical/not>`, `Or</api/language/operators/bitwise_logical/or>`, `Xor</api/language/operators/bitwise_logical/xor>` operators

</div>
