1.4.1 - Binary, hexadecimal and denary
The bit pattern 00101101 can be written much more compactly as 2D in hexadecimal without changing its value. This part of 1.4.1 develops positive integer representations in binary, hexadecimal and denary, then converts between every pair. You will use place values and bit grouping to explain the results rather than rely on a converter.
Place values depend on the base
A positional number system gives a digit its value from both the digit and its position. In denary, also called decimal, the base is ten: the rightmost whole-number place has weight one, then ten, one hundred and so on. Thus .
Binary has base two and permits only digits 0 and 1. Starting at the right, its whole-number weights are , or 1, 2, 4, 8, 16 and so on. Binary suits hardware with two distinguishable states; it is the agreed interpretation of those states that makes a number.
| Eight-bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Unsigned weight | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Example bit | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
Here 01011010 represents . All integer examples in this lesson are unsigned: no bit is a sign bit.
Eight unsigned bits provide patterns, representing 0 through 255. The number of patterns includes zero, so it is one greater than the largest unsigned value.
Leading zeros do not alter the value, but they matter when a task specifies a fixed width. 101101 and 00101101 both represent 45; only the second is eight bits.
Construct binary from a denary integer
To construct binary, decide which powers of two add to the value. For unsigned eight-bit 173, begin with the largest weight:
| Weight | Decision | Remainder |
|---|---|---|
| 128 | Use it: write 1 | 45 |
| 64 | Too large: write 0 | 45 |
| 32 | Use it: write 1 | 13 |
| 16 | Too large: write 0 | 13 |
| 8 | Use it: write 1 | 5 |
| 4 | Use it: write 1 | 1 |
| 2 | Too large: write 0 | 1 |
| 1 | Use it: write 1 | 0 |
Reading the decisions gives 10101101. Check by adding the selected weights: . The remainder reaching zero shows that the complete value has been accounted for.
Repeated division by two is another valid method. For 45, the divisions give quotient/remainder pairs 22 r1, 11 r0, 5 r1, 2 r1, 1 r0, 0 r1. Each remainder is the next least significant bit, so read the remainders from last to first: 101101. Pad on the left to obtain the requested eight-bit result 00101101. Reading the remainders in the original order reverses the bit positions.
A width limit is a real condition. Unsigned eight-bit 256 cannot be represented: it needs 100000000, nine bits. Removing its first bit would produce zero and change the value. Python integers can grow beyond eight bits, so a successful Python calculation would not demonstrate that a value fits this format.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account