Binary Formula Language Operations
In a recent project, there were a bunch of different things that could happen to a document, and they were all mutually exclusive. We wanted to keep track of all the items. But instead of having a bunch of different fields with Yes/No or 1/0 values, I thought it would be more efficient to have a bit-wise value. For example, the number 116 would represent that option 1, 2, 3, and 5 happened. This is because 116 in binary is 1110100.

Boolean operation is built-in to LotusScript, but there's no equivalent in formula language. So I had to come up with something.

This formula below will convert any number (in a field called Value) to a binary string of digits - each digit is a "0" or a "1".

B := "";
Temp := Value
@While(Temp > 0;
M := @Modulo(Temp; 2);
@If(B = ""; B := @Text(M); B := @Text(M) + B);
Temp := Temp - M;
Temp := Temp / 2
);
B

This formula could be consolidated more, but here's what's going on. You take the remainder of the number and 2 (which will be either 1 or 0). Then you put that digit, as a string, to the FRONT of the return string. Then you divide by 2 and eliminate any remainders. Then you repeat the process until the value is 0. The way I divided by 2 and eliminated remainders was to subtract the remainder (subtract either 0 or 1) which gave me an even number. Then dividing by 2 didn't have to worry about any remainders.

So that's all good, but that doesn't help any UI. What I ended up doing was creating a computed for display check box field. The values were the descriptions of each bit with an alias of the bit as an integer. For example:

Sunday|64
Monday|32
Tuesday|16
Wednesday|8
Thursday|4
Friday|2
Saturday|1

(The values weren't days of the week, but it's proprietary, so I can't actually say what they were. This gives you an idea what was going on).

The value for computed for display field takes the binary representation (the value "B" shown above) and finds out which bits are set:

B := "";
Temp := Value
@While(Temp > 0;
M := @Modulo(Temp; 2);
@If(B = ""; B := @Text(M); B := @Text(M) + B);
Temp := Temp - M;
Temp := Temp / 2
);
V := 1;
R := ""
@For(i := @Length(B); i >= 1; i := i-1;
D := @Right(@Left(B; i); 1);
@If(D = "1"; R := R : @Text(V); 0);
V := V*2
);
@Trim(R)

Going back to our example of 116, the first pass through (V = 1) would see that the right most character is "0" and not add anything to the return value. Then V = 2, and the 2nd to last character is also "0". When V = 4, the 3rd to last character is "1", so "4" gets added to the array. Similarly, 16, 32, and 64 get added to the array. So the value for the computed for display checkbox field is "4" : "16" : "32" : "64", which means (in our example) that Sunday, Monday, Tuesday, and Thursday would be checked off.