What is the size of int in DOS?

This is a common question that arises in so many minds who are learning to program in any high level language such as C. Thanks for asking. While programming, we need a way to make our program “remember” values. So, we use “variables”. For example, the statement, “int i = 10;” defines an int variable and assigns the value 10 to it. Further, this allows to manipulate the value in statements like “i = i + 20;” or “i = i * 2;”. Thus we are able to build on this idea and write more complicated programs. When we go for placements, or even for GATE kind of exams, the concept of space complexity is of utmost importance. Space complexity denotes the amount of memory our program will occupy. Based on this, programs are sometimes classified as good or bad. Sorting is an example. You can sort in-place i.e., without significant additional memory! The variables we declare and use are the primary reasons for memory usage. More variables you declare, your programs need more memory (RAM). Let us assume you have one program which uses 100 bit type variables and 100 strings. It is a no-brainer to claim that the latter program needs more memory than the former. Each data type, uses a defined amount of memory. Now that we understand the background of variables, data types and the space occupied by them, let us get to this question.

How much memory does int occupy on a DOS system? Let me assume for now that we are talking about C programming. DOS is a family of disk operating systems. There are multiple flavors of DOS namely MS DOS, FreeDOS, AppleDOS and so on. Refer to the wikipedia page of DOS for more detailed discussion on DOS. For this discussion, let us consider MS-DOS, the microsoft DOS that we know better. Interestingly, there are different flavors of DOS based on the underlying system architecture. Here comes the notion of “32-bit” machine. This means that the registers and addresses are 32-bits wide. Even FAT uses 32-bits. So, you cannot store one single file which is more than 4GB on your USB drive even if the drive is say 16 GB in size. Anyways, that was just a side-detail.

An Integer is a primitive data type. To learn about the memory occupied by integer, we refer to the C99 or any such standard. The standard only requires size relations between the data types and minimum sizes for each data type. C wants compiler designers to be only ensure that int is capable of containing at least the [−32,767, +32,767] range. Therefore, it is at least 16 bits in size. But, if we use 32 bit registers, we cannot allocate half register. So, nowadays, it is 32 bits. Earlier, when most C books were written, we had 16 bit processors. So, they stuck to the standard and provided 16 bits to int. Nowadays, it depends on the processor architecture. If you are on a 64-bit machine, you are even worse. You need 8 bytes or 64 bits! You can check this by yourself by using sizeof() in C.

So, the final answer is, assuming the version of MS DOS uses a 32-bit architecture, the sizeof() is very likely to give 4 as result (i.e., 4 bytes = 32 bits) for the size of int. Note the word, “likely”. It means that the compiler designer can come up with a different size. As long as it is more than 16 bits and adheres to the size ratios, they are allowed to do so. If no information such as DOS, or system architecture is given, we go with the C99 standard which requires an int to be at least 16 bits i.e., 2 bytes. Hope this answers your question.

One final bit to add here is that do not memorize these sizes. It is not required. Remember the ideas but not the exact number of bits occupied by each data type. That is not necessary and is often incorrect as I have explained above.

Advertisements

Things not going your way? Feeling depressed?

When the goals are shallow, depression is a typical consequence. Define success in such a way that it keeps you occupied for life. Do not keep a deadline for success.

Let me elaborate. Ask yourself, what is that you want to achieve in life. Look around. Understand the problem that attracts you. You come from financially deprived family. You perhaps want to eradicate poverty, or at least ensure that there are no more youth like you who is suffering for wrong reasons. Start thinking what you could do to solve this issue. It is a journey. It is not one step that you need to take. Commit your life for one such goal. In the meanwhile, you need to do a variety of things. You need to sustain yourself first. You need a job. You need a family. You need to keep your family safe and happy. You need good friends. For this to happen, you need to be mentally and physically strong. So, invest your time in all these things. You will quickly realize that getting a job is a small piece of a much bigger problem. The nature of the job only matters if it is somehow connected to the problem you want to solve.

Alright, having a goal for life and the pursuit to achieve it will keep us away from depression. It will keep us busy. It will bring joy and prosperity. But, this is not all. We need to deal with the intermediate small problems. The problem at hand is to find a job that helps you to stay healthy and reasonably wealthy. The mantra here is to be a good thinker, actor and producer.

