Adsense

Monday, April 20, 2015

COMP-3 variable in cobol. Calculate the number of bytes for COMP-3 variable.

COMP3 in cobol enables a computer to store 2 digits in each storage position, except for the rightmost position where the sign is stored. Each digit takes half a byte.

Point to remember:  Even if we specify 'S' or not in the variable declaration,  the sign is stored.

Suppose we move the digit 1265875 into a field 9(7). In display mode, cobol will occupy 7 bytes, ie, 7 storage positions.
If we use COMP-3 in the variable declaration,  it will take only 4 Bytes like below.

12
65
87
5C

Had it been a negative number, then The rightmost digit would store 'D' in place of  C.

How many bytes does COMP-3 take ?
To calculate the byte-length of a comp-3 field, start with the total number of digits and divide by 2 giving a result (discarding the remainder if any), then add 1 to the result.
  
Note: For "normal" processing the maximum number of digits is 18 . If we use compiler option as the ARITH(EXTEND) , the compiler will allow us to extend it to 31 bytes

Just to share, if we use Odd number of digits for packed decimal, the processing becomes 5-20 % faster than what the speed would be if we use even number of digits!. (Source : Some Informative journal in comp-3 in google )