1.2 - Memory and Storage (Part 3)
Binary (Base-2) and Denary (Base-10)
- In Denary, the number system you're likely to be used to,
each digit has a weight factor of 10 raised to a power,
so the units column is 100, the next is 101,
and so on.
- Because the system is base 10, there are 10 different digits in use:
0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. In Base 2, there is only 0 and 1 to
work with.
- In Binary, each digit has a weight factor of 2 raised to a power,
like 20 and 21.
Here's how to convert binary to denary, using 010101100
as an example:
Power | 2^8 | 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
Denary | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Binary | 0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 |
Binary to Denary | 0 | 128 | 0 | 32 | 0 | 8 | 4 | 0 | 0 |
This means that the final calculation for this number would be 128 + 32 + 8 + 4 = 172
To convert the other way round (from Denary to binary) you simply need to
subtract powers of 2 from your number. For example (using the same 172):
Power | 2^8 | 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
Denary | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
172-256. Is 256 smaller than 172?: no, add a 0 (number so far: 0).
172-128. Is 128 smaller than 172?: yes! Add a 1 (number so far: 01)
Since 128 was subtracted, you now have 44.
44-64. Is 64 smaller than 44?: no, add a 0 to the number (010).
44-32. Is 32 smaller than 44?: yes! Add a 1 and subtract (remainder 12) (0101).
12-16. Is 16 smaller than 12?: no, add a 0 to the number (01010).
12-8. Is 8 smaller than 12?: yes, add a 1 and subtract (remainder 4) (010101).
4-4. Is 4 smaller than 4?: no, but they are equivalent. Add a 1 (0101011)
and now populate the remaining columns (2^1 and 2^0) with 0s.
Final answer: 010101100
(the same as above).
Binary Addition
owie brain hurts (reference irrelevant)
Like when adding denary, you add to the rightmost column and move left.
For example, adding 1000 and 0110:
8 | 4 | 2 | 1 |
1 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 1 | 1 | 0 |
If a question is too complicated, you can convert from binary to denary, add, and then go back, but this introduces more chances for error.
Denary to Hexadecimal
- The hexadecimal system is made up of 16 digits, 0-9 and A-F
(A is 11, B is 12, and so on, up to F as 15).
- You may already be familiar with it as it is commonly used for things
like memory addresses and error codes, as it's easy to read
and convert from one base to another, particulary to binary.
- It is usually prefixed with '0x', and is also known as Base-16.
- Each digit has a weight factor of 16 raised to a power,
so the rightmost digit is ones (160), but the next digit to
the left is the 16s column (161).
- Hexadecimal can store four binary digits in one character,
so one nibble of space is required to store each digit.
Since at GCSE you only need to work with 2 digit hex
values, the easiest method convert from denary is to divide by 16.
The amount of times the digit goes in to the number is your first
column (remember if you get a number 10 or over that can still correspond to a hex digit)
and the remainder is the units column.
For example, 170/16 = 10 (A) remainder 10 (A),
so denary 170 = hex AA.
Binary to Hexadecimal
To convert binary to hexadecimal, first split the number into two binary nibbles.
For example, 11010110
into 1101
and 0110
.
Then, convert each number to its denary value (1*8+1*4+1*1 = 13 and 1*4+1*2 = 6).
Afterwards, convert those numbers into their hex digits (so D and 6), getting you D6.
Binary Shifts
- Binary shifts are how computers perform multiplication and division.
- Binary digits are moved left or right a set number of time.
- A shift to the left multiplies a number by 2, and a shift to the right divides.
- A shift can move by more than one place at a time, a shift of 2 multiples by 4 (*2 for each place)
- For example, to multiply 38 (100110) by 2, you would perform a left shift of 2,
giving you 1001100
, or 76.
- Depending on the amount of bits used to represent the number, an overflow error may occur.
This is when too many bits are going to be used to represent a number.
- For example, if during a binary shift you were only using 6 bits for 38,
and you can't use 7 which is what you need for 76, that would cause an overflow error.
- This can cause unpredictable behaviour including crashing, shutdowns or rollovers where
the system flips back around to the smallest possible number than can be represented in an
attempt to add to a number.