Home > Software > Mainframe > Cobol
Interview Questions   Tutorials   Discussions   Programs   

Cobol - What will COMP-3 field occupy ?

asked mar August 22, 2014 12:19 PM  

What will COMP-3 field occupy ?


1 Answers

answered By Mswami   0  
Comp-3 fields in COBOL:

COBOL Comp-3 is a binary field type that puts ("packs") two digits into each byte, using a notation called Binary Coded Decimal, or BCD.   This halves the storage requirements compared to a character, or COBOL "display", field.  Comp-3 is a common data type, even outside of COBOL, and is fairly standard across platforms -- that is, it is not dependent upon the operating system, language, or CPU, as the COBOL "comp" is.

The value in a comp-3 field is stored high-order to low-order.  That is, the upper nibble of the first byte encountered in the file is the most significant digit of the value, the lower nibble of that byte is the next digit, etc., etc.  The last nibble -- the low nibble of the least significant byte -- is used to store the sign for the number. Unlike IBM Signed fields, this nibble stores only the sign, not a digit.  "C" hex is positive, "D" hex is negative, and "F" hex is unsigned.  In COBOL comp-3 fields (and in most other languages) this nibble is reserved for the sign whether or not the field is denoted as a signed field in the COBOL PIC.

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

Comp-3 fields are denoted in COBOL with the "usage is" clause after the PIC, like this:

 PIC S9(5) usage is computational-3.

However, the "usage is" is not required and seldom used, and "computational-3" is usually abbreviated "comp-3", so you more commonly see:

  PIC S9(5) comp-3.

The COBOL PIC, or picture, 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 that.  To calculate the number of bytes from the PIC, add 1 (for the sign) to the total number of digits, divide by 2, and round up if necessary.  For example:

PIC S9(7) COMP-3.   Byte size = (7 + 1) / 2 = 4

PIC S9(5)V99 COMP-3. Byte size = (5 + 2 + 1) / 2 = 4

PIC S9(6) COMP-3.   Byte size = (6 + 1) / 2 = 3.5, rounded to 4

Comp-3 fields reserve a nybble for the sign, even for "unsigned" values, so the following fields are still 4 bytes:
PIC 9(7) COMP-3.   Byte size = (7 + 1) / 2 = 4

PIC 9(6) COMP-3.   Byte size = (6 + 1) / 2 = 3.5, rounded to 4
   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!