Skip to content
Study Hall
Lesson 2 of 3Intro to Computers
Unit 1 · Lesson 2 of 3

How Computers Represent Data

Why everything is binary, how to convert between binary, decimal and hexadecimal, how text, images and sound become numbers, and why a Java int can overflow.

8 min read12 practice questionsInteractiveAsk AI about this →

Key things to remember

  1. 1Binary place values from the right: — doubling each time.
  2. 2An -bit pattern has possible values: unsigned they run to , signed they run to .
  3. 38 bits = 1 byte, and 4 bits = exactly 1 hex digit because .
  4. 4Java int is 32 bits signed, so its range is to ; exceeding it wraps around silently.
  5. 51 KiB = 1024 bytes but 1 kB = 1000 bytes; lowercase b is bits, uppercase B is bytes.
  6. 6ASCII: A is 65, a is 97 (always 32 apart), and the character 0 is 48.

Inside a processor there are billions of transistors, and each one is just a switch. It conducts or it does not — high voltage or low. There is no reliable middle setting, because temperature, manufacturing tolerance and electrical noise would make ten distinguishable voltage levels unreadable at billions of cycles per second. So the entire machine counts in a system with exactly two digits. Every photograph, song, message and Java program on your computer is, physically, a very long pattern of switches. What makes the pattern mean something is an agreement about how to read it.

INPUTkeyboard, sensorPROCESSCPU + RAMarithmetic & logicOUTPUTscreen, motorSTORAGESSD / HDD — keeps dataEvery computing device, from a smartwatch to a server, is this loop.Storage is the only stage that survives a power cut.
Fig 1Input, process, output and storage. Every stage moves the same bits.

Bits, bytes and place value

A bit is one 0 or 1. Eight bits make a byte, which is the smallest chunk most systems can address individually. You already understand how binary numbers work, because you already understand place value: in decimal, 305 means three hundreds, no tens and five ones, and each column is worth ten times the one to its right because there are ten digits. Binary has two digits, so each column is worth twice the one to its right. The place values from the right are 1, 2, 4, 8, 16, 32, 64, 128. Nothing else about the arithmetic changes.

Binary is just place value with only two digits.1281640321161804120112ⁿ128 + 32 + 16 + 4 + 1 = 181Hex groups the same bits in fours:1011 → B0101 → 510110101₂ = 181₁₀ = B5₁₆
Fig 2One byte read three ways. The bits never change, only how we group them.

Binary to decimal, and back

To go from binary to decimal, write the place values above the bits and add up only the columns holding a 1. To go from decimal to binary, the division method is the one that is hardest to get wrong: divide by 2 repeatedly, writing down each remainder, then read the remainders from the bottom up.

Add only the columns that hold a one.
Divide by 2QuotientRemainder
214 ÷ 21070
107 ÷ 2531
53 ÷ 2261
26 ÷ 2130
13 ÷ 261
6 ÷ 230
3 ÷ 211
1 ÷ 201
Read the remainder column from the BOTTOM up: 11010110.
The most common conversion error
Reading division remainders top-to-bottom instead of bottom-to-top reverses the whole answer. The last remainder you calculate is the most significant bit. Always check by converting back: if the place values do not add up to the number you started with, you read the column the wrong way.

Why hexadecimal exists

Binary is correct but unreadable — nobody dictates 11010110 over the phone without making a mistake. Hexadecimal is base 16, and because 16 is , exactly four bits make exactly one hex digit with no arithmetic carrying across the boundary. A byte becomes two characters, a colour becomes six, a memory address becomes eight. That is why every error code, MAC address and web colour you will ever see is written in hex. The digits above 9 are letters: A is 10, B is 11, C is 12, D is 13, E is 14, F is 15.

DecimalBinary (4-bit)Hex
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
Learn this table. Every hex conversion is four lookups, not a calculation.
Never go through decimal
To convert binary to hex, split the bits into groups of four starting from the RIGHT and look each group up. 10110110 becomes 1011 0110, which is B6. Going via decimal works but adds an arithmetic step where mistakes happen.
Loading interactive…
Flip bits and watch all three representations move together.

The units ladder, and the 1024 problem

Above the byte the prefixes climb, but there are two competing sets of them and that difference causes a question that appears in every course like this one. Storage manufacturers and network engineers use decimal prefixes: a kilobyte is 1000 bytes. Memory sizing and most operating systems use binary prefixes: 1024 bytes, because memory is addressed in powers of two. The proper names for the binary ones are kibibyte, mebibyte and gibibyte, but many programs still label them KB, MB and GB.

Storage marketing uses powers of 10; memory uses powers of 2.bit (b)1one 0 or 1byte (B)8 bitsone ASCII characterkilobyte (KB)1 000 Ba short emailmegabyte (MB)1 000 KBa phone photogigabyte (GB)1 000 MBa movieterabyte (TB)1 000 GBa laptop drive1 KiB = 1024 B. A "500 GB" drive shows as about 465 GiB in Windows — no bytes went missing.
Fig 3The units ladder, and what each step holds in practice.
ADecimal prefixes (powers of 10)
  • 1 kB = 1 000 bytes
  • 1 MB = 1 000 000 bytes
  • 1 GB = 1 000 000 000 bytes
  • Used by drive makers, network speeds and the SI standard
BBinary prefixes (powers of 2)
  • 1 KiB = 1 024 bytes
  • 1 MiB = 1 048 576 bytes
  • 1 GiB = 1 073 741 824 bytes
  • Used for RAM sizing and by many operating systems
