In this topic, we described about the Computation/COMP/BINARY with detailed example.
Specifies for binary data items. The data items consisting of the decimal digits 0 through 9 and a sign. COMP usage clause applicable to numeric data type only. COMP usage is a binary representation of data and stores in pure binary format.
The amount of storage occupied by a binary item depends on the number of decimal digits defined in its PICTURE clause. The COMPUTATIONAL phrase is synonymous with BINARY and equal to BINARY.Below table represents the storage occupation based on the number of digits in PICTURE clause –
|Digits in PICTURE clause||Storage occupied|
|1 through 4||2 bytes (halfword)|
|5 through 9||4 bytes (full word)|
|10 through 18||8 bytes (doubleword)|
The operational sign is contained in the leftmost bit. If TRUNC compiler option in effect, BINARY, COMPUTATIONAL and COMPUTATIONAL-4 data items are affected.
Practical Example -
The above example, writing the record from the working-storage record and see how it is displayed during the file browse.
In the program, all variables declared with COMP USAGE.
In COMP USAGE, the memory calculation was provided on the above table. So there should be change in the output when we displayed from file.
The program output writing to MTH.COMP.OUTPUT file.
The below output shows the output written by the program and it can’t be in user understandable format.
But hex-on output can be understandable based on the below calculations.
As everybody aware, the data storing in memory for the data will be in the format of bits.
Refer memory storage structure & memory format conversion topic here.
Hex-on output always refers two layers. The data displays in each byte are a combination of both layers.
In the above hex-on output, 255 was displayed in two layers as
The calculations of the hex-on memory to its normal value like below.
For calculation, the lower left hand side byte will consider the first and next upper byte next move to 1 byte right and begins from lower and so on.
The below diagram can explain it very well about the positions of considerations.
Then it will apply the Hex-on to decimal calculation on the data showed in memory.
Hex on to decimal calculation for the above are
COMP-NM1 Decimal = 163 * 0 + 162 * 0 + 161 * F + 160 * F = (4096 * 0) + (256 * 0) + (16 * F) + (1 * F) = 0 + 0 + (16 * F) + F = (16 * 15) + 15 = 240 + 15 = 255 COMP-NM2 Decimal = 167*0 + 166*0 + 165*0 + 164*3 + 163*E + 162*4 + 161*9 + 160*8 = (65536 * 3) + (4096 * E) + (256 * 4) + (16 * 9) + (1 * 8) = 196608 + 57344 + 1024 + 144 +8 = 255128
Similarly the calculation would be same for COMP-NM3