Thursday, April 23, 2009

tricky or idiomatic

Ok  have written all this page and windows live writer stopped working when  i was posting, i hate programs when they do that (dont   get backup) i will tell about backing up in another post.I will not write all the stuff back i have things to do, please tell your best wishes to live writer group .

int x = 2;
int y = x << 1 | 1;
int z = x << 1 + 1;

ok these are not same y = 5 and z is 8 because of order of evaluation.

//x   y
//0   2   
//1   0
//2   1
 
int x, y = 1; ;
x = (2 - y) * (1 + 3 * y) / 2;
 
x = y + 1;
if (x == 3) x = 0;
 
switch (y)
{
    case 2:
        x = 0;
        break;
    case 0:
        x = 1;
        break;
    case 1:
        x = 2;
        break;
    default:
        {
           throw new Exception("incorrect parameter");
        }
}

ok here is 3 different codes that do the same thing : return values associated in table above code like 0 if 2 is given as parameter.

the first one is tricky and bad the second is good idiomatic and the last one is suboptimal and slow only may be used if algorith should be broken with future numbers.

Ok i know i didn’t told anything but i am sure codes will be explanatory enough.

No comments: