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.
Definitely be careful with things like COMP S9(1) (or 2 or 3 or 5 6 7 - can't remember if 8 is a problem, compile with S9(9), if no messages, then 8 is a problem as well). Although COBOL will always resevere space on a half-word boundary for binary fields, you can't necessarily "use" all that space. COMP S9(1) will contain only -9 to +9. S9(2), -99 to +99. If you look at the binary of those, it is as Robert Sample says, all the minuses will have high-order-bit set to 1, and value in 2's complement. -1 for any COBOL COMP S9(n) will show as a whole bunch of F's.
What do you have to be careful of? Different results depending on compiler options. I always used to code COMP PIC S9(4) and explicitly make sure +/- 9999 could not be exceeded. Else, one day, you'll get a problem.
Maybe it is no longer true, but I'd guess it is, someone will point it out if it is wrong.