Robert Sample wrote:Simple rule of thumb for COMP (binary) variables: if the first bit is 0, the number is positive and if the first bit is 1, the number is negative. If the first bit is 1, the entire value (16, 32, or 64 bits) is in two's complement notation.
Schubie wrote:Also remember that the smallest usable binary number is a halfword or 2 bytes. COBOL's S9(1) COMP will be a halfword. Even using Assembler, trying to work with a signed single byte binary takes too much code to properly treat a number whose maximum value would only be 126 and COBOL won't do it anyhow.
is not always true. There is a COBOL compile option, TRUNC, which allows COBOL to ignore the PICTURE size and use the full set of binary values for COMP variables. This option has been around for a number of years.though COBOL will always resevere space on a half-word boundary for binary fields, you can't necessarily "use" all that space.
Robert Sample wrote:BillyBoyo,though COBOL will always resevere space on a half-word boundary for binary fields, you can't necessarily "use" all that space. is not always true. There is a COBOL compile option, TRUNC, which allows COBOL to ignore the PICTURE size and use the full set of binary values for COMP variables. This option has been around for a number of years.
TRUNC has no effect on COMP-5 data items; COMP-5 items are handled as if TRUNC(BIN) were in effect regardless of the TRUNC suboption specified.
TRUNC(STD) applies only to USAGE BINARY receiving fields in MOVE statements and arithmetic expressions. When TRUNC(STD) is in effect, the final result of an arithmetic expression, or the sending field in the MOVE statement, is truncated to the number of digits in the PICTURE clause of the BINARY receiving field.
TRUNC(OPT) is a performance option. When TRUNC(OPT) is in effect, the compiler assumes that data conforms to PICTURE specifications in USAGE BINARY receiving fields in MOVE statements and arithmetic expressions. The results are manipulated in the most optimal way, either truncating to the number of digits in the PICTURE clause, or to the size of the binary field in storage (halfword, fullword, or doubleword).
Use the TRUNC(OPT) option only if you are sure that the data being moved into the binary areas will not have a value with larger precision than that defined by the PICTURE clause for the binary item. Otherwise, unpredictable results could occur. This truncation is performed in the most efficient manner possible; therefore, the results are dependent on the particular code sequence generated. It is not possible to predict the truncation without seeing the code sequence generated for a particular statement.
There are some cases when programs compiled with the TRUNC(OPT) option under Enterprise COBOL could give different results than the same programs compiled under OS/VS COBOL with NOTRUNC. You must actually lose nonzero high-order digits for this difference to appear. For statements where loss of high-order digits might cause a difference in results between Enterprise COBOL and OS/VS COBOL, Enterprise COBOL issues a diagnostic message. If you receive this message, make sure that either the sending items will not contain large numbers or that the receivers are defined with enough digits in the PICTURE clause to handle the largest sending data items.
The TRUNC(BIN) option applies to all COBOL language that processes USAGE BINARY data. When TRUNC(BIN) is in effect, all binary items (USAGE COMP, COMP-4, or BINARY) are handled as native hardware binary items, that is, as if they were each individually declared USAGE COMP-5:
BINARY receiving fields are truncated only at halfword, fullword, or doubleword boundaries.
BINARY sending fields are handled as halfwords, fullwords, or doublewords when the receiver is numeric; TRUNC(BIN) has no effect when the receiver is not numeric.
The full binary content of fields is significant.
DISPLAY will convert the entire content of binary fields with no truncation.
Recommendations: TRUNC(BIN) is the recommended option for programs that use binary values set by other products. Other products, such as IMS, DB2, C/C++, FORTRAN, and PL/I, might place values in COBOL binary data items that do not conform to the PICTURE clause of the data items. You can use TRUNC(OPT) with CICS programs as long as your data conforms to the PICTURE clause for your BINARY data items.
USAGE COMP-5 has the effect of applying TRUNC(BIN) behavior to individual data items. Therefore, you can avoid the performance overhead of using TRUNC(BIN) for every binary data item by specifying COMP-5 on only some of the binary data items, such as those data items that are passed to non-COBOL programs or other products and subsystems. The use of COMP-5 is not affected by the TRUNC suboption in effect.
Large literals in VALUE clauses: When you use the compiler option TRUNC(BIN), numeric literals specified in VALUE clauses for binary data items (COMP, COMP-4, or BINARY) can generally contain a value of magnitude up to the capacity of the native binary representation (2, 4, or 8 bytes) rather than being limited to the value implied by the number of 9s in the PICTURE clause.