Wednesday, May 20, 2009

Puzzle 25 - Solution

The '~' operator though rarely used gets you the two's complement of that number.
Basically what it boils down too is 2 is converted to -3, 1 to -2 and 0 to -1.

So, here the line that does the trick -
int x = ~0; //Here x is initalized to -1.
The one character used '~'

I did get couple of other interesting - though not correct solutions for this puzzle.

Kannan gives us - System.out.println("X is : " + 0) - The first time I saw it I was wondering how I had missed thinking of something so obviuos. This solution though relies on deleteing the character 'x' -so it can't be considered right.

Shyam - gives us a solution without any additional characters. Read up his commenets here.

Who's got it right this time?
1. Alex
2. Behive
3. bernardomen - Thanks for your comments too!!
4. Tim Yates
5. Anonymous

No comments:

Post a Comment

Solution for this question?