Why a 500 GB drive shows as 465 GB
The manufacturer sold you 500 000 000 000 bytes, which is honest. The operating system divides by 1 073 741 824 and then labels the result GB when it really means GiB. 500 000 000 000 divided by 1 073 741 824 is about 465.7. No bytes went missing; two different definitions of the same abbreviation were used. Also watch the capital letter: a lowercase b means bits and an uppercase B means bytes, so a 100 Mb/s connection downloads at 12.5 MB/s at best.

Turning real things into numbers

A bit pattern means nothing on its own. The byte 01000001 is the number 65, the letter A, a very dark red, or a fragment of a machine instruction, and which one it is depends entirely on what the program reading it expects. ASCII assigns a number from 0 to 127 to each English character, fitting in 7 bits stored inside one byte. The layout is deliberate: A is 65 and a is 97, exactly 32 apart, so flipping one bit changes case. The digit characters start at 48, so subtracting 48 from a digit's code gives the number it stands for.

Text is stored as numbers; the number-to-glyph map is the encoding.characterASCII codeone byte of binaryH7201001000i10501101001!3300100001A = 65a = 970 = 48ASCII covers 128 characters in 7 bits. Unicode (UTF-8) extends this to every writing systemusing 1–4 bytes per character, while keeping the first 128 codes identical to ASCII.
Fig 4Text stored as numbers, then as bits.

ASCII covers English and nothing else, which is why Unicode exists. Unicode assigns a code point to every character in every writing system, plus emoji. UTF-8 encodes those code points using one to four bytes each, and its first 128 values are deliberately identical to ASCII, so every ASCII file is already valid UTF-8. Images work by the same idea in two dimensions: a grid of pixels, each pixel storing a colour as a number, with colour depth deciding how many bits each pixel gets. Sound is the same idea in one dimension: an analog waveform sampled thousands of times a second, each sample stored as a number.

Divide by 8 for bytes.
Data typeWhat is actually storedWhat drives the size
Whole numberA fixed-width binary patternHow many bits the type reserves
Text (ASCII)One byte per characterNumber of characters
Text (UTF-8)1–4 bytes per characterWhich characters are used
Bitmap imageOne colour number per pixelWidth × height × colour depth
Audio (PCM)One number per sample per channelSample rate × bit depth × channels × seconds

Where this shows up in Java

This is not background trivia — it is the reason Java has more than one kind of whole number. Every primitive type is a promise about exactly how many bits the variable gets, and a fixed number of bits can only hold a fixed number of distinct values. Java's integer types are signed, which means one bit's worth of the range is given over to negatives, so an n-bit signed type runs from to .

Java typeBitsRangeUse it when
byte8−128 to 127Raw file or network data
short16−32 768 to 32 767Rarely; memory-tight arrays
int32−2 147 483 648 to 2 147 483 647The default choice for whole numbers
long64about ±9.2 × 10¹⁸Counts of milliseconds, very large totals
char160 to 65 535 (a UTF-16 code unit)A single character
boolean1 logicallytrue or falseA yes/no flag
int big = 2147483647;        // this is Integer.MAX_VALUE
System.out.println(big + 1); // prints -2147483648, NOT 2147483648

long safe = 2147483647L;     // the L makes it a long literal
System.out.println(safe + 1);// prints 2147483648

char letter = 'A';
int code = letter;           // 65 - a char really is a number
System.out.println((char)(letter + 32)); // prints a
Integer overflow, and why a char can do arithmetic.
What overflow actually is
There are only 32 bits, so there is no room to carry past the top one. Adding 1 to the largest int makes every bit roll over and the sign bit flip, producing the most negative value instead. Java does not warn you and does not crash — it silently gives a wrong answer. Storing the result in a long, or choosing long in the first place, is the fix.

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
Convert to decimal and then to hexadecimal.
Example 2
Convert to 8-bit binary using the division method, then verify the answer.
Example 3
A Java program stores the population of Earth (about 8 100 000 000) in an int. Explain what goes wrong and state the range that makes it go wrong.
Example 4
An uncompressed screenshot is 1920 × 1080 pixels at 24-bit colour depth. Find its size in megabytes, taking 1 MB = 1 000 000 bytes.

Where marks get lost

  • Reading division-method remainders top-to-bottom, which silently reverses the entire binary answer.
  • Splitting a binary number into nibbles from the LEFT. Always group in fours starting from the right and pad the left with zeros.
  • Writing and then claiming the largest 8-bit unsigned value is 256. It is 255, because counting starts at zero.
  • Assuming a Java int can hold any whole number. It is 32 bits, so anything beyond 2 147 483 647 wraps around without an error message.
  • Mixing up bits and bytes when a file size meets a connection speed — the factor of 8 makes the answer wrong by 800%.
  • Treating hex letters as variables. In 0xB6, the B is the digit 11, not an unknown.

Flashcards 10

Why do computers use binary?
Binary place values across one byte
How many values fit in n bits?
Why is hexadecimal base 16?
+ 6 more

Practice

1
Convert to decimal.
2
Convert to hexadecimal (two digits, no prefix).
3
Convert to decimal.
4
In Java, int big = 2147483647; System.out.println(big + 1); prints what?
5
What is the largest value a Java short can hold?
6
How many bytes do the elements of int[] scores = new int[1000]; occupy?
bytes
7
A 1 TB drive is reported by the operating system as 931 GB. What is the explanation?
8
The ASCII code for 'M' is 77. What is the ASCII code for 'm'?
9
What is the name of the character encoding that uses one to four bytes per character and whose first 128 values are identical to ASCII?
10
Order these values from smallest to largest.
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
11
Match each Java primitive type to its width in bits.
byte
short
int
long
12
An uncompressed image is 640 × 480 at 8-bit colour depth. How many bytes is it?

Spot a mistake on this page? Let us know