Bitwise/Boolean Operators

<< Click to Display Table of Contents >>

Home  mIoTa BASIC > mIoTa BASIC Language Reference > Lexical Elements > Tokens > Operators >

Bitwise/Boolean Operators

Bitwise / Boolean operators are use to modify individual bits of numerical or boolean operands.

 

Since boolean's and bit's are one of the same thing in mIoTa BASIC, so the following descriptions apply to numerical operands as well as conditional statements.

 

 

The operators AND, OR, and XOR operations are ..

 

Operator

Operation

AND

compares pairs of bits and returns 1 if both bits are 1, otherwise it returns 0.

OR

compares pairs of bits and generates a 1 result if either or both bits are 1, otherwise it returns 0.

XOR

compares pairs of bits and generates a 1 result if the bits are complementary, otherwise it returns 0.

 

giving the following table of results for two single bits 'a' and 'b' ..

 

a

b

a AND B

a OR b

a XOR b

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

 

 

 

The NOT operator only has one operand

 

Operator

Operation

NOT

inverts each bit, 0 becomes 1 and 1 becomes 0.

 

giving the following table of results for a single bit 'a' ..

 

a

NOT a

0

1

1

0

 

 

 

Binary operators SHL and SHR move the bits of the left operand by a number of positions specified by the right operand, to the left or right, respectively. The right operand has to be positive and less than 255.

 

With SHL, left most bits are discarded, and “new” bits on the right are assigned zeroes. Thus, shifting unsigned operand to the left by n positions is equivalent to multiplying it by 2n if all discarded bits are zero. This is also true for signed operands (Integer or Longint) if all discarded bits are equal to the sign bit.

 

With SHR, right most bits are discarded, and the “freed” bits on the left are assigned zeroes  Shifting operand to the right by n positions is equivalent to dividing it by 2n if the operand is unsigned (not Integer or Longinteger).

 

 

Operator

Operation

SHL

moves all bits to the left, discards the far left bit and assigns 0 to the right most bit.

SHR

moves all bits to the right, discards the far right bit and assigns 0 to the left most bit.

 

For example ..

 

u1 = %10010 shl 2; // u1 becomes equal to %1001000

u1 = %100001 shr 4; // u1 beomes equal to %10