Well, I had intended to delve into using & (bitwise comparison) for use in IF conditions to check to see if a bit is turned on, as well as $and(), $or() and $xor(), but since that information wasn't requested, I didn't go into them. However, for completeness' sake, I will do so now.

For an AND result to be true, both inputs (or bits) must be true (1). Otherwise, the result is false (0). This is useful in checking if a bit is turned on (all bits 0 except the bit you want to check - if you get a non-zero answer back, then the bit was on). It's also the way you turn off a bit (all bits 1 except the bit to turn off).

1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0

For an OR result to be true, at least one input must be true. If both are 0, then the result is false. This is useful in turning on a bit (all bits 0 except the bit you want to set - if you get the same answer back, then the bit was already on).

1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0

For an XOR (eXclusive OR) result to be true, one and only one input must be true. If neither or both are true, the result is false. This is useful in toggling bit states (all bits 0 except the bit you wish to toggle).

1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0

Just to refresh your memory of the bits and their decimal values we'll be working with throughout, here they are again:

1 = 0001
2 = 0010
4 = 0100
8 = 1000

and our base number is: 5 = 0101

[color:#00007F]if (N & M)


//if (5 & 1) echo -a * Bit is on | else echo -a * Bit is off
* Bit is on (because the 1st bit - which is worth 1 in decimal - is turned on)

//if (5 & 2) echo -a * Bit is on | else echo -a * Bit is off
* Bit is off (because the 2nd bit - which is worth 2 in decimal - is turned off)

//if (5 & 4) echo -a * Bit is on | else echo -a * Bit is off
* Bit is on (because the 3rd bit - which is worth 4 in decimal - is turned on)

//if (5 & 8) echo -a * Bit is on | else echo -a * Bit is off
* Bit is off (because the 4th bit - which is worth 8 in decimal - is turned off)

$and(N,M)

//echo -a * $and(5,14)
* 4

//echo -a * $and(5,13)
* 5

//echo -a * $and(5,11)
* 1

//echo -a * $and(5,7)
* 5

$or(N,M)

//echo -a * $or(5,1)
* 5

//echo -a * $or(5,2)
* 7

//echo -a * $or(5,4)
* 5

//echo -a * $or(5,8)
* 13

$xor(N,M)
Starting with 5 (0101) and toggling bits as we go:


//echo -a * $xor(5,1)
* 4 (0100 - toggled bit 1 off)

//echo -a * $xor(5,2)
* 7 (0111 - toggled bit 2 on)

//echo -a * $xor(5,4)
* 1 (0001 - toggled bit 3 off)

//echo -a * $xor(5,8)
* 13 (1101 - toggled bit 4 on)[/color]

:tongue:


DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C