Monday, January 06, 2014

C++ unsigned arithmetic gotcha

Recently I came across a piece of code that looked similar to the following:


int main() {
  unsigned int a, b;

  a = 20;
  b = 30;

  double c = double(a - b);

  printf("%lf\n", c);


One may, at first glance assume that this would spit out the required -10.0, however this is not the case. It will print out a huge number something like 4294967286.0 !

The reason for this is that the datatype of a and b are unsigned types. And at least with the GCC compiler (or for that matter, I assume any compiler), the intermediate variable that is generated would be an unsigned type, resulting in "mis-interpretation" of the sign bit when the casting operation is performed. 

In other words, when a subtraction is performed on unsigned quantities, you should better be careful to see what actually is happening! 

Sunday, January 05, 2014

This year : an experiment with an iOS device

I have used a number of iOS devices, but have never owned one, and never used one on a daily basis.

Enter iPod touch 5g (16gb model).

Lumia 800 and iPod Touch: going to use both on daily basis.

Now along with my two year old Lumia 800, the iPod will be my companion daily driver device. Being only 86 grams this is a sweet little pocket friendly device and compliments my Lumia in every way.

Let us see how this combination goes :)