Wednesday, April 15, 2009

Big endian or litle endian?

Use this below code to find out whether a processor is big-endian or little endian,

 int x = 1;
if(*(char *)&x == 1)
printf("little-endian\n");
else printf("big-endian\n");
or a union:
 union {
int i;
char c[sizeof(int)];
} x;
x.i = 1;
if(x.c[0] == 1)
printf("little-endian\n");
else
printf("big-endian\n");

Fake coin!


You have 10 bags with coins (each containing more than 100 coins ) and a pair of scales (one that doesn't compare weight's ,it just counts it). 9 of the bags contain real coins each weighing 10 grams and one contains false each weighing 9 grams .How can you find out which bag contains the false coins by using the scales just one time.

Answer : Take 1 coin from the first bag, 2 coins from the second bag, etc. through 10 coins from the 10th bag. If all of the bags contained genuine coins this would total 550 grams. The number of grams short of 550 tells you the bad bag.