in reply to Re^2: why divide 256 for the exit code
in thread why divide 256 for the exit code

You want 8 bits, not 7. IMHO, if you are doing bit operations, you should use hex (or octal if you are old-school) representations, so that it is clear. In this case, you should & with 0xff

Replies are listed 'Best First'.
Re^4: why divide 256 for the exit code
by almut (Canon) on Sep 07, 2009 at 16:32 UTC
    In this case, you should & with 0xff

    Actually, I think it's sufficient to use $? & 0x7f (or $? & 127 in decimal) to test for abnormal termination.  That's what the code snippet in system suggests. Also, the system header files responsible for the exit status handling (e.g. /usr/include/bits/waitstatus.h in my case) usually define those macros (among others):

    #define __WTERMSIG(status) ((status) & 0x7f) /* Nonzero if STATUS indicates normal termination. */ #define __WIFEXITED(status) (__WTERMSIG(status) == 0)

    (from that I would conclude that the core-dumped bit ($? & 128) doesn't occur in isolation (i.e. without WTERMSIG being non-zero) )