How signed integers are represented using 16 bits

xkcdcant_sleep

Source: http://www.xkcd.com/571/

This probably requires a little explanation with the nuances of how integers are stored in computers. A 16-bit signed integer simulates binary digits with 16 digits, where the first digit represents either positive (0) or negative (1). Because this first digit represents positive or negative, the counting system is a little different than regular counting in binary.

For starters,

0000000000000000 represents 0

0000000000000001 represents 1

0000000000000010 represents 2

\vdots

0111111111111111111 represents 2^{16}-1 = 32767

For the next number, 1000000000000000, there’s a catch. The first 1 means that this should represent a negative number. However, there’s no need for this to stand for -0, since we already have a representation for 0. So, to prevent representing the same number twice, we’ll say that this number represents 0 - 32768 = -32768, and we’ll follow this rule for all representations starting with 1. So

0000000000000000 represents 0-32768 = -32768

0000000000000001 represents 1 - 32768 = -32767

0000000000000010 represents 2 - 32768 = -32766

\vdots

0111111111111111111 represents 32767-32768 = -1

Because of this nuance, the following C computer program will result in the unexpected answer of -37268 (symbolized by the sheep going backwards in the comic strip).

main()

{

      short x = 32767;

      printf(“%d \n”, x + 1);

}

For more details, see http://en.wikipedia.org/wiki/Integer_%28computer_science%29

Gravity wells

xkcdgravity_wells

This is one of most creative diagrams that I’ve ever seen: the depth of various solar system gravity wells. A large version of this image can be accessed at http://xkcd.com/681_large/.

From the fine print:

Each well is scaled so that rising out of a physical well of that depth — in constant Earth surface gravity — would take the same energy as escaping from that planet’s gravity in reality.

Depth = \displaystyle \frac{G M}{g r}

It takes the same amount of energy to launch something on an escape trajectory away from Earth as it would to launch is 6,000 km upward under a constant 9.81 \hbox{m}/\hbox{s}^2 Earth gravity. Hence, Earth’s well is 6,000 km deep.

Here’s some more details about the above formula.

Step 1. The escape velocity from the surface of a spherical planet is

v = \displaystyle \sqrt{ \frac{2GM}{r} },

where G is the universal gravitational constant, M is the mass of the planet, and r is the radius of the planet. Therefore, the kinetic energy needed for a rocket with mass m to achieve this velocity is

E = \displaystyle \frac{1}{2} m v^2 = \frac{GMm}{r}

Step 2. Suppose that a rocket moves at constant velocity upward near the surface of the earth. Then the force exerted by the rocket exactly cancel the force of gravity, so that

F = mg,

where g is the acceleration due to gravity near Earth’s surface. Also, work equals force times distance. Therefore, if the rocket travels a distance d against this (hypothetically) constant gravity, then

E = mgd

The depth formula used in the comic is then found by equating these two expressions and solving for d.