liveBook · Manning

Binary representation

As explained in JavaScript in Depth

A representation of numbers using digits in base 2 and powers of two.

Why does a decimal number need a different form before it can be stored? JavaScript interprets decimal notation, converts the value to binary, then encodes it in bytes. Each step can constrain what is represented accurately. section 3.1.1

Binary: 1011 = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 11 in decimal

Binary uses place values that are powers of two: 1011 means 8 + 0 + 2 + 1, or 11 in decimal. section 3.1.1

In a byte, the rightmost bit is least significant and the leftmost is most significant. An unset bit is 0; a set bit is 1. Whole-number bit patterns do not, by themselves, represent fractions such as 0.1. Signed integers introduce another encoding choice: in eight-bit two’s complement, −1 is represented by eight set bits. section 3.1.1

Table 3.3 Representing 0.1 and 0.2 as floating-point numbers
Value
Sign
Exponent
Significand
0.1
0
01111111011
1.1001100110011001100110011001100110011001100110011010
0.2
0
01111111100
1.1001100110011001100110011001100110011001100110011010

The floating-point encodings of 0.1 and 0.2 show how fractional values are represented using sign, exponent and significand fields rather than a whole-number bit pattern. section 3.1.1

How JavaScript in Depth covers it

How deep it goes
Works through positional bits and byte order, then connects signed and floating-point layouts without covering the full IEEE-754 standard.
Examples
Uses 1011 as 11, eight-bit encodings of signed integers, and floating-point encodings of 0.1 and 0.2.
What it assumes
Starts with bits, bytes and positional value while using familiar decimal notation as the comparison.
What you can do after
Evaluate/understand how positional bits and numeric encoding choices affect the representation of JavaScript numbers.
How it connects
Links binary representation to two’s complement, floating-point representation, IEEE 754 and byte encoding.

Also explained in

Compare how 7 books explain binary representation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.