Let me elaborate again. The common mistake we do when we look out for jobs is to see where the crowd is moving towards. Everyone is going to a call center does not mean that is the only place available for work. For example, consider teaching in a small school. It will pay you at least 20k and will help you lead a good life. It will force you to be a good communicator. It will keep you active by standing and walking for most parts of your day. There are many such jobs – administrative jobs in colleges and companies are another option. We have been trained to stop thinking from our early school days. For example, one of my teachers used to reprimand me for asking questions. She does not like anybody to ask question. So, gradually, students became 1) memorization machines and 2) follow-the-crowd ants. Get out of this mode. Find the right job. Do not spoil your life to get into one wrong job which is not working for you. Thinking deserves at least two more pages of writing. But, let us now move towards acting. I do not mean the cinama kind of acting. This is to “work”. Act. Put effort. Everyday, make a small plan on how you want to progress towards a goal. Execute that plan. Without thinking, acting will only waste your time in the wrong direction. This is what is happening with you in your UPSC and GATE preparations. You need counselling on how to prepare. Stop cramming all topics and memorizing formulae. Stop studing everything in the world. Other members here can give tips on how to study. Same applies to interviews. Write a great resume, apply to the right places and know how to ace interviews. With the commitment to work hard, there is nothing we cannot achieve. Finally, just thinking and acting is insufficient to a happy life. Start producing results. If you are not producing results, make smaller objectives (towards the same big or intermediate goal). Try to achieve them. If you still cannot, make it even smaller. If you cannot achieve anything, there is something wrong with you. Go back to the first step. Think.

We have one life. One opportunity to experience. Your situation is unique. Your experience will be unique. Make it useful, not only to yourself but to everyone around. Cheers and good luck.

I’m unable to think of a solution within the allotted time. I can solve them, but it takes too much time. This way, I won’t score well in the actual exam. Can someone tell me a way to improve?

Well, I can only say that you are not alone. I too am very slow in my thinking. Am sure there is nothing wrong with my comprehension or application skills. But, I need my time. To put it better with an example, if I have huge amount of time, I know how to find the best move in a chess match. But, there are millions around who will find that best move much faster. When I asked around the same question, I found these interesting points.

Firstly, almost everyone said, practice more. Perhaps, practice improved their speed. I had a hard time memorizing 8 * 7 in my childhood. I used to do 8 * 5 = 40 first. Then add 8 twice to it. But over a period, I realized that if I have to calculate it 100 times in a week, I will eventually remember it. So, I did accept the point. Yes, practice does matter.

But, this still did not answer my question. How come, the other kids could do 8 * 7 in the first attempt? This is when I realized the second and bigger secret. Pay attention to this. First time, I memorized the 8th Table, I took a shortcut. I studied till 8 * 5 and then I knew that I can always use addition trick to answer. That one day of laziness had cultivated this bad habit. Now, my brain refuses to memorize it. Again, I can take a chess example here. In chess notation, a1 is the left bottom square from where white plays. b2 is diagonally above a. So, you go one step right and one step upwards from a1 to reach b2 and so on. Fairly easy stuff. Now, when I learned this notation, I used to calculate from a and 1 and then narrate the square. It just became the habit. While some kids memorized the squares, I was calculating it. A bad habit. It led to humongous cost. I was so slow in taking notes during a game.

Alright, now I understand what slows me down. But, how to improve it. Breaking these unhealthy habits is not easy. Luckily, there are several ways we have known already. Know your habits, target them with training. Nowadays, I spend several minutes just visualizing and thinking differently about notations. Another thing you could do, is try a bit of meditation. Am not joking here. In a way, we are addicts of compulsive behavior. Our mind knows that it must change but refuses to change. The best solution is to take some time, give our mind a rest. Clear it off. Then, try again. This works. I spend around 15 minutes whenever I get time, to just stay still and relax. Finally, the best of all ways, is to find a purpose or a passion. The stronger your passion is, you will find innovative ways to circumvent your problems. If nothing works, only one thing can save you, God! Really, I am not kidding. Transcendental purpose has supernatural powers. For me, helping students is my way to find a spot in heaven. It is my dharma, my duty. It gives me transcendental satisfaction and fulfillment. I have conditioned my mind to believe that my purpose of life is to find a smile in a student, promote computer science and play chess. You may have different purpose; but if you believe in God, then all the above can be sidestepped and you can still win.

Good luck!

PhD Life

PhD life is like a train that takes you to a new station every day. You look through the window in anxiety for whether you will find something new, something novel as we say it! Moreover, the taste of the same tea but from different hands in different stations, is so different! This is what happens to us when we find the same solution for the same problem through a different route.