Binary Representation of Numbers

Sign-Magnitude Representation

For example

and

Therefore, bin(125.6875) = 111101.1011

The sign-magnitude representation includes a sign bit, where represents a positive number and represents a negative number.

One's Complement

The one's complement of a positive number is the number itself. For a negative number, all bits except the sign bit are inverted.

Two's Complement

The two's complement of a positive number is the number itself. For a negative number, it's the one's complement plus 1.

Particularly, the 8-bit two's complement 1000 0000 stands for the number

Overflow

The range of numbers an 8-bit unsigned integer variable could represent is

The range of numbers an 8-bit signed integer variable could represent is

if we add two 8-bit unsigned integers and together, the sum we get should be . Similarly, will get

IEEE 754 Floating-Point Representation

A single-precision floating-point number has 32 bits. For example:

The significant digits part is called the mantissa, and the exponent part is also called the exponent. The mantissa must start with , which allows us to omit the before the decimal point. The exponent has 8 bits, the mantissa has 23 bits after omitting the leading 1, and there's 1 sign bit. Thus, we get the IEEE 754 single-precision floating-point representation of :

  • The leftmost (highest) bit is the sign bit
  • The exponent comes first, note that it needs to add a bias, i.e., the exponent part needs to add an offset of
  • The mantissa follows

Similarly:

Therefore:

Classification of Floating-Point Numbers

Normalized Numbers

The exponent bits (after adding 127) are not all 0 or all 1, so the range of the exponent is . The range of normalized numbers is:

Denormalized Numbers

The exponent bits are all 0, but the mantissa is not all 0. In this case, the actual exponent is defined as , representing the range

Zero

Zero has two forms. In denormalized numbers, set all mantissa bits to 0. By changing the sign bit, we have positive zero and negative zero.

Infinity

All exponent bits are 1, and all mantissa bits are 0. By changing the sign bit, we get and .

Not a Number

All exponent bits are 1, and not all mantissa bits are 0, representing .

ASCII Table

DecOctalHexChar
0x00NUL (Null)
0x09HT (Horizontal Tab)
0x0ALF (Line Feed)
0x0DCR (Carriage Return)
0x20Space
0x300
0x399
0x41A
0x5AZ
0x61a
0x7Az