Swapping Values of Two Integer Variables

The simplest way to swap the values of two variables is to have a third variable that can temporarily hold the value of one variable while the other is being copied. This is very easy code, but a small problem is that we have to allocate extra memory. In these days of gigabytes of data, that is not a huge concern. But still it is useful to know a technique where you can achieve that.

So here are a couple of ways to do that I saw in a few places. I don’t have the name of the original author, I am assuming that it became common knowledge early. The first method uses addition, but if you have large values, then there is the chance of arithmetic overflow based on the result. The second method uses bitwise operations that avoid the “over-flaw” (sorry!)

public class Swap extends Object
{
    public static void main(String args[])
    {
        int a = 123;
        int b = 345;
        System.out.println(a + " : " + b);

        //Swapping using temporary variable : straight-forward method
        int temp = a;
        a = b;
        b = temp;
        System.out.println(a + " : " + b);

        //Swapping using arithmetic operations : saves memory
        a = a + b;  //You can use a += b;
        b = a - b;
        a = a - b;  //You can use a -= b;
        System.out.println(a + " : " + b);

        //Swapping using bitwise operations : avoids arithmetic overflow
        a = a ^ b;  //You can use a ^= b;
        b = a ^ b;  //You can use b ^= a;
        a = a ^ b;  //You can use a ^= b;
        System.out.println(a + " : " + b);
    }
}

Book Review — The Mystery of the Blue Train, by Agatha Christie

Rating: ** (2 stars out of 5)

The Mystery of the Blue TrainOne of Agatha Christie’s poorest efforts with Hercule Poirot, as she herself acknowledged. I wouldn’t say that it is a complete waste of time, but the writing is amateurish and the final conclusion not very satisfying, Poirot’s summary failing miserably. The “murder on a train” plot was done in a much better fashion in “Murder on the Orient Express”.

The key problem in the story is the lack of characters you want to identify with. In many stories, Christie builds up characters that we become very sympathetic to. When some suspect is used as a red herring, either Christie comes up with a justification that we empathize with, or the character is truly bad and comes to a miserable end. In “The Blue Train”, the characters are so skin-deep that I actually wanted one of the “good” characters to be the murderer, which didn’t happen. While the ending itself was pulled out of a hat, it was at the same time totally predictable the way the rest of the book went.

More Hindi Songs, Contd.

I guess there will be more of this series. I haven’t watched some of these movies, so there will be fewer notes.

  1. Chahunga Main Tujhe”, from “Dosti” (1964).
  2. Yeh Jeevan Hai” from “Piya Ka Ghar” (1972): Another song in it (““Ye Zulf Kaisee Hai””) is also sweet.
  3. Mera Raja Beta” from “Anuraag” (1973): There are 2 versions of the song, the happy one is very sweet and the sad one is heartbreaking in the context of the scene.
  4. Tere Mere Hoton Pe” from “Chandni” (1989): I liked this movie when I saw it, but can hardly remember the story unlike many movies from the same period. But the songs are still pretty good. Another one is “Lagi Aaj Sawan”.
  5. Morni Bagha” from “Lamhe” (1991): The first half is excellent with both a good story and great songs, but the second half goes downhill. Not sure why it is a cult classic because the change of pace and emotions in the second part makes the movie fall down on its face. This song perhaps epitomizes the movie as it too goes wobbly at the end.
  6. Tum Bhi Chalo” from “Zameer” (1974): I don’t remember much of this movie other than it is kind of the opposite of Lamhe, where it started poorly, but ended on a high note. But my memory could be failing me here.
  7. Zindagi Imtehan Leti Hai” from “Naseeb” (1981).
  8. Thoda Hai Thode Ki Zaroorat Hai” from “Khatta Meetha” (1978): Some rotten acting, but the song itself is good.
  9. Kuchh Dil Ne Kaha” from “Anupama” (1966): Another great song from the same movie is “Dheere Dheere Machal”.
  10. Aane Se Uske” from “Jeene Ki Raah” (1969)
  11. Chhup Gaye Saare Nazaare” from “Do Raaste” (1969): The popular songs of this movie have little to do with the main plot.

Book Review — Murder in Mesopotamia, by Agatha Christie

Rating: *** (3 stars out of 5)

