# Dustlayer

Retro means old but cool.

I grew up with the Commodore C64 but was never able to master the machine. I was young, I wanted to play the latest games and let other people do the pioneer work on exploring this incredible hardware. Today I have better skills to catch up on what it takes to code the C64. I will share what I learn along the way. Enjoy the trip to the past!

# Math Basics Part 2 - Calculating in Three Systems

April 19, 2013 / actraiser

- Math Basics Part 1 - Know your Numbering Systems
- Math Basics Part 2 - Calculating in three systems
- Math Basics Part 3 - Bit Manipulation

### Losing the anxiety towards conversion Math

After the Introduction to Numbering Systems we want to dive deeper into how to convert numbers between systems. Luckily as a C64 programmer we are usually concerned about small numbers in the range of 0-65535 or $0000-$FFFF. This helps us a lot to minimize the Math we need to know.

After this article you will be comfortable to do the following:

- Binary to Decimal conversion and vice-versa
- Hex to Decimal conversion and vice-versa
- Binary to Hex conversion and vice-versa

### Binary and Decimal Conversions

Honestly, I don't know any real-life scenario where Binary to Decimal or Decimal to Binary conversions are required for a Machine Language programmer. However, BASIC programmers on the other hand may find it useful. For everyone else it is just cool to be able to quickly convert between the two systems without a calculator nevertheless. ​

We start with Converting from Binary to Decimal - the example was already briefly brought up in the first part of the Basic Math article. We have a Binary 8-Bit number %00110101. The Binary System is Base 2 which means that with every position starting on the right of the number the value needs to be multiplied by 2 to the power of the current position. The very first position in a number is considered to be the Zero-Position from the Multiplier perspective. By the way, any Number to the power of Zero results to One.

Especially for somebody familiar with computers it is easy to memorize all multipliers for an 8-Bit-Number since you encounter them all the time, for example when talking about system memory. When I give you a wakeup call at 3am you should be able to instantly enumerate those: 1,2,4,8,16,32,64,128 - practice!

From here it is straight forward. If a Bit is set we use the Decimal value of the multiplier and add everything up until we have the final Decimal number representing this Binary. It should be easy to follow by looking at graphics again where we converted from %00110101 to Decimal 53. ​

​Converting a Binary to a Decimal number

To convert back from Decimal to Binary format is easy too but you may need to use a piece of paper to do the calculation. We take the result 53 from the calculation before. Now how do we get back to Binary?

You start by dividing the Decimal number by Two. If there is a remainder, the Binary Digit is supposed to be set, if there is no remainer the Binary Digit is Zero. Repeat this process until you cannot divide by Two anymore. Build the resulting Binary number by writing down alls remainders starting with the last one..

​Converting 53 to Binary notation

No surprise, the result is %00110101 - we pad the number to be compliant with the 8-Bit notation but whether you would write %00110101 or %110101 does not matter to the actual value.

### Hex and Decimal Conversions

Again, in the daily coding routine you don't often convert between Hex or Binary and the Decimal numbering system, yet it is useful to know how it works. We will look at the example from the other Math article again to show the conversion process from a Hexadecimal number to Decimal.

​A conversion from Hex to Decimal

The process to convert Hex to Decimal is exactly like with Binary to Decimal. The only difference is that with Binary numbers it is easier to use the multipliers. Since a digit in a Binary Number is either Zero or One you don't have to take a number of times into consideration that you would have to multiply against the Base value of each position. That means that there is a little extra step in Hex sind each position can have a value from 0 to 15 or $00-$0F. In the case of our Hex Number $314 - which you should have encountered in our Episode on Interrupts - the calculation is like outlined in the image above which should be self-explanatory.

Let's again take turns and convert the Decimal Number 788 back to Hex! ​Big surprise - it is also as simple as dividing the Number by the Base which is 16 of course and work with the Remainder exactly like when we did Decimal to Binary conversion.

Converting Decimal Number 788 to Hex ​

​Again we write doen the resulting Remainders starting from the last position and get as expected our Hex number $314 which we pad for convenience to $0314 which is the common way of writing 16-Bit wide memory locations. As you can see converting bigger Decimal numbers to Hex is not as straight forward as with Decimal to Binary where we just have to divide by Two all the time.

### Hexadecimal and Binary Conversions

This couple is actually very important because you will convert all the time between the two. The reason will become obvious a bit later when we talk about Bit manipulation. Fortunately we can use a characteristic relationship of the two numbering systems.

A Byte consists of 8 Bits. If you split a Byte in half - you have two times 4 Bits, also called two Nibbles. One isolated Nibble can hold values from 0-15 or in other words 16 combination. This is exactly the range of digits a Hex number uses of course. ​Could it really be that we just need to split any Hex number into it's individual 4-bit Binary equivalents and write those next to each other? Is it that easy? Yes it is!

​Conversion from Hex to Binary is very easy.

$D012 converted to Binary results to %1101000000010010 ​- if this was not easy I don't know what is!

This is a skill set you can use to gain some credibility in your office or at school. Converting between two non-Base 10 systems back and forth with ease! The conversion back from Binary to Hex is a no-brainer. Basically rewind the diagram above. ​​

-act

1 Likes