Hexadecimal Demystified I remember dreading having to learn hexadecimal. As it turned out, it didn't take any time at all to get comfortable with it. The dread turned out to be an unjustified worry. Hexadecimal wasn't created just for the heck of it. It has its place in the world of computers where it offers significant advantages over the everyday "base 10", the decimal system, that you are accustomed to. Read through this. Perhaps next time it won't look like something from another world. Hexadecimal: The intent here is to show its advantages and how you, too, can be comfortable with it. You don't need a chart and you only need to remember one thing. That one thing is an easy one. I will show how it relates to model railroading and how you can use it to work for you. First, a review of our everyday number system, base 10. What is $2.34? Two hundred and thirty-four cents. That's two times one hundred cents plus three times ten cents plus four times one cent. You had the 1's column, the 10's column, and the 100's column. This next bit is provided just as information. You need not worry about it past today. So don't get a headache. Binary has the 1's column, the 2's column, 4's, 8's, 16's, 32's, 64's, and 128's to name those repesented in a byte. A byte has 8 bits and is used by computers including those in DCC equipment. It takes eight columns to represent numbers up to 255. Considering those columns only have 1's and 0's, it's a real eye sore. Hexadecimal, commonly referred to as hex, has a 1's column and a 16's column. Hexadecimal's first advantage is that numbers up to 255 can be represented with just two columns. In fact, one hex column can fully represent four binary columns. You can do this without a chart and remembering just one thing! By the way, this ability to represent a byte with two characters is why hex is in use. No mad man thought this up. 0-9 in decimal is 0-9 in hex. Now make a fist and throw out a finger. No, not that one. Put it back. Use your thumb. "A is 10." Now another finger. "B is 11." Now that finger you so badly wanted to use a moment ago. "C is 12." Keep going. "D is 13." You're on a roll. "E is 14." Oops. Out of fingers? Use your nose. "F is 15." As long as you don't have a close encounter with a table saw, you will not need a chart. You can probably run through this in your head faster than you could find a chart. Entering Decoder Options into CVs — aka Binary to Hex Conversion: Many decoders have a variety of options that are controlled by bits. After you have selected the bits, you need to enter them as hex characters. But first, you need to figure out what that hex character needs to be. Look above in the binary section. We have the 1's, 2's, 4's, and 8's columns from right to left. From left to right that's 8421. That's it. That's the only thing you need to remember - 8421. It's easy to remember because each goes down by half. So how do you use this on a byte? Bytes are broken into two nibbles. Really! I'm not making this up! Let's work through one. Suppose you selected options that gave you the byte 10110110. Break it into two nibbles, 1011 and 0110. Using the columns concept introduced at the beginning and the 8421, the first nibble has 1 in each of the 8, 2, and 1's column. 8 and 2 is 10 and 1 is 11. Now throw the fist. "A is 10 and B is 11." Write down B. The second nibble is 4 and 2 is 6. Write down 6. You are done. Enter B6 into the appropriate CV. Here area few more examples. Work through them in your head and see if you get the same answer. I've already broken them into nibbles for you. Grouped together as eight, it is easy to make a mistake. 1110 1100 = EC 1101 0010 = D2 0101 1010 = 5A 0000 1111 = 0F Many manufacturers attempt to do you a favor by sparing you knowing anything about hex. You may see things like, "if you want option a, add 8, otherwise skip to the next option. If you want option B, add 16, otherwise skip to the next option. You may vary the bell ringing speed by adding 32, 64, or 128." This method does spare you knowing hex, but you will need a pencil and paper. It will also take a while to decompose a stored value into their various options. And you will need to read their page long instructions for that one CV. There is nothing wrong with their approach. It just has a small price. If you feel comfortable with what you have read here, you will probably find this more convenient, faster and easier. You have the freedom to choose what works best for you. Figuring Out What Options You Have in Your CVs - aka Hex to Binary Conversion: You are looking at a CV and are wondering what options are in effect. Here's how you go from hex back to bits. Suppose the CV has 6C in it. How do you get 6 using the 8421? Write it down putting 0's for columns you don't use. 6 uses 4 and 2. So 6 is 0110. For the C, throw the fist. "A is 10, B is 11, C is 12. 12 uses 8 and 4. Therefore, 12 is 1100. Here's more practice. BF = 1011 1111 23 = 0010 0011 DA = 1101 1010 E0 = 1110 0000 That's all there is to it! If you can figure change for a quarter in your head, this method should work easily for you. This is how I did it until I finally could remember them. Yes, some of us actually have this memorized. Sick, aren't we? Hex Notation: This is very important. If you see E7, you know it's hex. But what if you see 12? 12 in hex translates to 18 in decimal. So we need to be able to tell a typical 12 from a hex 12. If it has to do with a CV, it's probably a hex 12 unless it's an address. I prefer to use hex notation so you don't have to wonder. I omitted the notation above so it wouldn't get in the way of you learning hexadecimal. But in all my pages you will see hex suffixed with an h. You may also see hex prefixed by 0x in other DCC literature. Look at the 0F above. You don't write $01.33 do you? The 0 is just a place holder. Since hex is usually used with reference to bytes, the whole byte is 0F. In a computer, anything that is not a 1 is a 0, so whether you write it down or not, it's in the computer as a 0. Many people write as many 0's as necessary to represent what is actually in the computer. So a programmer might see 000F. What matters to you is that if you see a leading 0, it is probably hex. Lastly, you may see the hex number prefixed by a 0 if the first hex character is a letter like 0D6. This is because some computer program assemblers require a leading number like a 0 so that the assembler can tell that a number is coming up rather than a variable name. People who have worked with these assemblers will be in the habit of writing a leading 0. These 0's have no consequence to us humans. All the following are ways you might see a hex byte: CF CFh CFH 0xCF 0CFh 0CFH 0E 0Eh 0EH 0xE 0x0E
|
Copyright by Allan Gartner 1996 - 2010 © All rights reserved. You may print this for your own, personal, non-commercial use. Non-commercial, non-personal reproduction may be requested by visiting www.WiringForDCC.com/writeme.htm . All users, commercial and non-commercial, may link only to this site at www.WiringForDCC.com. Thanks to all who contribute to this site and the Q&A forum! |