Murder in MesopotamiaThis is one of those mysteries where you are very happy at the conclusion when you are reading the book, but after sometime, you start questioning everything. The book develops as Agatha Christie novels usually do. An initial murder, suspects, additional murders, red herrings followed by the final unveiling of the actual murderer. The explanation seems to fit all the known facts. So you are happy. But then after you put down the book, your mind wanders back to the book and slowly things that you digested seem to float back up.

How do I explain this without any spoilers? Well, first, if the murderer is who he/she is revealed to be, then it is pretty incredible given the other events described in the book which are not connected to the murderer, but happen during the course of the novel. Second, the crime depends on too many things to go exactly right, including the timing. There is also the question as to why the murderer chose this particular method of execution when there were so many easier ways. Why even Mesopotamia for that matter?

Book Review — The ABC Murders, by Agatha Christie

Rating: ***** (5 stars out of 5)

The ABC Murders

This is one of my most favorite Agatha Christie novels. It starts with a surprising twist as a serial murderer taunts Hercule Poirot by sending him letters of his upcoming murders before they happen and challenges him and the police to stop him from getting to his next victim. The victims are chosen using their names that start with  the next letter of the English alphabet in a town that matches the same criteria. The result: A massive manhunt followed by a mind-blowing conclusion.

What is particular brilliant about this novel is that while red herrings abound, the reader is left wondering at the end how they could have missed the obvious solution. There is a key question that is asked and left improperly answered at the start of the story. And it is only when Poirot asks that question again do we understand how we are led astray by emotional thinking. Everything falls into place perfectly at the end, even a minor lie by Poirot. The story-writing is excellent: fast-paced but engaging.

Book Review — The Creative Habit, by Twyla Tharp

Rating: ** (2 stars out of 5)

The Creative HabitCreativity, like many other business concepts, is an elusive idea, even for those who are creative. What works for some may not work for others. You cannot teach people to be creative, but you can provide ideas for creative people to improve themselves and be more productive.

In that sense, Tharp’s book provides some useful insights into activities and behaviors that can produce ideas and reduce dead time. However, it is surrounded by a lot of fluffy stories that turn this book into more of an auto-biography than a self-help book. Which would have been okay, if the author was something other than a Broadway choreographer. I suppose it is a worth a quick browse, but nothing more than that.

Book Review — 30 Years of Laughs and Lasanga, by Jim Davis

Rating: ***** (5 stars out of 5)

30 Years of Laughs and Lasanga[Perhaps the most famous cat in the world, Garfield delivers in this book. This is a collection of past Garfield comic strips with the cartoonist Jim Davis explaining some of his decision making process. Davis has a different idea about commercialization than Watterson (of Calvin and Hobbes) and he makes a compelling argument.

Garfield, as usual, torments his owner and everyone who comes in his path. There is lovable Odie. There is the gluttony and mad rushes at lasagna. There is Garfield’s laziness which results in the audacity of the mice in the house. And finally Garfield’s terrible singing on the fence in the wee hours of the night.

Warren Buffet and The Interpretation of Financial Statements, by Mary Buffett and David Clark

Rating: *** (3 stars out of 5)

The Interpretation of Financial StatementsA dry, short book that explains which elements of financial statements does Warren Buffet pay attention to when he estimates the value of a company. In a typical company’s annual statements, there are many different figures. It is a mistake to focus on each of those figures because they can obscure the real truth of what is going on. This book provides insight into which numbers are more meaningful than others.

The book does more or less what it claims to do. The authors use a point-by-point analysis of each financial figure, but I expected a more comprehensive analysis. There is no summary that allows the reader to understand the relative importance of the various criteria, or the special circumstances under which they change. You can find bits and pieces scattered throughout the book, but it is not consolidated properly.

Book Review — The Girl in Blue, by P G Wodehouse

Rating: ** (2 stars out of 5)

The Girl in BlueThe title refers to a miniature painting that is the center of focus of activities of the chief protagonists. The action quickly moves from New York to London to country houses. Although it has the customary Wodehouse mix-up plots, there are a few disappointments. Primarily, it does not contain any of the regular Wodehouse characters. Plot points with tension are resolved rather quickly, instead of prolonging them to create more suspense. There are very few twists in the tale, or at least which cannot be predicted easily. The start of one of the story lines (where a donation is made to the church) is rather unbelievable and spoils that part of the book until its unsatisfactory explanation. I did like the final comeuppance of the bad character, though the rushed-through ending of the book prevented me from enjoying the moment to the full.