Should I really care about internal working of a computer?

Well, the answer is it depends. Computer science gives you this flexibility always. You can figure out 1000 reasons for why you should care and another 1000 reasons for why need not care.

My personal view point is – “One should know internal working of computer atleast to a minimum”. But then, minimum is a subjective term. We have lot many subjective terms floating – like Health, Quality, Performance, Security etc. For some health means running 10 miles at a stretch and for some, health means walking a few steps. For some Quality Software means bug free software and for some Quality  may be different. For some Service means devlierying something in a day and for some , service means delivering something in a week. So, the subjective terms can only be quantified by you. As such, the minimum knowledge can only be quantified by you.

I wish you atleast read a couple of books a year (set this your minimum).

Ok, lets answer the question. Should I care. Take a look at this program.

using System;
class InternalWorking
{
     static void Main()
     {
          int i = 2;
         for (int j= 0;j<10;j++)
        {
            Console.WriteLine(” {0} * {0} = {1}”,i, i*=i);
        }
   }
}

If you compile and run the above program, you will see the following output

2 * 2 = 4
4 * 4 = 16 
16 * 16 = 256 
256 * 256 = 65536 
65536 * 65536 = 0 
0 * 0 = 0 
0 * 0 = 0 
0 * 0 = 0 
0 * 0 = 0 
0 * 0 = 0

Why is that “i” became zero after some point of time.

Now, I am going to initialize i to 3 in the above program and see what happens using

using System; 
class InternalWorking 

     static void Main()
    {
         int i = 3;
         for(int j= 0;j<10;j++)
         {
             Console.WriteLine(” {0} * {0} = {1}”,i, i*=i);
          }
      }
}

The output is as follows  
3 * 3 = 9 
9 * 9 = 81 
81 * 81 = 6561 
6561 * 6561 = 43046721 
43046721 * 43046721 = -501334399 
-501334399 * -501334399 = 2038349057 
2038349057 * 2038349057 = -1970898431 
-1970898431 * -1970898431 = 120648705 
120648705 * 120648705 = 1995565057 
1995565057 * 1995565057 = -1876701183

Oops.. why is that results are getting negative after some point of time. Well, definitely my computer is not mis-behavinig.

Ok.. here goes the explanation of why it gets to negative after some point of time. All integers in C# occupy 32 bits. Of this, Most Signigicant Digit (MSB) is Sign bit. The sign bit for positive numbers is 0 and negative numbers, its 1. So effectively 31 bits are used to represent the number. In the above program, we get the correct results as long as the result can be represented with 31 bits. When the numebr gets larger, it takes up additional bits. We know int uses only 32 bits and so, the remaining bits are ignored. The above program, after a few iterations, sets the 32nd bit ( which is sign bit). Thus it becomes negative. It gets into a kind of circular loop.

May be the following code will help you understand more 

using System;
class zip
{
     static void Main()
     {
          int iMaxValue= Int32.MaxValue, iMinValue = Int32.MinValue;
          Console.WriteLine(” Int Max Value is {0} and Int Min Value is {1} “, iMaxValue ,iMinValue);
          iMaxValue++;
          iMinValue –;
          Console.WriteLine(” Incremented Int Max Value is  {0} and Decremented Int Min Value is {1}  “, iMaxValue , iMinValue);
          Console.WriteLine(“Is Incremented Int Max Value equals Int MinValue? (True/False) : {0}”, iMaxValue == Int32.MinValue  );
          Console.WriteLine(“Is Decremented Int Min Value equals Int MaxValue? (True/False) : {0}”, iMinValue == Int32.MaxValue  );
     }
}

The output is 

Int Max Value is 2147483647 and Int Min Value is -2147483648
Incremented Int Max Value is  -2147483648 and Decremented Int Min Value is 2147483647  
Is Incremented Int Max Value equals Int MinValue? (True/False) : True
Is Decremented Int Min Value equals Int MaxValue? (True/False) : True

Negative numbers are represented using 2s complement. Well, I am not going to get into the details of 2s complement here. Do write your feedback..

Advertisements

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s