Kbase 21355: Two Programs To Do Bit-wise (Binary) XOR Using Progress 4GL
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  11/12/2002 |
|
Status: Unverified
GOAL:
How to do a binary (bit-wise) XOR on two integer values using the Progress 4GL language.
FIX:
The function is followed by two MESSAGE statements demonstrating how
this function may be used recursively and how it may be used with either character or integer input values.
There are 2 solutions given. Both show the same message, however the code is different. The second solution has less code lines. It is another option to get to the same result.
/* SOLUTION 1 */
FUNCTION BinaryXOR RETURNS INTEGER (INPUT iFirstOperand AS integer,
INPUT iSecondOperand AS integer):
DEFINE VARIABLE iLoopCounter AS INTEGER no-undo.
DEFINE VARIABLE iXORedResult AS INTEGER no-undo.
DEFINE VARIABLE iFirstOpBit AS INTEGER no-undo.
DEFINE VARIABLE iSecondOpBit AS INTEGER no-undo.
DEFINE VARIABLE lFirstOpBit AS LOGICAL no-undo.
DEFINE VARIABLE lSecondOpBit AS LOGICAL no-undo.
iXORedResult = 0.
DO iLoopCounter = 0 to 31:
ASSIGN
iFirstOpBit = GET-BITS(iFirstOperand, iLoopCounter + 1, 1)
iSecondOpBit = GET-BITS(iSecondOperand, iLoopCounter + 1, 1)
lFirstOpBit = (iFirstOpBit = 1)
lSecondOpBit = (iSecondOpBit = 1).
IF (lFirstOpBit AND NOT lSecondOpBit) OR
(lSecondOpBit AND NOT lFirstOpBit) THEN
iXORedResult = iXORedResult + EXP(2, iLoopCounter).
END.
RETURN iXORedResult.
END FUNCTION.
MESSAGE "The BinaryXOR OF the CHARACTER STRING 'abcd' is "
BinaryXOR(BinaryXOR(BinaryXOR(ASC('d') , ASC('c')), ASC('b')),
ASC('a'))
VIEW-AS ALERT-BOX INFO BUTTONS OK.
MESSAGE "The BinaryXOR OF the four integers values 1, 3, 5 and 7 is "
BinaryXOR(BinaryXOR(BinaryXOR(1 , 3), 4), 7)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* SOLUTION 2 */
FUNCTION BinaryXOR RETURNS INTEGER (INPUT iFirstOperand AS integer,
INPUT iSecondOperand AS integer):
DEFINE VARIABLE iLoopCounter AS INTEGER no-undo.
DEFINE VARIABLE iXORedResult AS INTEGER no-undo initial 0.
DEFINE VARIABLE iFirstOpBit AS INTEGER no-undo.
DEFINE VARIABLE iSecondOpBit AS INTEGER no-undo.
DO iLoopCounter = 1 to 32:
IF GET-BITS(iFirstOperand, iLoopCounter, 1) + GET-BITS(iSecondOperand, iLoopCounter, 1) EQ 1 THEN
iXORedResult = iXORedResult + EXP(2, iLoopCounter - 1).
END.
RETURN iXORedResult.
END FUNCTION.
MESSAGE "The BinaryXOR OF the CHARACTER STRING 'abcd' is "
BinaryXOR(BinaryXOR(BinaryXOR(ASC('d') , ASC('c')), ASC('b')),
ASC('a'))
VIEW-AS ALERT-BOX INFO BUTTONS OK.
MESSAGE "The BinaryXOR OF the four integers values 1, 3, 5 and 7 is "
BinaryXOR(BinaryXOR(BinaryXOR(1 , 3), 4), 7)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
References to Written Documentation:
Progress Language Reference Manual