Boolean lane quick reference.
Boolean operators (0 denotes False, 1 denotes True)
And / AndAlso / & / && |
Cond1 | Cond2 | Result |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
The output is only 1 when the left condition And right condition are 1. |
|
Or / OrElse / | / || |
Cond1 | Cond2 | Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
The output is 1 when either the left condition Or right condition are 1. |
|
Xor / ^ |
Cond1 | Cond2 | Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
The output is 1 when the conditions are not the same (either input1 or input2, but not both.) |
|
Not / ! (has only one condition operand) |
Cond1 | Result |
0 | 1 |
1 | 0 |
The output is Not the condition. |
|
Eqv / Not (cond1 Xor cond2) |
Cond1 | Cond2 | Result |
0 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 1 |
1 | 1 | 1 |
The output is 1 when the inputs are equal. |
|
Imp / Not cond1 And cond2 |
Cond1 | Cond2 | Result |
0 | 0 | 1 |
0 | 1 | 1 |
1 | 0 | 0 |
1 | 1 | 1 |
The output is 1 when the first condition is less than/equal to the second condition. |
|
Some common uses besides checking conditions:
Getting certain bits.
00110110 01101100
& 00001000 & 00001000
= 00000000 = 00001000
Is number odd?
Number And 1 (the bold zero will tell if it is (1) or not (0).
12 = 0000 1100, 13 = 0000 1101, 14 = 0000 1110, 15 = 0000 1111...
-1 = 1111 1111, -2 = 1111 1110, -3 = 1111 1101, 0 = 0000 0000
Setting certain bits.
00110110 01101100
Or 00001000 Or 00001000
= 00111110 = 01101100
Resetting certain bits.
00110110 01101100
& 11110111 & 11110111
= 00110000 = 01100100
Computing remainder: If you so happen to want the remainder of a division, where you are dividing by a power of 2.
Mod (or %) is the remainder operator.
N Mod #, # is 2 to the Xth power =
N Mod # = N And (# - 1)
26 Mod 4 = 26 And 3
(2) = 0001 1010
& 0000 0011
= 0000 0010 = 2
188 Mod 16 = 188 And 15
(12) = 1011 1100
& 0000 1111
= 0000 1100 = 12
Bit Shifting.
0000 1100 0010 0110
<< 2 << 3
0011 0000 0011 0000
0000 1100 0010 0110
>> 2 >> 3
0000 0011 0000 0100
Aside from that, if you want to multiply or divide by a power of 2:
Multiplication (as long as some of the bits don't fall off of the left).
N × # = N << X
17 × 4 = 68 = 17 << 2 = 0001 0001
<< 2
= 0100 0100 = 68
7 × 8 = 56 = 7 << 4 = 0000 0111
<< 4
= 0011 1000 = 56
Division (Integer Division, but still Division).
N ÷ # = N >> X
72 ÷ 4 = 18 = 72 >> 2 = 0100 1000
>> 2
= 0001 0010 = 18
200 ÷ 16 = 12 (actually 12.5, but I'm using Integer division)
= 200 >> 4 = 1100 1000
>> 4
= 0000 1100 = 12
(Bit Shifting is included within VB.NET 2003 and later, not in VB.NET 2002 or earlier versions)