Crystal Reports 9 has many bitwise operators such as And, Or, Xor but you may have a problem with this message "A boolean is required here." if you are trying to perform bitwise on bytes of numbers e.g. (1 xor 5).
Fortunately, you can create your own user-defined functions in the Formula Editor. Open Formula Editor, right click on the Report Custom Functions node in the workshop tree, select New and name your own function in the textbox.
Function Name: ToBin
Function (NumberVar n)
(
Local StringVar b;
Local NumberVar d;
b := "";
d := n;
Do
(
b := CStr(Truncate(d Mod 2),0) & b;
d := Truncate(d / 2);
)
While (d > 1);
CStr(Truncate(d),0) & b;
)
(
Local StringVar b;
Local NumberVar d;
b := "";
d := n;
Do
(
b := CStr(Truncate(d Mod 2),0) & b;
d := Truncate(d / 2);
)
While (d > 1);
CStr(Truncate(d),0) & b;
)
Function Name: ToNum
Function (StringVar b)
(
Local NumberVar lng := length(b);
Local NumberVar n := 0;
Local NumberVar i;
For i := lng To 1 Step -1 Do
(
n := n + ((2 ^ (lng – i)) * Truncate(ToNumber(Mid(b,i,1))))
);
n;
)
(
Local NumberVar lng := length(b);
Local NumberVar n := 0;
Local NumberVar i;
For i := lng To 1 Step -1 Do
(
n := n + ((2 ^ (lng – i)) * Truncate(ToNumber(Mid(b,i,1))))
);
n;
)
Function Name: BitAnd
Function (NumberVar n1, NumberVar n2)
(
Local StringVar b1 := ToBin(n1);
Local StringVar b2 := ToBin(n2);
Local NumberVar l1 := length(b1);
Local NumberVar l2 := length(b2);
Local NumberVar lng := IIF(l1 > l2, l1, l2);
Local StringVar x;
Local StringVar y;
Local StringVar z := "";
Local NumberVar i;
For i := lng To 1 Step -1 Do
(
x := "0";
If (l1 >= i) Then
x := Mid(b1, (l1-i)+1, 1);
y := "0";
If (l2 >= i) Then
y := Mid(b2, (l2-i)+1, 1);
z := z & IIF(x=y And x="1","1","0");
);
ToNum(z);
)
(
Local StringVar b1 := ToBin(n1);
Local StringVar b2 := ToBin(n2);
Local NumberVar l1 := length(b1);
Local NumberVar l2 := length(b2);
Local NumberVar lng := IIF(l1 > l2, l1, l2);
Local StringVar x;
Local StringVar y;
Local StringVar z := "";
Local NumberVar i;
For i := lng To 1 Step -1 Do
(
x := "0";
If (l1 >= i) Then
x := Mid(b1, (l1-i)+1, 1);
y := "0";
If (l2 >= i) Then
y := Mid(b2, (l2-i)+1, 1);
z := z & IIF(x=y And x="1","1","0");
);
ToNum(z);
)
Function Name: BitXOr
Function (NumberVar n1, NumberVar n2)
(
n1 + n2 – (BitAnd(n1, n2) * 2)
)
(
n1 + n2 – (BitAnd(n1, n2) * 2)
)
Function Name: BitOr
Function (NumberVar n1, NumberVar n2)
(
n1 + n2 – BitAnd(n1, n2)
)
(
n1 + n2 – BitAnd(n1, n2)
)
Example
BitAnd(1, 5) // = 1
BitXOr(1, 5) // = 4
BitOr(1, 5) // = 5
n := n + ((2 ^ (lng – i)) * Truncate(ToNumber(Mid(b,i,1))))
my crystal report finds an error in this statement in
Function Name: ToNum
just a note for the first comment, my version of crystal reports is 10.
the error message says that there is missing a “)”, but all the brakes are ok
It looks like some standard functions (Truncate, ToNumber, Mid) are deprecated/changed in version 10. Try to comment them out and see if error exists. You may need other function to replace them. However, I may be wrong because I never used version 10. Let’s me know if it helps.
Note to previous comment. It’s a copy paste error. The dash will not be interpreted as a minus so rewrite the minuses after paste.
Thanks for this fine functions.
Hi Joachim, I’m gladded it’s working and thanks for your helpful information.
Thanks for the post! I’ve been trying to do bitwise comparisons in CR 2011 and this has helped me out immensely. One benefit of CR being so static is that I can use the same information from 2008 and it works great. The only problem I ran into was performing a BitAnd operation when one of the numbers is negative.
My knowledge of binary operations is limited, so maybe someone else has a better solution. My intention was to detect a negative number and use Two’s complement to get the binary representation, so I added the following statement underneath your variable declarations:
If n < 0 Then n := (n * -1) – 1;
At the end of the function I added a variable that stores "CStr(Tuncate(d),0) & b;". If n is positive, the result is returned as-is. If n is negative, then each bit gets flipped. It's been working great so far, I just wish SAP had thought to implement bitwise operations from the get-go.