Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

Extracting the exit status of a wait()ed-for process

You've seen a bunch of example code which looks something like this:
pid = wait(&status);
...
return(status >> 8);

What's this ">>" about?

Well, it goes like this. Suppose the process exit status was 3. As an eight-bit number in binary that is 00000011.

This is put together with some flags in another eight bits which indicate how the process terminated. Often they will all be zero. In that case, the number would be 0000001100000000.

As a base-ten number this is 768, but that's not useful for much. What we want is to extract that number "3".

To do this, we have to discard the low eight bits of the number 0000001100000000, so as to move the 00000011 part to the right. In machine-language programming terms this is called a "right shift" — the bits of the number are shifted to the right. Generally you can request a right-shift for any number of places. In the current case we want 8.

High-level languages (as opposed to machine languages) traditionally don't have shift operators, but as a programming language designed to be able to be a replacement for almost all uses of machine language, the C programming language does have shift operators. The right-shift operator is ">>".

So, we want "status >> 8".