Get Started
Bits and Bytes
On this page
  1. Bits
  2. Bytes
  3. Hex Code

Bits and Bytes

Background for working with binary data, such as byte arrays, the bit/byte blocks, and User Defined Communication (Part 8).

Bits

A computer's CPU contains billions of transistors (switches) that switch on and off billions of times per second. The computer reads a switch that is on as 1 and off as 0, so all data is expressed with 0 and 1 only; this is binary. One binary digit (0 or 1) is one bit. Decimal 3, for example, is 11 in binary; it has two digits, so it takes 2 bits.

BinaryDecimal
000
011
102
113

As above, 2 bits can represent 4 numbers (2²), and n bits can represent 2ⁿ numbers.

Bytes

Data comes in all sizes, so if the CPU accessed data bit by bit it would have to read and write far too often. Decimal 37, for example, is 100101 in binary, or 6 bits, and handling it bit by bit takes six reads or writes. Computers therefore process 8 bits together as one unit, called a byte. 37 is padded with 00 to 00100101, 8 bits (1 byte), and handled in one operation. A few bits are wasted, but processing is far more efficient.

Hex Code

Byte values are hard to read in binary, so they are shown in hexadecimal, called hex code. One byte fits in two hexadecimal digits, and 0x is usually prefixed to mark a hexadecimal value. Binary 11111111, for example, is 0xFF.