Code pondering – Number 1: Swapping two integers

Having done a fair few academic computer science courses and various coding jobs in a past-life I found that it was often thinking about the smallest code snippets carefully that you pick-up the raw building blocks for good, safe and efficient code. I haven’t written much code recently so have been brushing up my basics.

Compilers are now so good and computers so powerful that in practice you can write pretty terrible code and still make a perfectly performant and functional application.

One famous code problem is to swap two numbers without using an additional temporary variable. So you use only the memory already storing the numbers without requiring extra for the temporary variable. Consider….


Method 1: Using a temporary variable

int temp;

int a = 6;

int b = 5;


temp = a;

a = b;

b = temp;


Method 2: Avoiding a temporary variable

int a = 6;

int b = 5;


a = a + b;

b = a – b;

a = a – b;


OK now let’s think about this harder, which method is better?:

  • Method 2 seems clever and no unnecessary memory used


  • It’s far less readable
  • If you really are just swapping two integers the extra memory for a single integer is negligible
  • the sum (a+b) can larger than the addressable space of an integer so overflow could occur, i.e. (a+b) could wrap around and the algorithm break!
  • If using real or another data type you could pick up machine level errors
  • I suspect many modern compilers would take care of this for you
  • With method 1; a is safely stored in temp should something go wrong
  • Both methods use the same number of copies but the amount of checking required to ensure no overflow occurs on (a+b) and similar really is more trouble than it’s worth and would make the code look messy

So when would you avoid a temporary variable:

  • When you are certain your data is going to be well below half the capacity of the data type (e.g. int) – more relevant if looking at facetted data in real format; i.e. scenarios where you can safely avoid extra checks and code
  • When you don’t care about machine level noise
  • When it isn’t just two pieces of data but a vast array of data perhaps representing a frame buffer or screen

Of course, we can now go on to ponder how this code could be made fail safe…. But it’s amazing how three lines of code make you think a bit harder…. And another discussion is using pointers …

I’d be interested to hear whether people actually use Method 2 (other than those using EDSAC2) and how much can be relied on to be taken away by compilers… and whether people still think hard about such things…



When to start your tweets with a “@”! Why some people add a dot e.g. “.@rhbbse” to tweets

dottweet.pngThe quirk of twitter

To avoid personal chatter spreading and swamping others twitter put in a clever yet obscure feature to ensure those tweets you want to broadcast spread and one-on-one chatter dies a quick death in the twitterverse.

If you send a tweet starting with a handle i.e. an @ is the first character:

  • “@rhbbse you are awesome”

It will get seen in news streams by me and anyone who follows BOTH of us, “mutual friends”

If you want the world to know though you have to avoid the @ e.g.

By adding a “.” in front or some other text e.g. “Hey world” the tweet will go to all your followers and not just those that we share.

This also applies when retweeting others’ tweets which is why you often see a “.” added.


You can read more about this quirk here:

“Now, you don’t punish anyone, Dutch or otherwise, for having big boobs.” – Women in IT

I couldn’t really think of an image I wanted to use for this blog – so here’s a pretty picture of Bury St. Edmunds where it appears there are some women employed in IT!

“Now, you don’t punish anyone, Dutch or otherwise, for having big boobs.” is a famous quote from the UK Comedy “The Office”. In the UK this week there has been a “can sexy women work in IT” story in the news this week. Amazingly focused on a woman working in IT in my rural home town Bury St. Edmunds (which was one of the most surprising elements to the story – I thought I was the “only woman in IT in the village”!. The Office sketch is 15 year old and pre-dates the explosion in social media networking, the comical figure of David Brent discussing women’s bust size in the workplace actually seems to have come true. Continue reading “Now, you don’t punish anyone, Dutch or otherwise, for having big boobs.” – Women in IT

Coffee vs. Tea – Product packaging, marketing and licensing

tea_coffeeI’ve just got myself a shiny new Nespresso machine to make my home office be slightly more like a real one. It takes those rather expensive pods (c. 40p a shot) and churns out a cup of rather nice coffee that I can’t differentiate from a coffee shop nor decent office coffee machine. Continue reading Coffee vs. Tea – Product packaging, marketing and licensing

Christmas for mini-geeks! See Santa’s sleigh fly across the sky!

Yep that will fly!

As an ex-astrophysicist you hear some funny space related stories, one of my favourites is the Santa Sleigh trick! The International Space Station has a fab website where you can find out the precise dates and times when you can see the space station where you live, check it out here. Continue reading Christmas for mini-geeks! See Santa’s sleigh fly across the sky!