2.1.1-2.1.3 - Binary states, unsigned and signed integers
Binary is a pattern whose meaning comes from how it is interpreted. In 2.1.1 you will connect binary to data and instructions and count the states possible with a given number of bits; in 2.1.2 you will distinguish unsigned from two's-complement signed integers; and in 2.1.3 you will convert 8-bit values in the ranges 0 to 255 and -128 to +127. By the end, you will be able to explain why the same eight bits can mean two different denary numbers.
Binary patterns and states
Binary is a base-2 representation that uses the digits 0 and 1 to label two distinguishable states. The labels do not always literally mean "off" and "on"; they are symbols that let hardware store and process two reliably different states.
Bit
A bit is a binary digit with one of two possible values: 0 or 1.
Bits are grouped into binary patterns. Computers use these patterns to represent data, including numbers, text, sound and graphics, as well as program instructions. A pattern does not carry its interpretation by itself: the system using it must know the agreed context. For example, 01000001 could be treated as a number, as encoded data, or as part of an instruction.
For a pattern of length n, every bit position has two choices. The maximum number of unique patterns, or states, is therefore:
2 x 2 x ... x 2 = 2^n
Worked example: four bits
2^4 = 2 x 2 x 2 x 2 = 16 states
The states run from 0000 to 1111. There are 16 states even though the largest value is 15 when the patterns are interpreted as unsigned integers. Zero uses one of the states, so the number of states and the largest value are not the same.
n bits provide 2^n unique states. When those patterns represent unsigned integers, the largest value is 2^n - 1.
Unsigned 8-bit integers
Denary is the everyday base-10 number system, and an integer is a whole number. An unsigned representation has no negative values: every bit contributes a positive place value, or contributes zero when that bit is 0.
Unsigned integer
An unsigned integer is a whole number represented using only non-negative place values, so its possible values are zero and positive integers only.
An 8-bit unsigned integer uses these place values:
| Bit position | 7 (MSB) | 6 | 5 | 4 | 3 | 2 | 1 | 0 (LSB) |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
The most significant bit (MSB) is the leftmost bit and has the greatest place value. The least significant bit (LSB) is the rightmost bit and has the smallest place value.
To convert unsigned binary to denary, add the place values wherever the bit is 1.
Worked example: 01011010 to denary
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
64 + 16 + 8 + 2 = 90
So 01011010 represents denary 90 when it is unsigned.
To convert denary to unsigned binary, work from the largest place value to the smallest. Put 1 when the place value can be used, subtract it, and continue with the remainder; otherwise put 0.
Worked example: denary 154 to 8-bit unsigned binary
154 = 128 + 16 + 8 + 2
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
Therefore 154 is 10011010.
Eight bits provide 2^8 = 256 states. Unsigned values begin at zero, so the range is:
- minimum:
00000000 = 0 - maximum:
11111111 = 255
Write all eight bits when an 8-bit answer is requested, including any leading zeros.
Two's-complement signed integers
Sometimes a value can be negative, so an unsigned representation is unsuitable. A signed integer representation can represent negative integers as well as zero and positive integers. Pearson Edexcel uses two's complement for signed binary integers.
Two's-complement signed integer
In an 8-bit two's-complement integer, the MSB has place value -128; the other seven bits keep the positive place values 64 to 1.
| Bit position | 7 (MSB) | 6 | 5 | 4 | 3 | 2 | 1 | 0 (LSB) |
|---|---|---|---|---|---|---|---|---|
| Two's-complement weight | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
The MSB gives a quick indication of the sign:
- MSB
0: the value is zero or positive, from0to+127 - MSB
1: the value is negative, from-128to-1
However, the MSB is not a separate minus symbol followed by a seven-bit magnitude. It is a bit with the weight -128, and it must be included in the calculation.
Worked example: 11010110 to signed denary
| Weight | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
-128 + 64 + 16 + 4 + 2 = -42
So 11010110 represents -42 when interpreted as 8-bit two's complement.
For a non-negative signed value, the MSB is 0 and the remaining place values work exactly as they do for unsigned binary. For example, 00101101 = 32 + 8 + 4 + 1 = +45.
The boundary patterns establish the 8-bit signed range:
- minimum:
10000000 = -128 - maximum:
01111111 = +127
For 8-bit two's complement, read the place values as -128, 64, 32, 16, 8, 4, 2, 1.
Encoding negative integers
The negative place-value method also converts a negative denary integer into 8-bit two's complement. Start by using the -128 place, then find which positive places are needed to reach the target.
For a target value x between -128 and -1:
- Put
1in the-128position. - Calculate the positive offset:
x - (-128), which is the same asx + 128. - Represent that offset using the remaining seven place values
64, 32, 16, 8, 4, 2, 1. - Verify the final pattern by adding its signed weights.
Worked example: denary -53 to 8-bit two's complement
First find how far -53 is above -128:
-53 - (-128) = 75
Now represent the positive offset:
75 = 64 + 8 + 2 + 1
| Weight | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 |
The pattern is 11001011. Verify it:
-128 + 64 + 8 + 2 + 1 = -53
With less scaffolding, -96 is 32 above -128, so only the -128 and 32 positions are 1: 10100000. Checking gives -128 + 32 = -96.
Choosing the interpretation
The stored bits do not announce whether they are signed or unsigned. That interpretation comes from the data's agreed type or context, so the same 8-bit pattern can have two different denary values.
| 8-bit pattern | Unsigned interpretation | Two's-complement interpretation |
|---|---|---|
00000000 | 0 | 0 |
01111111 | 127 | +127 |
10000000 | 128 | -128 |
10110110 | 182 | -74 |
11111111 | 255 | -1 |
Choose unsigned when the possible values cannot be negative and the extra positive range is useful. An 8-bit counter required to store 0 to 200, for example, fits unsigned 0 to 255 but not signed -128 to +127.
Choose signed two's complement when negative values are needed and the complete required range fits -128 to +127. The choice depends on every possible value, not just the value currently stored.
Eight bits always provide 256 patterns. Unsigned uses them for 0 to 255; two's complement uses them for -128 to +127.