Course contentsShow
← JavaScript
Lesson 254 of 2,7237. Arithmetic and OperatorsFree lesson

The Binary Number System Refresher

Understand how numbers are stored as sequences of 1s and 0s in 32-bit signed integers.

The Binary Number System Refresher

What you'll learn: How JavaScript stores numbers as sequences of 1s and 0s in 32-bit signed integers when performing bitwise operations.

Why Binary Matters for Bitwise Operators

When you use bitwise operators in JavaScript, numbers aren't treated as the decimal values you're used to — they're converted to binary representation: sequences of 1s and 0s. Understanding this is essential to making sense of how bitwise operations work.

What is Binary?

Binary is a base-2 number system (versus our everyday base-10 system). Each position represents a power of 2, just like each decimal position represents a power of 10.

Think of it like light switches: each "bit" is either ON (1) or OFF (0). When you line up 32 switches, you can represent any integer from about -2 billion to +2 billion.

32-Bit Signed Integers

JavaScript converts numbers to 32-bit signed integers for bitwise operations:

  • 32 bits means 32 positions, each holding a 1 or 0
  • Signed means it can represent both positive and negative numbers
  • The leftmost bit is the "sign bit" (0 = positive, 1 = negative)

For example:

  • The decimal number 5 becomes: 00000000000000000000000000000101
  • The decimal number -5 uses "two's complement" encoding

Reading Binary

Each bit position represents a power of 2, starting from the right:

  • Rightmost bit: 2⁰ = 1
  • Next bit: 2¹ = 2
  • Next bit: 2² = 4
  • And so on...

So 101 in binary = (1×4) + (0×2) + (1×1) = 5 in decimal.

Key Takeaway: JavaScript bitwise operators work on 32-bit binary representations of numbers, where each bit is either 1 or 0, and the leftmost bit indicates the sign (positive or negative).