BITVAL: Evaluating a Bit String as an Integer
 How to:Evaluate a Bit String

Available Languages: reporting, Maintain

The BITVAL function evaluates a string of bits within a character string. The bit string can be any group of bits within the character string and can cross byte and word boundaries. The function evaluates the subset of bits in the string as an integer value.

If the number of bits is:

• Less than 1, the returned value is 0.
• Between 1 and 31 (the recommended range), the returned value is a zero or positive number representing the bits specified, extended with high-order zeroes for a total of 32 bits.
• Exactly 32, the returned value is the positive, zero, or the complement value of negative two, of the specified 32 bits.
• Greater than 32 (33 or more), the returned value is the positive, zero, or the complement value of negative two, of the rightmost 32 bits specified.
 Top of page
Syntax: How to Evaluate a Bit String
`BITVAL(source_string, startbit, number, output)`

where:

source_string

Alphanumeric

Is the character string to be evaluated, enclosed in single quotation marks, or a field or variable that contains the character string.

startbit

Integer

Is the number of the first bit in the bit string, counting from the left-most bit in the character string. If this argument is less than or equal to 0, the function returns a value of zero.

number

Integer

Is the number of bits in the subset of bits. If this argument is less than or equal to 0, the function returns a value of zero.

output

Integer

Is the name of the field that contains the binary integer equivalent, or the format of the output value enclosed in single quotation marks.

 Top of page
Example: Evaluating a Bit String

BITVAL evaluates the bits 12 through 20 of LAST_NAME and stores the result in a field with the format I5:

```TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
STRING_VAL/I5 = BITVAL(LAST_NAME, 12, 9, 'I5');
WHERE DEPARTMENT EQ 'MIS';
END```

The output is:

```LAST_NAME        STRING_VAL
---------        ----------
SMITH                   332
JONES                   365
MCCOY                    60
BLACKWOOD               316
GREENSPAN               412
CROSS                   413```
 WebFOCUS