Wednesday, January 22, 2014

A Little Coding

It has been a while since I have done any real computer programming. Sure I work on a computer all day and spend a lot of time writing SQL statements but I don't really count SQL as a programming language. I figure I have been getting a little rusty and so I have taken it upon myself to do a little coding every evening when I get back to the boat. When I am at home in Utah, there is too much to do each evening. When I am on the boat in California, there is nothing to do but watch television. I figure it is a good time to refresh my programming skills.

It is best to start with what I already know and so I have a simple example C program that converts IP addresses into integers. An IP address is simply 4 numbers separated by a dot. My IP address is something like "101.102.103.104". This can be converted into an integer simply by breaking the numbers up and thinking of them in binary format (that will be in 1's and 0's). If you take 101 as a decimal number and shift it to the left by 24 bits, add it to 102 shifted to the left by 16 bits, add it to 103 shifted to the left by 8 bits, and add it to 104, you are left with an unsigned 32-bit integer that is the equivalent of your IP address. This is a number between 0 and 4,294,967,295.

Now the question you need to ask is why? Isn't it easier to remember 101.102.103.104 than some seemingly random integer? Yes, but it also takes more space. The IP address requires up to 16 bytes when left as a string. The unsigned integer only requires 4 bytes.  With computers that have gigabytes of memory, does 12 bytes really matter? Yes, when you are trying to remember 90 million IP addresses. 4 times 90 million is only 360 megabytes of memory where 16 times 90 million is well over a gigabyte.

My little program is working in the C programming language because that is what I am most familiar with. Now my goal is to rewrite it in a number of other languages so I can become familiar with them. When I am finished, I want to compare execution times and see which is fastest. I also want to see which one is easiest to understand, easiest to code, and fewest number of lines. It may sound like a boring exercise but my hope is that it will help improve my coding skills at the same time I learn about a number of new languages. It also has a practical application to a problem I am trying to solve at work and so that makes it significantly more interesting.

No comments:

Post a Comment