M G Harish

Thursday, February 15, 2007

Exploring new ways

If you have ever been to programming, you will surely have been asked to write a program to swap two numbers (If you haven't bothered about this simple program, you need not have to continue reading this article). Even if you are not well versed with the concept of programming, you will be able to solve this problem in one or the other way. Well, that's good. But I am trying to do much more than that, something that involves the combination of knowledge, thinking and understanding. I will go on describing about it in C language, since that's the language I like and that's the language I speak!!

Let's get down to the business here. If you ask anyone to swap two numbers, he might do it in this way:
temp = a;
a = b;
b = temp;

That's right. But it needs one more variable, temp. So, let us think and find some other way that needs no extra variable. If you know simple arithmetic, you might come up with this way:
a = a + b;
b = a - b;
a = a - b;

Let us see how the above code works.. For example, if a = 5, b = 7 then a = a + b; makes a = 12. The next statement b = a - b; makes b = 5 and the final statement a = a - b; makes a = 7. As we see, the values of a and b are interchanged. Our goal is achieved. Wow, the extra variable is eliminated! But there is one more concern here. It uses addition operations instead of mere assignments as in the previous case. Yeah, but what's the problem with that? you might ask. Well, there is, and you will notice it if you know how the microprocessors calculate. Addition is not a logical operation. It is an arithmetic operation. It is a combination of Exclusive OR (XOR) operation and AND operation. (Although ALUs of modern processors take care of this and do the operation in same amount of time as that needed for individual XOR or AND operations, I am not going to stop here, I'm here to explore new ways!). So, is there any way of still improving this? Yes, there is...

Now, if we can somehow eliminate this combinational operation of XOR and AND, we can achieve some more efficiency. For that one needs the knowledge of bitwise operations and obviously needs to know binary arithmetic. Assuming that you know it, here is the way:
a = a ^ b;
b = a ^ b;
a = a ^ b;

Going back to the same previous example of a = 5 (0101b), b = 7 (0111b) we get a = 2 (0010b) after the execution of the first statement. And after the second statement gets executed, we will have b = 5 (0101b) and after the execution of the third statement, we will finally have a = 7 (0111b) which is what we need.

Hmm, think it is the optimum, don't you? But I don't think so... If you know C, you will know that C provides shorthand operators. So, how about this:
a ^= b;
b ^= a;
a ^= b;

Well, now no one can further compress it. Eh, you think that? Remember, C provides the provision to put multiple assignments in a single statement and the operations are carried out from right to left. So, I come up with this, which is nothing but the merging of the above three statements, which I think, is the optimized one.
a ^= b ^= a ^= b;

If you find any piece of code that beats it, let me and the rest of the world know...

Friday, February 09, 2007

Aspired - Inspired - Expired

He was a boy of just seventeen years. Yet, he seemed to have got enough of his life. Out of frustration, he decided to commit suicide.

Sleeping pills and whisky make a deadly combination, he had heard. He worked in a drug house, where he stole some sleeping pills. He waited for a Saturday, for the home to be deserted. He was all set to go, go forever. The boy drank whisky and picked up the pills to take. At that time, his father who had forgotten something, and came back to the home. Loss of memory can, many times, be a boon and seeing this boy drinking, he asked what was going on...

The first reply was "Nothing", as expected. But when the father noticed the pills, there was no way to pretend. He said "I'm going to commit suicide". Well, some situations make you daring. The boy continued, "If you stop me today, I'll do it tomorrow". It is a tough time for a father, any father for that matter. What can he do now?

If the father was just like any other person, the story would have been different. But he wasn't like others. He called his son for a little walk, saying that the boy didn't seem to be in any urgency. That was true, for, the boy agreed to go for the walk. The father talked about many things, but none of them got the attention of the boy, until he said something about the boy's ambition to become a writer. Although it got the attention of the boy, it was a normal sentence, and he replied that it was a dream, which didn't come true. "That was yesterday". Father was very cautious, and asked "What about tomorrow?". The boy was puzzled. The father continued, setting up one of the best examples of inspiration... "You don't know what can happen tomorrow. Life is like a novel. Isn't it? It's filled with suspense. You have no idea what's going to happen until you turn the page". That's true, the boy thought. He had not seen the next pages of his life at all... There might be something wonderful happening tomorrow, he thought. And finally, the boy decided to postpone his suicide.

Does it seem to be a good fiction? Sorry, it is not a fiction. It is a biography. Story of one of the best modern English novelists. It is the biography of Sidney Sheldon, author of best selling books like If Tomorrow Comes, Master of the Game, The Other Side of Midnight, Rage of Angels etc.




The incident above took place in 1934, and Sidney Sheldon had to wait for many more years before becoming a famous writer. He started writing at the age of 50. He achieved what he wanted to be, and became a legendary writer.

Even though he is such a great writer, and almost all bibliophiles have read his novels, it is unfortunate that his death wasn't a big news at all. Yes, he passed away, on 30 of January 2007. I came to know about it after 4 days. An inspired soul, which had aspired to be a good writer, has expired on the same date as the other great soul Mahatma Gandhi was assassinated.

You can read the complete autobiography The Other Side of Me. And, if you are curious about every details of what happened on that day, you can just go through this exercept of his autobiography.

Friday, February 02, 2007

The fate of engineering: Jogi Remix

ಹೊಡಿ ಮಗ, ಹೊಡಿ ಮಗ, ಹೊಡಿ ಮಗ, ಹೊಡಿ ಮಗ, ಬಿಡಬೇಡ Copyನಾ...
ಅಕ್ಕ ಪಕ್ಕ ಮುಂದೆ ಹಿಂದೆ ಯಾರೆ ಸಿಗ್ಲಿ ನೀನು ಮಾತ್ರ ಬಿಡಬೇಡ Copyನಾ...
Engineering ಸೇರಿದ ಮೇಲೆ ಹೊಡೀಲೇ ಬೇಕೋ...
ಈ ಕೈಗೆ ಪೆನ್ನು ಸಿಕ್ಕಿದ ಮೇಲೆ ಬರೀಲೇ ಬೇಕೋ...
ಮಗ ಜೋರಾಗ್ ಬರೀಬೇಡ, Nervous ಆಗಬೇಡ
Bench ಅನ್ನು ಮುರೀಬೇಡ, ಬರ್ದು ಬೇಗ್ ಮುಗಿಸ್ಬೇಡ... ಲೇ
ಬಲವೀರ ರೆಡ್ಡಿ ಮುಂದೆ ಬಿಟ್ಟು Sketch ಹಾಕ್ತಾರೋ...
ಸ್ವಲ್ಪ ಕೂಡ ಯಾಮಾರಿದ್ರೆ Fail ಮಾಡ್ತಾರೋ...
ಬರಿ ಗೋಳು ಕಣೋ... ಇದು VTU ಕಣೋ...
Revaluation ಇಲ್ದಿದ್ರೆ ಸಾವೇ ಕಣೋ...


This is an SMS I got from one of my friends. I am in a dilemma here, whether to appreciate the creativity of the writer, or to commiserate him for being a VTU student.

This message not only exposes the condition of the VTU students, but also describes the wrong paths chosen by the students in order to survive. Hats off to the original author of this message!!