<p>I'm trying to decode the result of the Python os.wait() function. This returns, according to the Python docs:</p>

<blockquote>
  <p>a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.</p>
</blockquote>

<p>How do I decode the exit status indication (which is an integer) to obtain the high and low byte? To be specific, how do I implement the decode function used in the following code snippet:</p>

<pre><code>(pid,status) = os.wait()
(exitstatus, signum) = decode(status) 
</code></pre>

[{'answer_id': 10129, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 5, 'selected': True, 'text': '<p>This will do what you want:</p>\n\n<pre><code>signum = status &amp; 0xff\nexitstatus = (status &amp; 0xff00) &gt;&gt; 8\n</code></pre>\n'}, {'answer_id': 10132, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 1, 'selected': False, 'text': '<p>You can unpack the status using <a href="https://docs.python.org/3/reference/expressions.html#shifting-operations" rel="nofollow noreferrer">bit-shifting</a> and <a href="https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations" rel="nofollow noreferrer">masking</a> operators.</p>\n<pre><code>low = status &amp; 0x00FF\nhigh = (status &amp; 0xFF00) &gt;&gt; 8\n</code></pre>\n<p>I\'m not a Python programmer, so I hope got the syntax correct.</p>\n'}, {'answer_id': 10148, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 0, 'selected': False, 'text': "<p>The folks before me've nailed it, but if you really want it on one line, you can do this:</p>\n\n<pre><code>(signum, exitstatus) = (status &amp; 0xFF, (status &gt;&gt; 8) &amp; 0xFF)\n</code></pre>\n\n<p>EDIT: Had it backwards.</p>\n"}, {'answer_id': 10213, 'author': 'Jason Pratt', 'author_id': 99, 'author_profile': 'https://Stackoverflow.com/users/99', 'pm_score': 4, 'selected': False, 'text': '<p>To answer your general question, you can use <a href="https://en.wikipedia.org/wiki/Bit_manipulation" rel="nofollow noreferrer">bit manipulation</a></p>\n<pre><code>pid, status = os.wait()\nexitstatus, signum = status &amp; 0xFF, (status &amp; 0xFF00) &gt;&gt; 8\n</code></pre>\n<p>However, there are also <a href="https://docs.python.org/3/library/functions.html" rel="nofollow noreferrer">built-in functions</a> for interpreting exit status values:</p>\n<pre><code>pid, status = os.wait()\nexitstatus, signum = os.WEXITSTATUS( status ), os.WTERMSIG( status )\n</code></pre>\n<p>See also:</p>\n<ul>\n<li>os.WCOREDUMP()</li>\n<li>os.WIFCONTINUED()</li>\n<li>os.WIFSTOPPED()</li>\n<li>os.WIFSIGNALED()</li>\n<li>os.WIFEXITED()</li>\n<li>os.WSTOPSIG()</li>\n</ul>\n'}, {'answer_id': 686128, 'author': 'RobM', 'author_id': 83100, 'author_profile': 'https://Stackoverflow.com/users/83100', 'pm_score': 2, 'selected': False, 'text': '<p>You can get break your int into a string of unsigned bytes with the <a href="http://docs.python.org/library/struct.html" rel="nofollow noreferrer">struct</a> module:</p>\n\n<pre><code>import struct\ni = 3235830701  # 0xC0DEDBAD\ns = struct.pack("&gt;L", i)  # "&gt;" = Big-endian, "&lt;" = Little-endian\nprint s         # \'\\xc0\\xde\\xdb\\xad\'\nprint s[0]      # \'\\xc0\'\nprint ord(s[0]) # 192 (which is 0xC0)\n</code></pre>\n\n<p>If you couple this with the <a href="http://docs.python.org/library/array.html" rel="nofollow noreferrer">array</a> module you can do this more conveniently:</p>\n\n<pre><code>import struct\ni = 3235830701  # 0xC0DEDBAD\ns = struct.pack("&gt;L", i)  # "&gt;" = Big-endian, "&lt;" = Little-endian\n\nimport array\na = array.array("B")  # B: Unsigned bytes\na.fromstring(s)\nprint a   # array(\'B\', [192, 222, 219, 173])\n</code></pre>\n'}, {'answer_id': 687977, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 2, 'selected': False, 'text': '<pre><code>exitstatus, signum= divmod(status, 256)\n</code></pre>\n'}, {'answer_id': 50074196, 'author': 'Barish Ahsen', 'author_id': 9712098, 'author_profile': 'https://Stackoverflow.com/users/9712098', 'pm_score': 0, 'selected': False, 'text': '<pre><code>import amp as amp\nimport status\nsignum = status &amp;amp; 0xff\nexitstatus = (status &amp;amp; 0xff00) &amp;gt;&amp;gt; 8\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/10123', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/742/']