Consultor Eletrônico



Kbase P155845: How to rewrite GET-BITS function?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   13/11/2009
Status: Unverified

GOAL:

How to rewrite GET-BITS function?

GOAL:

Is GET-BITS function available in version 8?

GOAL:

Is there a equivalent function for GET-BITS in version 8?

FACT(s) (Environment):

Progress 8.x
Progress 9.x
Progress 10.x
All Supported Operating Systems

FIX:

The following sample code constructs a map of the 32 bits for a given number and then allows a developer to simulate GET-BITS functionality by getting the value of a range of bits from it.

This routine first uses the getBitMap function, which uses a Division by Two algorithm to convert a decimal number into binary, then passes it back to the getBits function which extracts the value of the range of bytes requested.
FUNCTION getBitMap RETURNS CHARACTER ( INPUT piByte AS INTEGER ):

DEFINE VARIABLE iBit AS INTEGER NO-UNDO INITIAL 31.
DEFINE VARIABLE iTestByte AS INTEGER NO-UNDO.
DEFINE VARIABLE lNeg AS LOGICAL NO-UNDO.
DEFINE VARIABLE cBitMap AS CHARACTER NO-UNDO.

ASSIGN lNeg = (piByte LT 0)
piByte = piByte * (IF lNeg THEN -1 ELSE 1).

/* This loop uses the division by two algorithm to get a map of the bits in the passed byte.
This takes into account that, if the number is a negative, it reverses the bits and places
a 1 in the 32nd bit position. */
DO WHILE piByte GT 1:
iBit = (piByte MOD 2).
cBitMap = cBitMap + (IF (cBitMap GT "") THEN "," ELSE "") +
(IF lNeg AND iBit GT 0 THEN "0"
ELSE IF lNeg AND iBit EQ 0 THEN "1"
ELSE STRING(iBit)).

piByte = (piByte - iBit) / 2.
IF piByte EQ 1 THEN
cBitMap = cBitMap + "," + (IF lNeg THEN "0" ELSE "1").
END.

cBitMap = cBitMap + FILL((IF lNeg THEN ",1" ELSE ",0"),32 - NUM-ENTRIES(cBitMap)).

RETURN cBitMap.

END FUNCTION.

FUNCTION getBits RETURNS INTEGER ( INPUT piByte AS INTEGER,
INPUT piStartingPoint AS INTEGER,
INPUT piEndingPoint AS INTEGER ):

DEFINE VARIABLE cBitMap AS CHARACTER NO-UNDO.

DEFINE VARIABLE iBits AS INTEGER NO-UNDO.
DEFINE VARIABLE iCount AS INTEGER NO-UNDO.

ew">cBitMap = getBitMap(piByte).
IF piEndingPoint EQ 1 THEN
iBits = INTEGER(ENTRY(piStartingPoint,cBitMap)).
ELSE DO iCount = piStartingPoint TO piStartingPoint:
iBits = iBits + INTEGER(ENTRY(iCount,cBitMap)).
END.

RETURN iBits.

END FUNCTION.

/* Compare the results of the internal GET-BITS function (available in 9.1A and later) and this routine */
MESSAGE GET-BITS(-235641,5,1) SKIP
getBits(-235641,5,1)
VIEW-AS ALERT-BOX INFO BUTTONS OK..