Converting denary to binary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Working out the value of a binary number is straight forward. Going the other way, however, is more difficult. It depends on the size of the denary number will determine how best to do it. First of all we will look at a formal method of doing this. Then we shall look at a quicker way of doing it which works well on small numbers but can be too difficult to do on much larger ones. The algorithm used to convert numbers into binary is -
Lets consider the example of converting the number 4321 The result is built up from the left to the right. So the first division by two represents the left most binary digit (in the 1’s column). To double check the answer we can convert it back into denary.
4096 + 128 + 64 + 32 = 4320 At the end we had the same result which is exactly what should of happened. Let us consider a simpler example, that of 163. Converting back (to check our answer) we get.
128 + 32 + 2 + 1 = 163 Looking at a simpler example, convert 21. The above diagram shows exactly where each of the bits end up in the final answer of 10101. The remainder of the first division will end up being the right most digit.
This process can be done faster on small numbers like this. The process for this faster way is
As 2^N is fairly easy to remember (1,2,4,8,16,32,64) just double the last value to get the next one) this method can be done swiftly. Lets work through the example. Initially we will have nothing written down. The largest value which will fit is 128. 256 does not fit as 128 - 256 is less than zero. As such I put a 1 under the 128 column. 163 -128 = 35
I then move on. 64 can not be used as 35 - 64 < 0. However 32 can be used. As we could not use 64 we place a zero under its column. 35 - 32 = 3
Now we just keep repeating. 16, 8 and 4 will not fit so get replaced with zeros. 2 does fit so we write a one under its column and subtract.
3 - 2 = 1
And finally we add the one which leaves us with the answer -
|