Boolx is an inbuilt function which returns a byte chain of type xstring.
Below example shows how individual bits are being set. Bitwise OR operations are able to give final result which has bit string 01010101 or hex value 55.
DATA(result) = boolx( bool = 2 > 1 bit = 8 )
BIT-OR boolx( bool = 2 < 1 bit = 7 )
BIT-OR boolx( bool = 2 > 1 bit = 6 )
BIT-OR boolx( bool = 2 < 1 bit = 5 )
BIT-OR boolx( bool = 2 > 1 bit = 4 )
BIT-OR boolx( bool = 2 < 1 bit = 3 )
BIT-OR boolx( bool = 2 > 1 bit = 2 )
BIT-OR boolx( bool = 2 < 1 bit = 1 ).
Documentation says this function allows efficient saving of truth values. Not sure how much performace will be gained when the space is littered with inefficient code. Bit position 1 is leftmost and 8 is rightmost. A byte has only 8 bits.
There is also a fancy code that I don't understand but it does same as above using REDUCE table expression.
DATA(result) =
REDUCE xstring( INIT x TYPE xstring
FOR j = 4 THEN j - 1 UNTIL j < 1
LET b1 = 2 * j b2 = 2 * j - 2 IN
NEXT x = x BIT-OR boolx( bool = 2 > 1 bit = b1 )
BIT-OR boolx( bool = 2 < 1 bit = b2 ) ).
Still if I had to explain without understanding, I would go like this:
1. x variable of type xstring is declared.
2. A loop counter j value starts with 4 and in every pass it is decremented till it becomes 1. Value zero is the exit condition. Total 4 passes.
3. 2 more variables are declared, b1 and b2. b1 = 2 * j. b2 = 2 * j - 2. So in loop passes, (b1,b2) values go like this (8,6) (6,4) (4,2) (2,0).
4. Bitwise OR is done on x, boolx using b1 and boolx using b2.
But since the number sequence 86644220 does not look like 87654321, it looks like the code is incorrect in documentation. However, the end result is correct because we just need to set the bit positions 8, 6, 4 and 2. Odd bit positions are anyways in initial state.
Correct code would be this:
DATA(result) =
REDUCE xstring( INIT x TYPE xstring
FOR j = 4 THEN j - 1 UNTIL j < 1
LET b1 = 2 * j b2 = 2 * j - 1 IN
NEXT x = x BIT-OR boolx( bool = 2 > 1 bit = b1 )
BIT-OR boolx( bool = 2 < 1 bit = b2 ) ).
Above code would be setting bit positions 87654321.
No comments:
Post a Comment