DOCS

# Bitwise Operators

Bitwise operators all work on the individual bits within an integer type (i.e. BYTE, SHORT, INTEGER or LONG). The binary bitwise operators work expect two values that must both be the same type. If they are not already the same type then you must use a type conversion function on one of the values to ensure that both types match. The result of all bitwise operations will produce a new value that is the same type as the input value(s).

# Bitwise AND Operator ( & )

The bitwise AND operator ( & ) performs a bitwise and operation between the corresponding bits in each of the input values. The value of the matching bits in the result can be determined by examining the true table that is shown below. The bitwise AND operator is commonly used to set certain bits in an integer value to zero.

## Truth Table

## Example: 0xf0f0 & 0xff00 = 0xf000

```
SUB MAIN
DIM X AS INTEGER
X = 0xf0f0 & 0xff00
END SUB
```

# Bitwise OR Operator ( | )

The bitwise OR operator ( | ) performs a bitwise or operation between the corresponding bits in each of the input values. The value of the matching bits in the result can be determined by examining the true table that is shown below. The bitwise OR operator is commonly used to set certain bits in an integer value to one.

## Truth Table

## Example: 0xf0f0 | 0xff00 = 0xfff0

```
SUB MAIN
DIM X AS INTEGER
X = 0xf0f0 | 0xff00
END SUB
```

# Bitwise Exclusive OR Operator ( ^ )

The bitwise Exclusive OR operator ( ^ ) performs a bitwise exclusive or operation between the corresponding bits in each of the input values. The value of the matching bits in the result can be determined by examining the true table that is shown below. The bitwise Exclusive OR operator sets each output bit to one when one (and only one) of the corresponding input bits is 1. If both bits are zero or both bits are one then the output bit will be zero.

## Truth Table<

## Example: 0xf0f0 ^ 0xff00 = 0x0ff0

```
SUB MAIN
DIM X AS INTEGER
X = 0xfff0 ^ 0x00ff
END SUB
```

# Bit Complement Operator ( ~ )

The bit complement operator ( ~ ) inverts the value of each bit in the input value to its immediate right as shown in the truth table below.

##Truth Table

## Example: ~0xf0f0 = 0x0f0f

```
SUB MAIN
DIM X AS INTEGER
X = ~0xf0f0
END SUB
```

# Bitwise Shift Left Operator ( « )

The bitwise shift left operator ( << ) is used to shift the bits in the first value to the left by the number of bits specified by the second value.

## Example: 0x00f0 « 1 = 0x01e0

```
SUB MAIN
DIM X AS INTEGER
X = ~0xf0f0
END SUB
```

# Bitwise Shift Right (Signed) Operator ( » )

The bitwise shift right (signed) operator ( » ) is used to shift the bits in the first value to the right by the number of bits specified by the second value. If the sign bit (MSB) was originally set then it will remain set in the output value.

## Example: 0xf0000000 » 1 = 0xf8000000

```
SUB MAIN
DIM X AS INTEGER
X = 0xf0000000 >> 1
END SUB
```

# Bitwise Shift Right (Unsigned) Operator ( »> )

The bitwise shift left (unsigned) operator ( »> ) is used to shift the bits in the first value to the right by the number of bits specified by the second value. The sign bit (MSB) will always have a zero shifted into it.

## Example: 0xf0000000 »> 1 = 0x78000000

```
SUB MAIN
DIM X AS INTEGER
X = 0xf0000000 >>> 1
END SUB
```