Open In App

COMP-3 in COBOL

Last Updated : 26 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In COBOL, a programmer is allowed to specify the internal form of the records items that allows you to facilitate its use in the maximum efficient way. COMPUTATIONAL and DISPLAY are the two general forms of internal representation. It is used to store and compress the storage space. 

  • COMPUTATION USAGE – Store Binary Value
  • DISPLAY – Store Decimal Value

How Data Get Stored:

The Binary Coded Decimal (BCD) data type is exactly the same as its name represents,  it’s a value stored in decimal, and each digit is binary coded. Since a digit has only ten possible values ​​(0-9), it can only be represented in binary form with 4 bits. The four bits are called a “nibble” and each nibble contains one digit of the value. Therefore, you can get two digits in each 8-bit byte.

Value in the COMP-3 gets stored in high-order to low-order. the upper nibble of the first byte encountered within the document is the most significant digit of the value, the lower nibble of the byte is the following digit, and so on. the final nibble, the low nibble of the least enormous byte.  “C” hex is positive, “D” hex is negative, and “F” hex is unsigned.

Comp-3 fields are aligned on byte boundaries and a field is always an integer number of bytes. The nibble character is always the low LSD nibble (least significant digit). Since a sign takes one nibble, and since there are always an even number of nibbles in any number of bytes, an odd number of digits will completely fill the comp-3 field. (An odd number of digits plus a nibble sign makes an even number of nibbles or fully filled bytes). If the field size is specified as an even number of digits, as in “PIC S9(6) comp-3.”, the upper nibble is ignored and is usually, but not always, set to zero.

Example 1:

Cobol




COBOL program for COMP-3
IDENTIFICATION DIVISION.
PROGRAM-ID. Comp3-Code.
ENVIRONMENT DIVISION.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        77 WS-VALA  USAGE IS COMP-1.
        77 WS-VALB  USAGE IS COMP-2.
        77 WS-VALC  PIC S9(3USAGE IS COMP-3.
        77 WS-VALD  PIC S9(4USAGE IS COMP-3.
PROCEDURE DIVISION.
MAIN-PARA.
    DISPLAY '***COMP-1 USAGE DISPLAY***'.
        MOVE 999 TO WS-VALA.
    DISPLAY 'VALUE OF WS-VALA IS: ' WS-VALA.
    DISPLAY 'LENGHTH OF WS-VALA IS: ' LENGTH OF  WS-VALA.
    DISPLAY '***COMP-2 USAGE DISPLAY***'.
        MOVE 999 TO WS-VALB.
    DISPLAY 'VALUE OF WS-VALB IS: ' WS-VALB.
    DISPLAY 'LENGHTH OF WS-VALB IS: ' LENGTH OF WS-VALB.
    DISPLAY '***COMP-3 USAGE USING 2 BYTES DISPLAY***'.
        MOVE 999 TO WS-VALC.
    DISPLAY 'VALUE OF WS-VALC IS: ' WS-VALC.
    DISPLAY 'LENGHTH OF WS-VALC IS: ' LENGTH OF WS-VALC.
    DISPLAY '***COMP-3 USAGE USING 3 BYTES DISPLAY***'.
        MOVE 999 TO WS-VALD.
    DISPLAY 'VALUE OF WS-VALD IS: ' WS-VALD.
    DISPLAY 'LENGHTH OF WS-VALD IS: ' LENGTH OF WS-VALD.
STOP RUN.


Output:

 

Explanation:

  • For a comp-3 packed field specifies the number of digits after unpacking.  The actual number of bytes occupied in the file is about half of that bytes.
    To calculate the number of bytes, we have to add 1 (for sign) to the total number of digits, then need to divide it by 2, and round up.
  • Here in our taken example, we are considering the variable named WS-VALC and WS-VALD.
  • For WS-VALC PIC S9(3) USAGE IS COMP-3, so here we can take it as (3+1)/2 which is equal to 2bytes size.
  • Similarly, in the 3byte variable which is WS-VALD PIC S9(4) USAGE IS COMP-3, so here we can take it as (4+1)/2 which is equal to 2.5(rounded off) so it will take 3bytes.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads