Information Representation
Binary and decimal prefixes, number bases, BCD, one's and two's complement with overflow, character sets, bitmaps, vectors, sound and compression.
WATCH IT EXPLAINED
Kilo is not kibi
A kilobyte is 1000 bytes, but a kibibyte is 1024 bytes, and the gap grows with every bigger unit.
Computers count memory in powers of 2. The SI prefixes (kilo, mega, giga, tera) count in powers of 10. The syllabus uses both.
A kilobyte (kB) is 1000 bytes. A kibibyte (KiB) is 1024 bytes.
| Unit | Power | Bytes |
|---|---|---|
| 1 kB, kilo | 10³ | 1 000 |
| 1 KiB, kibi | 2¹⁰ | 1 024 |
| 1 MB, mega | 10⁶ | 1 000 000 |
| 1 MiB, mebi | 2²⁰ | 1 048 576 |
| 1 GB, giga | 10⁹ | 1 000 000 000 |
| 1 GiB, gibi | 2³⁰ | 1 073 741 824 |
| 1 TB, tera | 10¹² | 1 000 000 000 000 |
| 1 TiB, tebi | 2⁴⁰ | 1 099 511 627 776 |
The binary unit is bigger at every step: by 2.4%, then 4.9%, then 7.4%, then 10.0%.
Example:A drive sold as 500 GB holds 500 000 000 000 bytes. Divide by 2³⁰ and you get about 465.66 GiB. No bytes are missing. They are counted in a different unit.
Binary, denary and hexadecimal: one number, three spellings
Binary, denary and hexadecimal are three ways to write the same number.
- Binary (base 2): the place values of a byte are 128, 64, 32, 16, 8, 4, 2 and 1.
- Denary (base 10): the numbers we use every day.
- Hexadecimal (hex, base 16): the digits are 0 to 9, then A to F for 10 to 15.
One hex digit stands for exactly four bits. A group of four bits is called a nibble.
| To go from | Do this |
|---|---|
| binary to denary | add the place values of the 1s |
| binary to hex | split the bits into nibbles, starting from the right; write each nibble as one hex digit |
| two hex digits to denary | multiply the first digit by 16, then add the second |
Watch out:Split into nibbles from the RIGHT, not from the left.
binary to denary:
add the place values of the 1s
0110 1001 = 64 + 32 + 8 + 1 = 105
binary to hex:
split into nibbles from the right
0110 | 1001 = 6 | 9 = 69 in hex
(9618 assembly writes it &69)
hex to denary:
first digit × 16, plus the second
6 × 16 + 9 = 105Where hexadecimal is used
Hex is a short way for people to write long binary values; the computer still stores binary.
People use hexadecimal wherever they must read or type long binary values. One hex digit replaces four bits, so hex is a quarter of the length of the binary.
Why people like it:
- It is easier to read and remember.
- People copy it with fewer mistakes.
- It is easy to turn back into binary, one digit at a time.
| Where | Example |
|---|---|
| MAC address | 48 bits = 12 hex digits: 00-1A-2B-3C-4D-5E |
| colour code | #FF8000 = red 255, green 128, blue 0 |
| memory dumps | addresses and contents, when debugging |
| error codes | short codes a user can read out |
| assembly code | &4A is the hex number 4A = 74 |
| IPv6 address | 2001:db8::1 |
Watch out:Hex is a shorthand for people only. Writing a value in hex does not save any memory.
BCD, and why a clock uses it
BCD stores each denary digit in its own 4 bits, so digits stay exact and easy to display.
In binary coded decimal (BCD), every denary digit gets its own 4 bits.
Example:59 in BCD is 0101 1001 (5, then 9). In pure binary, 59 is 0011 1011.
Only the patterns 0000 to 1001 are used, for the digits 0 to 9. The patterns 1010 to 1111 are not BCD digits. So BCD wastes some bit patterns.
BCD is used where digits are shown on a display, or must stay exact:
- Digital clocks and calculators: each nibble (4 bits) drives one digit of the display.
- Money systems: amounts such as 0.10 are stored exactly, digit by digit, with no rounding error.
59 in BCD 0101 1001
(5 = 0101, 9 = 1001)
59 in binary 0011 1011
(32 + 16 + 8 + 2 + 1 = 59)
the time 12:59 in BCD
1 2 5 9
0001 0010 0101 1001
read as plain binary,
the same 16 bits are 4 697Negative numbers: one's complement and two's complement
Two's complement stores negative numbers by giving the leftmost bit the value −128.
One's complement: to make a number negative, invert every bit (0 becomes 1, 1 becomes 0).
- +5 is 0000 0101, so −5 is 1111 1010.
- Its flaw: zero has two patterns, 0000 0000 and 1111 1111.
Two's complement: the leftmost bit is the sign bit. A 1 there means negative. In 8 bits, it is worth −128.
Example:1111 1011 = −128 + 123 = −5.
To negate a two's complement number:
- Invert every bit.
- Add 1.
The range in 8 bits is −128 to +127. Why? The leftmost bit is worth −128, and the other seven bits add up to at most 127.
−5 in one's complement
+5 0000 0101
invert 1111 1010 = −5
−5 in two's complement
+5 0000 0101
invert 1111 1010
add 1 1111 1011 = −5
reading 1111 1011 back
−128 64 32 16 8 4 2 1
1 1 1 1 1 0 1 1
−128 + 64 + 32 + 16 + 8 + 2 + 1 = −5
8-bit ranges
one's complement −127 to +127
two's complement −128 to +127
n bits, two's −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1Adding, subtracting, and overflow
Add binary column by column, subtract by adding a negative, and watch for overflow.
Adding: work column by column, from the right. Write the carry row above the numbers.
- 1 + 1 = 0, carry 1
- 1 + 1 + 1 = 1, carry 1
Subtracting: add the two's complement of the second number. So 8 − 5 is done as 8 + (−5). Throw away any carry out of the leftmost bit.
Overflow happens when the true answer needs more bits than the register has. Then the sign of the result is wrong:
| You add | Result |
|---|---|
| two positive numbers | overflow gives a negative-looking result |
| two negative numbers | overflow gives a positive-looking result |
| a positive and a negative | can never overflow |
Watch out:A carry out is not the same as overflow.
8 − 5 done as 8 + (−5)
carries (1) 1111 0000
8 0000 1000
+ −5 1111 1011
= (1) 0000 0011 = 3
the carry out of bit 7 is
thrown away: no overflow
100 + 50 in 8-bit two's complement
carries (0) 1100 0000
100 0110 0100
+ 50 0011 0010
= 1001 0110 = −106
two positives gave a negative: overflow
the true answer 150 is more than +127Characters are numbers too
Text is stored as numbers: a character set gives every character its own binary code.
A character set gives each character a unique binary code. Text is stored as those codes.
| Set | Size | What it covers |
|---|---|---|
| ASCII | 7 bits, 128 codes | English letters, digits, punctuation, control codes |
| Extended ASCII | 8 bits, 256 codes | the first 128 are ASCII; the other 128 differ between versions, for different languages |
| Unicode | up to 4 bytes per character | almost every writing system, such as Mongolian Cyrillic Ө and Ү, and emoji; its first 128 codes match ASCII |
Codes run in order: 'A' is 65, so 'B' is 66.
| Character | Denary | Hex | Binary |
|---|---|---|---|
| A | 65 | 41 | 0100 0001 |
| B | 66 | 42 | 0100 0010 |
| a | 97 | 61 | 0110 0001 |
| 0 | 48 | 30 | 0011 0000 |
| Ө | 1256 | 4E8 | 0000 0100 1110 1000 |
Remember:No need to learn the codes by heart. Use the order: 'a' − 'A' = 97 − 65 = 32.
How a bitmap is stored
A bitmap is a grid of pixels, and each pixel's colour is stored as a binary number.
A bitmap image is a grid of pixels. A pixel is the smallest element of the picture. Each pixel's colour is stored in binary.
Colour depth (bit depth) is the number of bits per pixel. With n bits there are 2ⁿ colours:
| Colour depth | Colours |
|---|---|
| 1 bit | 2 |
| 8 bits | 256 |
| 24 bits | 16 777 216 |
Two kinds of resolution:
- Image resolution belongs to the image: the number of pixels, width × height.
- Screen resolution belongs to the screen: how many pixels it can show, or how many per inch (dpi).
The file header comes first in the file. It holds data such as the width, height, colour depth, file type and compression. With it, software can read the pixel data that follows.
an 8 × 8 smiley at 1 bit per pixel
(0 = white, 1 = black), one row per line:
00111100
01000010
10100101
10000001
10100101
10011001
01000010
00111100
pixel data: 8 × 8 × 1 = 64 bitsBitmap file size, and what changing it does
Bitmap file size = width × height × colour depth, in bits; a smaller file costs quality.
Exam:File size in bits = width × height × colour depth.
Then change the unit. The question says which one.
- Divide by 8 to get bytes.
- Divide by 1000 for kB, or by 1024 for KiB.
What happens when you change the image:
| Change | File size | Image |
|---|---|---|
| double the width AND the height | four times as big: four times as many pixels | bigger |
| lower colour depth | smaller | fewer colours, so it looks less like the original |
| lower resolution | smaller | blocky (pixelated) when it is enlarged |
Watch out:The answer is an estimate. The real file also holds the header, and it may be compressed.
1920 × 1080 pixels, colour depth 24 bits
1920 × 1080 × 24 = 49 766 400 bits
÷ 8 = 6 220 800 bytes
÷ 1000 ÷ 1000 ≈ 6.22 MB
÷ 1024 ÷ 1024 ≈ 5.93 MiB
half the width and the height:
960 × 540 × 24 = 12 441 600 bits
colour depth 8 instead of 24:
1920 × 1080 × 8 = 16 588 800 bitsVector graphics: a list of drawing instructions
A vector graphic is a list of shapes, not pixels, so it can grow to any size and stay sharp.
A vector graphic is stored as a drawing list: a list of drawing objects, such as lines, circles, rectangles and text.
Each object has properties: its coordinates, size, line colour, line width and fill colour.
To show the image, the computer works through the list. It draws every object at the size needed.
| Vector | Bitmap | |
|---|---|---|
| Scaling | any size, with no loss of quality | blocky when enlarged |
| File size depends on | the number of objects, not the size it is shown at | the number of pixels |
| Good for | images made of shapes, such as logos | photographs |
Watch out:A vector is not suitable for photographs. A photo would need a huge number of objects, so use a bitmap.
circle centre (50,50) radius 30 fill yellow line black width 2
rect corner (20,80) width 60 height 10 fill blue
line (35,40) to (45,40) line black width 3
the drawing list, stored as text:
154 bytes
the same picture as a 32 × 32 bitmap
at 24 bits:
32 × 32 × 24 bits = 24 576 bits
= 3 072 bytesSound: sampling an analogue wave
To store sound, the computer measures the wave many times a second and stores each measurement in binary.
Sound is an analogue wave: it varies continuously. To store it as digital data, an analogue-to-digital converter (ADC) samples it:
- It measures the amplitude (height) of the wave at regular intervals.
- It stores each measurement as a binary number.
| Term | Meaning | Example |
|---|---|---|
| Sampling rate | the number of samples taken per second, in hertz (Hz) | 44 100 Hz = 44 100 samples a second |
| Sampling resolution | the number of bits used for each sample | 16 bits give 65 536 levels |
In general, raising either one makes the recording more accurate (closer to the original sound). It also makes the file bigger.
Exam:File size = sampling rate × sampling resolution × seconds × channels.
a 180-second stereo track:
44 100 samples a second, 16 bits,
2 channels
44 100 × 16 × 2 × 180 s
= 254 016 000 bits
÷ 8 = 31 752 000 bytes
≈ 31.75 MB (or about 30.28 MiB)Compression: lossless, lossy, and RLE
Compression makes files smaller; lossless keeps the original exactly, lossy throws some data away for good.
Compression makes a file smaller. It takes less storage space, and it is sent faster, using less bandwidth.
| Lossless | Lossy | |
|---|---|---|
| The original | can be rebuilt exactly | some data is removed for good |
| What is removed | nothing | data people are unlikely to notice: fine detail in a photo (JPEG) or in music (MP3) |
| File size | smaller | much smaller |
| Needed for | text and program files, because one changed character can change the meaning | photos and music |
Run-length encoding (RLE) is lossless. It stores each run of identical values as a count and the value.
Example:AAAAAABBBCCCCD becomes 6A 3B 4C 1D.
RLE: 1 byte per character,
2 bytes per run
AAAAAABBBCCCCD → 6A 3B 4C 1D
14 → 8 bytes
WWWWWWBBBBWWWWWW → 6W 4B 6W
16 → 6 bytes
ABCD → 1A 1B 1C 1D
4 → 8 bytes, bigger
text lossless: RLE, or replace
repeated words
bitmap lossless: RLE
lossy: lower colour depth or
resolution, JPEG
vector lossless, like text: it is
text and numbers
sound lossy: MP3, lower sampling
rate or resolution
lossless: FLACKEY TERMS
CHECK YOURSELF
1.In 8-bit two's complement, what is −5?
2.What is the range of an 8-bit two's complement number?
3.Which image type stays sharp at any size?
4.How many bytes are in 1 KiB?
5.The byte 0101 1001 is stored in BCD. Which denary number is it?
6.In 8-bit two's complement, 0000 1000 + 1111 1011 gives a carry out of the leftmost bit. What does that tell you?