Math calculation

pima

New Member
Hello folks,

I've to do an calculation within progess, but i've no idea how to get the formula below work in Progress (9.1D):

y = (x / 256) AND 255

The windows-calculator supplies the "AND" function, but can I also calculate this with progress?


I hope somebody can help me!


Thanks in advance!


Marcus.
 
It's not easy:

Functions that Implement Bit Manipulation (BINARY AND OR)

Title: Functions that Implement Bit Manipulation (BINARY AND OR
KnowledgeBase Number: 18648
Creation Date: 10-MAR-99
Modified Date: 28-APR-99
This document applies to: All Platforms
Version and Release Number: 8.2+ (can convert for earlier versions)

Summary:

The following functions are implemented in the code shown below:

- Convert a signed integer into an unsigned integer
- Convert an unsigned integer into a signed integer
- Do a bitwise AND of two integers
- Do a bitwise OR of two integers

Step by step details:

FUNCTION IntToUnsignedInt RETURNS DECIMAL
(INPUT intOperand AS INTEGER):

DEFINE VARIABLE decUnsignedInt AS DECIMAL NO-UNDO.

ASSIGN decUnsignedInt = intOperand.

IF intOperand < 0 THEN
ASSIGN decUnsignedInt = EXP(2, 32) + decUnsignedInt.

RETURN decUnsignedInt.

END FUNCTION.



FUNCTION UnsignedIntToInt RETURNS INTEGER
(INPUT decOperand AS DECIMAL):

DEFINE VARIABLE intSignedInt AS INTEGER NO-UNDO.

IF decOperand < EXP(2, 31) THEN
ASSIGN intSignedInt = decOperand.
ELSE
ASSIGN intSignedInt = decOperand - EXP(2, 32).

RETURN intSignedInt.

END FUNCTION.



FUNCTION BinaryAnd RETURNS INTEGER
(INPUT intOperand1 AS INTEGER, INPUT intOperand2 AS INTEGER):

DEFINE VARIABLE decValue AS DECIMAL NO-UNDO INITIAL 0.
DEFINE VARIABLE decLoop AS DECIMAL NO-UNDO.
DEFINE VARIABLE decOperand1 AS DECIMAL NO-UNDO.
DEFINE VARIABLE decOperand2 AS DECIMAL NO-UNDO.

ASSIGN decLoop = EXP(2, 31)
decOperand1 = IntToUnsignedInt(intOperand1)
decOperand2 = IntToUnsignedInt(intOperand2).

DO WHILE decLoop > 0.5:
ASSIGN decValue = decValue + decLoop
WHEN decOperand1 >= decLoop AND decOperand2 >= decLoop
decOperand1 = decOperand1 - decLoop
WHEN decOperand1 >= decLoop
decOperand2 = decOperand2 - decLoop
WHEN decOperand2 >= decLoop
decLoop = decLoop / 2.
END.

RETURN UnsignedIntToInt(decValue).

END FUNCTION.



FUNCTION BinaryOr RETURNS INTEGER
(INPUT intOperand1 AS INTEGER, intOperand2 AS INTEGER):

DEFINE VARIABLE decValue AS DECIMAL NO-UNDO INITIAL 0.
DEFINE VARIABLE decLoop AS DECIMAL NO-UNDO.
DEFINE VARIABLE decOperand1 AS DECIMAL NO-UNDO.
DEFINE VARIABLE decOperand2 AS DECIMAL NO-UNDO.

ASSIGN decLoop = EXP(2, 31)
decOperand1 = IntToUnsignedInt(intOperand1)
decOperand2 = IntToUnsignedInt(intOperand2).

DO WHILE decLoop > 0.5:
ASSIGN decValue = decValue + decLoop
WHEN decOperand1 >= decLoop OR decOperand2 >= decLoop
decOperand1 = decOperand1 - decLoop
WHEN decOperand1 >= decLoop
decOperand2 = decOperand2 - decLoop
WHEN decOperand2 >= decLoop
decLoop = decLoop / 2.
END.

RETURN UnsignedIntToInt(decValue).

END FUNCTION.

eg:

y = BinaryAnd((x / 256), 255) [untested]

HTH


ps. can someone tell me how to preserve code indentation when posting?
 
Here's a math library sample, I didn't add comments but I think it's pretty self explanatory. HTH

<snippet>

/* math.p */

FUNCTION binAND RETURNS INTEGER ( piInt1 AS INTEGER, piInt2 AS INTEGER ):

DEFINE VAR iReturn AS INT NO-UNDO.
DEFINE VAR i AS INT NO-UNDO.

DO i = 1 TO 32:

PUT-BITS( iReturn, i, 1 ) =
( IF GET-BITS( piInt1, i, 1 ) = 1
AND GET-BITS( piInt2, i, 1 ) = 1
THEN 1 ELSE 0 ).

END. /* i = 1 to 32 */

RETURN iReturn.

END FUNCTION. /* binAND */

FUNCTION binOR RETURNS INTEGER ( piInt1 AS INTEGER, piInt2 AS INTEGER ):

DEFINE VAR iReturn AS INT NO-UNDO.
DEFINE VAR i AS INT NO-UNDO.

DO i = 1 TO 32:

PUT-BITS( iReturn, i, 1 ) =
( IF GET-BITS( piInt1, i, 1 ) = 1
OR GET-BITS( piInt2, i, 1 ) = 1
THEN 1 ELSE 0 ).

END. /* i = 1 to 32 */

RETURN iReturn.

END FUNCTION. /* binOR */

FUNCTION binXOR RETURNS INTEGER ( piInt1 AS INTEGER, piInt2 AS INTEGER ):

DEFINE VAR iReturn AS INT NO-UNDO.
DEFINE VAR i AS INT NO-UNDO.

DO i = 1 TO 32:

PUT-BITS( iReturn, i, 1 ) =
( IF GET-BITS( piInt1, i, 1 ) = 1
OR GET-BITS( piInt2, i, 1 ) = 1
AND NOT ( GET-BITS( piInt1, i, 1 ) = 1
AND GET-BITS( piInt2, i, 1 ) = 1 )
THEN 1 ELSE 0 ).

END. /* i = 1 to 32 */

RETURN iReturn.

END FUNCTION. /* binXOR */

FUNCTION binNOT RETURNS INTEGER ( piInt AS INTEGER ):

DEFINE VAR iReturn AS INT NO-UNDO.
DEFINE VAR i AS INT NO-UNDO.

DO i = 1 TO 32:

PUT-BITS( iReturn, i, 1 ) =
( IF GET-BITS( piInt, i, 1 ) = 1 THEN 0 ELSE 1 ).

END. /* i = 1 to 32 */

RETURN iReturn.

END FUNCTION. /* binNOT */

/* mathprto.i */

FUNCTION binAND RETURNS INTEGER (INPUT piInt1 AS INTEGER, INPUT piInt2 AS INTEGER) IN SUPER.

FUNCTION binOR RETURNS INTEGER (INPUT piInt1 AS INTEGER, INPUT piInt2 AS INTEGER) IN SUPER.

FUNCTION binXOR RETURNS INTEGER (INPUT piInt1 AS INTEGER, INPUT piInt2 AS INTEGER) IN SUPER.

FUNCTION binNOT RETURNS INTEGER (INPUT piInt AS INTEGER) IN SUPER.

/* math.i */

&IF DEFINED( libmath ) = 0 &THEN

{libmathprto.i}

DEFINE VAR hMath AS HANDLE NO-UNDO.
hMath = SESSION:FIRST-PROCEDURE.

DO WHILE VALID-HANDLE( hMath ) AND hMath:FILE-NAME NE "math.p":
hMath = hMath:NEXT-SIBLING.
END. /* WHILE VALID-HANDLE */

IF NOT VALID-HANDLE( hMath ) THEN
RUN math.p PERSISTENT SET hMath.

THIS-PROCEDURE:ADD-SUPER-PROCEDURE( hMath ).

&ENDIF /* defined( math ) = 0 */

&GLOBAL-DEFINE math YES



/* bin.i */

&IF "{6}" <> "" &THEN

bin{1}( {2}, bin{1}( {3}, bin{1}( {4}, bin{1}( {5}, {6} ) ) ) )

&ELSEIF "{5}" <> "" &THEN

bin{1}( {2}, bin{1}( {3}, bin{1}( {4}, {5} ) ) )

&ELSEIF "{4}" <> "" &THEN

bin{1}( {2}, bin{1}( {3}, {4} ) )

&ELSEIF "{3}" <> "" &THEN

bin{1}( {2}, {3} )

&ENDIF



/* and ( dot nothing ) */

{bin.i "AND" {*}}

/* or ( dot nothing ) */

{bin.i "OR" {*}}

/* xor ( dot nothing ) */

{bin.i "XOR" {*}}

/* not ( dot nothing ) */

binNOT( {1} )



/* main.p */

{math.i}

DEFINE VAR x AS INT NO-UNDO.

x = 1.

DISPLAY {AND x 255}.

</snippet>
 
Back
Top