Arithmetic to bit-string conversion converts the absolute value of an arithmetic value to a bit string, but may not produce the result you expect.
The arithmetic source value is first converted to fixed binary(K) where K is determined by the source data type, as shown in the following table.
Source Data Type | Value of K |
---|---|
Fixed Decimal(P,Q) | MIN(N,MAX(CEIL((P-Q)*3.32),0)) |
Float Decimal(P) | MIN(N,CEIL(P*3.32)) |
Fixed Binary(P) | (P) |
Float Binary(P) | MIN(N,P) |
The absolute value of the resulting integer is considered to be a bit string of length K. If the context that caused the conversion supplied a target length, this bit string is extended on the right with zero bits to make it the length of a longer target, or excess bits are truncated from the right to make it the length of a shorter target. The program is in error and may produce unpredictable results if the value of the source is too large to be converted to an integer of precision (K). For example, if V is a Fixed Binary(15) variable with the value of 5, its value when converted to a bit string of length 15 is '000000000000101'B. If the target is Bit(16), the result is '0000000000001010'B, because padding occurs on the right. If the target is Bit(5), the result is '00000'B, because all excess bits are truncated from the right.