Alright, you asked for it...
Before starting, a quick recap on the use of "and" with true/false values. You've likely already used this when creating if/then statements. The basic "if" statement goes something like this:
if condition then action
To add "and" into this mix, you'd use something like:
if condition1 and condition2 then action
In this latter case, both conditions must evaluate as "true" for the entirety of the statement to evaluate as "true". If either is false, then the action won't be performed.
Next up, a recap on how CC deals with colours. Go read the
bottom bit of the Color API, and you'll notice that each colour is really represented by a number, and each number is a power of 2 (from 2 to the power of 0 (1), up to 2 to the power of 15 (32768)). In particular, take a look at the binary representations of those numbers and the pattern it forms as you read down the list.
Now, when you set your bundled output to green, what ComputerCraft is actually doing is assigning a value of 8192 to that cable (0010000000000000). If you set it to purple, then the number ends up as 1024 (0000010000000000). If you wish to enable
both those colours, then CC simply adds them together to get 9216. In binary, that looks like:
0000010000000000
+0010000000000000
-----------------
0010010000000000
0000000000000000 hence means "no colours", 1111111111111111 means "all colours", 1111111111101111 means "all colours except yellow" and so on.
So with all of the above in mind, what bit.band() does is perform the "and" operation logic that you'd usually use on your true/false statements and apply that to the bits in the binary representations of two numbers. The result is a new number that only has the bits set to 1 that were 1 in
both of the originals (a "bitwise and").
Eg:
0110010010001010
AND 0010010100111110
--------------------
0010010000001010
Hence, if you take the "bitwise and" of the number assigned to your cable output along with any individual colour, then if the resulting number equals the value of your colour, you know that colour is enabled in your cable (otherwise the result will be 0).
0101000101000010 -- Some value in your bundled cable...
AND 0000000001000000 -- The colour pink...
--------------------
0000000001000000 -- Also the colour pink!
Which is pretty much exactly what the
colors.test() function is there to check for you (something I only remembered
after writing my previous post).
That is to say, this:
if bit.band(rs.getBundledInput(rsSide),cableColor[mob]) == cableColor[mob] then
... gives the exact same result as this:
if colors.test(rs.getBundledInput(rsSide),cableColor[mob]) then
Anyway, an interesting quirk of numbers written in binary is that any
odd number will have the right-most bit set to 1, whereas any
even number has it set to 0. Hence checking what "bit.band(
number,1)==1" returns can tell you whether a number is odd or not. This is computationally faster then dividing by two and checking the remainder (ie, getting the modulus), because it's not performing any division - it's simply looking at that last bit, no matter how big or complex-looking the rest of the number really is.