0

I'm only a novice when it comes to bitwise math - if that's even the correct term - and was looking for a better way to do logic on an int-summed return code (as is standard for various Unix programs). I.e. the return code could be any combination of 1,2,4,8,etc

Here is my code (snippet) so far:

[...]
if (result == 0)
    //no problem
else {
    if ((result > 127) && (result % 128 == 0)) {
        // exit code contained 128
        result = result - 128;
    }
    if ((result > 63) && (result % 64 == 0)) {
        // exit code contained 64
        result = result - 64;
    }
    [...]
    if (result > 0) {
        // exit code contained 1
    }
}

I know that I should be able to do this with a bitwise operator such as AND (&) but am not sure how to go about it. I don't really understand much about bitwise operations though, i.e. if I do if (result & 64) could that also not be true if return code was 128?

Obviously my understanding of binary math is shocking, I've never really done code in this area. Just looking for some clarification of the correct bitwise method.

CosmicDan
  • 61
  • 2
  • 9
  • 1
    Maybe better suited to http://codereview.stackexchange.com/, since you appear to have working code. – Roberto Reale Apr 17 '14 at 15:32
  • 1
    `WEXITSTATUS` and the other macros from `` will do all the bit manipulation for you. It's not necessary or desirable to do it yourself. –  Apr 17 '14 at 15:38
  • 1
    `int contains_128 = result & 128, contains_64 = result & 64, ...` – twalberg Apr 17 '14 at 16:16
  • @RobertoReale, thanks for the newbie tip - although I'm a bit novice on the whole bitwise situation so was hoping to get some explanation. Will update my question. – CosmicDan Apr 18 '14 at 08:15
  • @WumpusQ.Wumbley I don't see how, the only macro of use to me is WEXITSTATUS which I am already getting, and it's a summed-value which could contain any combination of multiple status. I've read wait.h documentation and can't find any helpers. – CosmicDan Apr 18 '14 at 08:26
  • @twalberg, thanks for the tip - would you care to elaborate (I have updated my question) since I am a newbie with bitwise operations and would like some concerns addressed (I admit I'm not a very math-orientated person, strange I know) – CosmicDan Apr 18 '14 at 08:27
  • Ok I understand bitwise & op now. It will return > 0 if both compared bytes have a same bit set, makes complete sense *facepalm* typical cowboy I am. Well twalberg if you want rep feel free to answer, or anyone else who beats them to it :) – CosmicDan Apr 18 '14 at 08:40
  • I thought you were trying to dissect the status yourself without using the `W*` macros. Particularly the `128` part looked like an attempt to get the core dump bit. But applying bitwise operators to the output of `WEXITSTATUS` is different, and not bad at all. It's just not common to see a program that stores several independent booleans in the bits of its exit code. –  Apr 18 '14 at 12:08
  • "*I'm only a novice when it comes to bitwise math ...*" so you might like to read this in general: http://en.wikipedia.org/wiki/Bitwise_operation and this in particluar: http://en.wikipedia.org/wiki/Bitwise_operations_in_C – alk Apr 18 '14 at 14:55

1 Answers1

1

As pointed out in the comments by Wumpus Q Wumbley, for "standard" utilities/exit codes, there are a set of macros in <sys/wait.h> that are useful and recommended for extracting information from the exit codes. However, if the exit codes you're trying to process don't follow those usual conventions, you can extract individual bits with a simple logical AND operation like this:

int bit_7_is_set = result & (1U << 7);  // a.k.a. 128

If the result of the AND operation is non-zero (it will either be 0 or 128 in the above case), the corresponding bit is set in the result.

twalberg
  • 59,951
  • 11
  • 89
  • 84