In this instructable you will convert a number from decimal to binary and binary to grey and hex.
The example number that is going to be used in this instructable is 385 (in base 10).
There is some general knowledge that you will need to know to complete this instructable which you may or may not know.
If you already know how number systems work then you can skip this section. Number systems work as such, you have a base n from which numbers from 0 to n-1 are position holders. The place to the farthest right is always the one's place or the n^0th place. As you travel to the left you increase the value of the place by one power. Other than our normal decimal (base 10) system the other most prevalent system is the base 60 that is used for seconds and minutes. The seconds go from 0 to 59 then on the next tick it goes to 1 min 0 sec and then begins counting again from 0 to 59. This same idea can be used for any base system, for example octal (base 8) counts 0 1 2 3 4 5 6 7 10 11 12... and hexadecimal (base 16) counts 1 2 3 4 5 6 7 8 9 A B C D E F 10 11... Since we have only created places for 0 through 9 we use A B C D E and F as placeholders for 10 11 12 13 14 and 15 respectively.
This section goes over division and modulus. The division that I talk about here is different from the division that most people are taught in their elementary classes. The division that I talk about is simply the whole number quotient, that is without any part remaining. The modulus(%) of a number is the remainder of the quotient after it has been divided by that number. For example:
24/5=4 and 24%5=4, 50/7=7 and 50%7=1
Step 1: Decimal to Binary
Converting from decimal to binary is the most difficult of all the conversions. In order to convert a number from decimal to binary there are two steps that are repeated until the number equals zero.
First you take the modulus 2 of the number write that number down in the 2^0th place. Then you divide the number by two and use this new number to calculate the next position and continue to do these two steps traversing to the left from the 2^0th place to 2^1st place and then 2^2nd place and so on and so forth. When the number reaches zero you are done because 0%2=0 and 0/2=0 so you would have zero's from then on to the left.
Using the number 385 you should have the binary number 110000001.
Step 2: Binary to Hex
If you used the number 385 for the last step then you should have the binary number 110000001.
To convert that number into hex you start at the left side and split the number up into four bit chunks like so: 1|1000|0001. Then you convert each chunk of four back into decimal. The first digit is the one's place the second is the two's, third is the four's, and fourth is the eighth's. You then add each number up using decimal for 0-9 and A for 10, B for 11, C for 12, D for 13, E for 14, and F for 15. Do this for each set of four and you have successfully converted your binary number into hex.
The hex conversion of the decimal 385 is 181.
Step 3: Binary to Grey
To convert from binary to grey you take the original binary number (110000001) and right shift by one throwing away the right most number (_11000000). You then do a bitwise xor on the original and right shifted numbers.
In order to do a bitwise xor you line up the two numbers from right to left as you would do when adding two numbers together:
then for each bit you write a one if the two numbers are different and a zero if they are the same carrying through any numbers that do not have a partner:
Therefore the grey ordering of the binary number 110000001 is 101000001.