Data Representation, Logic Gates and the Role of the Operating System
IB Computer Science · Computer Organization
Why binary. Digital circuits distinguish two states reliably, so all data is encoded as bit patterns. The same pattern means different things depending on how it is interpreted, which is why questions always state the representation being used.
Unsigned and hexadecimal. In an 8-bit unsigned integer, the bit weights are 128, 64, 32, 16, 8, 4, 2 and 1, so the range is 0 to 255. Hexadecimal is used because each hex digit corresponds exactly to four bits, so a byte is always two hex digits; this makes long bit patterns readable without changing their meaning.
Two's complement. Signed integers use two's complement, in which the most significant bit carries a negative weight. In eight bits the weights become minus 128, then 64, 32, 16, 8, 4, 2, 1, giving the range minus 128 to plus 127 — note the asymmetry, and that there is only one representation of zero. To negate a number: invert every bit, then add one. The reason this representation is used is that ordinary binary addition then works for negative numbers with no special case, so the same adder circuit handles subtraction. Overflow occurs when the result will not fit: adding two positives and obtaining a negative, or adding two negatives and obtaining a positive.
Text, images and sound. Characters are encoded by ASCII, which uses seven or eight bits and cannot represent most of the world's writing systems, or by Unicode, which assigns a code point to every character in use and is usually stored in the variable-length UTF-8 encoding. An image is a grid of pixels; its size in bits is width times height times colour depth. Sound is captured by sampling: the size in bits is sample rate times bit depth times the number of channels times duration. Raising resolution, colour depth, sample rate or bit depth improves fidelity and increases file size proportionally, which is the trade-off every question in this area is testing.
The operating system. The operating system is the layer between hardware and applications. It manages resources — scheduling the processor between processes, allocating memory, controlling peripherals through device drivers, and organising secondary storage as a file system — and it provides a consistent interface so that an application does not need to know which particular hardware it is running on. That abstraction is what allows the same program to run on machines with different components.
Worked example: Two's complement: converting back and forth and detecting overflow
Use 8-bit two's complement representation. (a) Represent the number negative 45. (b) Verify the result by recalculating the value of the bit pattern obtained. (c) Convert that bit pattern to hexadecimal. (d) Calculate 100 plus 45 in 8 bits and explain the result.
Solution
(a) Representing negative 45. Step 1: write 45 as 8-bit unsigned binary. Since 45 equals 32 plus 8 plus 4 plus 1, we get 0010 1101. Step 2: invert every bit, giving 1101 0010. Step 3: add one, giving 1101 0011.
(b) Verification. This step is mandatory and it also earns marks, because it shows the student understands rather than just memorizing the procedure. In 8-bit two's complement, the weights are negative 128, 64, 32, 16, 8, 4, 2, 1. The bit pattern 1101 0011 has bits set at the weights negative 128, 64, 16, 2 and 1. Adding up: negative 128 plus 64 plus 16 plus 2 plus 1 equals negative 128 plus 83 equals negative 45. Correct, as required.
(c) To hexadecimal. Split into two groups of four bits: 1101 and 0011. The first group equals 8 plus 4 plus 1 equals 13, which is the digit D. The second group equals 2 plus 1 equals 3. So the result is D3. Note that this splitting is always valid because each hexadecimal digit corresponds to exactly four bits, and it does not require knowing whether the bit pattern represents a positive or negative number.
(d) Calculating 100 plus 45. The number 100 is 0110 0100 and the number 45 is 0010 1101. Binary addition gives 1001 0001. Interpreting this pattern as two's complement: negative 128 plus 16 plus 1 equals negative 111. Adding two positive numbers and getting a negative number — this is exactly the sign of overflow. The cause is that the true result, 145, exceeds the maximum value representable in 8-bit two's complement, which is 127.
The overflow detection rule to remember. In two's complement, overflow occurs if and only if the two operands have the SAME SIGN and the result has the opposite sign: positive plus positive giving negative, or negative plus negative giving positive. Adding two numbers of different signs never overflows, because the result always lies between the two operands. Note that a carry out of the highest bit is NOT a sign of overflow in two's complement — it is the sign of overflow for unsigned numbers, and confusing the two is a common mistake.
Worked example: Calculating image and audio file sizes, then reading the meaning of the trade-off
(a) An image of 1,920 by 1,080 pixels, color depth 24 bits, uncompressed. Calculate its size in MB. (b) A stereo audio clip 3 minutes long, sampling rate 44,100 Hz, depth 16 bits. Calculate its size in MB. (c) Explain why lossy compression is acceptable for these two kinds of data but not acceptable for program text files.
Convention: 1 MB equals 1,000,000 bytes.
Solution
(a) Image. The number of pixels equals 1,920 times 1,080 equals 2,073,600. Each pixel needs 24 bits, that is 3 bytes. The size equals 2,073,600 times 3 equals 6,220,800 bytes, about 6.22 MB. The meaning of 24-bit depth: 8 bits for each of the red, green and blue channels, giving 2 to the power of 24, about 16.7 million colors.
(b) Audio. Each second has 44,100 samples per channel; with two channels that is 88,200 samples. Each sample is 16 bits, that is 2 bytes, so each second needs 88,200 times 2 equals 176,400 bytes. For 3 minutes equals 180 seconds: 176,400 times 180 equals 31,752,000 bytes, about 31.75 MB.
A remark worth making about the two figures. Both are much larger than the file sizes users usually encounter, and that difference is exactly what compression produces. This is why compression is not a side feature but the condition that makes storing and transmitting multimedia data feasible.
(c) Why lossy compression is acceptable for images and audio. The final recipient of these two kinds of data is human senses, which have limits. The ear cannot hear sounds masked by a louder sound at the same moment, and cannot hear frequencies outside the audible range; the eye is far more sensitive to changes in brightness than to changes in color. Lossy compression exploits exactly those limits: it removes information that the user does not notice is missing, so the compression ratio achieved is many times higher than with lossless compression.
Why it is not acceptable for source code or executable files. Here the recipient is a compiler or a processor, and it requires every bit to be exact. One wrong bit in source code makes the program fail to compile or run incorrectly; one wrong bit in an executable can corrupt the whole program. There is no concept of "approximately right" here, so LOSSLESS compression must be used, which works by removing redundancy that can be reconstructed exactly — for example replacing repeated strings with a reference to an earlier occurrence.
The general sentence for answering every question about compression. Choose the type of compression according to the final recipient: if it is human senses then lossy compression is reasonable; if it is a machine requiring bit-for-bit accuracy then lossless compression must be used.
Common mistake
Trap 1 — confusing the overflow sign for signed numbers with that for unsigned numbers. In two's complement, overflow occurs when the two operands have the SAME SIGN and the result has the opposite sign. A carry out of the highest bit is the sign of overflow for UNSIGNED numbers, not for two's complement.Trap 2 — mixing up XOR and OR. The two gates differ in exactly one cell: when both inputs are 1, XOR gives 0 and OR gives 1. Remember it by the sentence "XOR gives 1 when the two inputs are DIFFERENT".
Trap 3 — forgetting to multiply by the number of channels when calculating audio file size, or forgetting to divide by 8 when converting bits to bytes. Write out the full formula sampling rate times bit depth times number of channels times duration, then divide by 8, and always state the unit at each step.