M G Harish

Thursday, September 27, 2007

Free SMS for Karnataka Spice Customers!!

With all the Cell phone operators increasing the charges for SMS, it has been a difficult time for the students and those who rely upon SMS for the communication most of the times. Spice, Airtel, Hutch all have increased their tariffs to 5 paise or above. But a week ago, my friend RJP sent me a message telling a trick to get free SMS. This works only for Spice customers in the Karnataka circle.

Change your message centre number in the message settings from default number to +919810051913 +919840011055 +919810051873.

A few notes about this trick.
  • It works only for Karnataka Customers.
  • All messages are completely free.
  • This works only for Spice numbers which start from 9964.
  • You can't recharge your mobile by sending SMS with this message centre. You have to revert to your old message centre, recharge and then put this number back on.
  • The specified message centre is Broadway II Gateway number.
  • Even interstate messages are also free.
Just enjoy the free messages as long as Spice finds this out and corrects this bug. I owe you a lot RJP :)

Tuesday, September 25, 2007

Wonder at the Wanderers!!!

First things first. Congratulations India for winning the first ever Twenty-20 Cricket World Cup. Congratulation Pakistan, for posing a spectacular final at the Wanderers stadium, Johannesburg.

Champions: Indian Team celebrating after winning final of Twenty-20 Cricket against Pakistan

It had all the ingredients to be a tooth-and-nail fight, a treat to watch out for anyone, even remotely connected with cricket, in and around the Subcontinent - a dream contest between the traditional rivals.

Let's set aside the result for a while and look how it fired up every one in the contending countries, especially in India. Even the fussiest roads were cool and calm as country-yards, even the money minded IT companies, which want its workers to work on Saturdays too, had allowed its officials to call it a day a bit early. And, everyone, no matter interested in cricket or not, watched at least a part of the match. After all, that's how it works in a country where cricket is a religion!

Now to the result of the match. Who in this world has imagined that the team India would win? A team which had no veterans playing, which had no regular coach, which had just got a new captain to lead the team and which had a bunch of novel players half of which hadn't played even a single international T20 game. What a wonder! But that's cricket for you! Anything can happen until the last ball is bowled. India somehow clinched the cup, but the real winner was the game, as Sunil Gavaskar told after the match.

The aftermath? Celebrations in India at least for a week, a warm and grand welcome and pouring cash prizes from each corner of the country for the players forgetting the past performances, and what not? The players can sigh a breathe of relief after what happened in the World Cup earlier this year.

Foot ball to start with, then hockey and now Cricket!! Can this be a CHAK DE INDIA magic?!

A final punch: Forget cricket, former President APJ Abdul Kalam's dream has come true. India has become a strong power in 2020!

Monday, September 24, 2007

One-liner ABS Function using Binary Operators

A few days ago, my friend TJ, who always keeps finding innovative programming challenges, asked a question: "Can you implement abs() function without using any relational operators in C?". But he also insisted to do it without using if, for, while etc., any control and loop structures for that matter.

I immediately came up with this:
abs_x = sqrt(x*x);

But he is not a guy to get contented with such an answer. He told that sqrt() internally uses relational operators in its implementation. Well, true. The sqrt() function that is included in math.h uses series expansion methods, which use if() statement in one or the other way. So I had no choice but to find some other way. After struggling a lot, I came up with this which uses only binary operators and arithmetic operators:
abs_x = (x^((((x&(1 << (sizeof(int)*8-1))) >> (sizeof(int)*8-1)&1)*-1)) + (((x&(1 << (sizeof(int)*8-1))) >> (sizeof(int)*8-1))&1);

The sizeof() opeator is necessary only to generalize the expression for any size (16 bit or 32 bit) of the integers. Otherwise a smaller version of the same would suffice.

For 32 bit integers:
abs_x = (x^((((x&(1<<31))>>31)&1)*-1))+(((x&(1<<31))>>31)&1);

or
abs_x = (x^((((x&0x10000000)>>31)&1)*-1))+(((x&0x10000000)>>31)&1);

For 16 bit integers:
abs_x = (x^((((x&(1<<15))>>15)&1)*-1))+(((x&(1<<15))>>15)&1);

or
abs_x = (x^((((x&0x1000)>>15)&1)*-1))+(((x&0x1000)>>15)&1);

Does it seem complicated? As we know, in C, numbers are represented in Two's Complement method. In 2's complement method of representation, the most significant bit represents the sign of the number. This bit will be 0 if the number is positive and 1 if it is negative.

The idea is to first check the most significant bit of the number. If it is 1 we need to negate the number. To negate a number, we should find the one's complement the number and add 1. That is what is done in the above expression!!

Do you think it's a great piece of one-liner code? It would have been, if it was impossible to simplify it further like swapping code posted before. Yes! It can further be reduced! Look at this code:
abs_x = x*(-2*(((x&(1<<(sizeof(int)*8-1)))>>(sizeof(int)*8-1))&1)+1);

For specific length, it looks like (for 32 bit integer):
abs_x = x*(-2*(((x&(1<<31))>>31)&1)+1);

All this code does is to multiply the original number with -1 if it is negative and with 1 if it is positive! Short and sweet one-liner, isn't it? But I can't say it's the shortest like I did in my earlier post. Feel free to express your opinions!

UPDATE
I guess thinking a lot certainly helps! I was able to find a still shorter one-liner!!
abs_x = x+2*x*(x>>(sizeof(int)*8-1));

Can I say this is the shortest? I still doubt!!

Monday, September 03, 2007

Beauty is in the eyes of the beholder

This is an era where everything is concluded with the result of a contest or a poll. Beauty isn't an exception. Beauty pageant, however, is not only based on the external appearance, but also the wit of the person. Nevertheless, errors happen and wrong persons get through various phases in the contest and, sometimes, even emerge as the winners despite having no brainpower. One such example happened in Miss Teen 2007 contest in USA:

Miss Teen South Carolina 2007, Lauren Caitlin Upton, screwed up her response to the question:
Recent polls have shown a fifth of Americans can’t locate the U.S. on a world map. Why do you think this is?


In case you are unable to follow what Miss Lauren Caitlin Upton said, here it is (What she might have intended to say, is emphasized using bold letters)
I personally believe that U.S. Americans are unable to do so because, um, some people out there in our nation don’t have maps and uh, I believe that our, I, education like such as uh, South Africa, and uh, the Iraq, everywhere like such as, and I believe that they should, uh, our education over here in the U.S. should help the U.S., uh, should help South Africa, it should help the Iraq and the Asian countries so we will be able to build up our future, for us.

In spite of going through this quotation many times, it makes no sense to me! But to err is human, and she has admitted her mistake and also has tried to correct her answer in a recent interview:
Well personally, my friends and I, we know exactly where the United States is on our map. I don't know anyone else who doesn't. And if the statistics are correct, I believe there should be more emphasis on geography.

Looks a bit cleaner, isn't it? Well, the real irony is that she went on to become the third runner-up in the Miss Teen USA 2007 contest!! As they say, beauty is in the eyes of the beholder, and the judge must have seen something special in Miss Lauren Caitlin Upton, may be humor, to pick her as Miss Teen South Carolina 2007!

PS: This blog crossed the landmark of 10000 visitors today.