result = * ((unsigned short int *)(buffer + x));
これは
result = * ((unsigned short int *)(buffer) + x);
でないの?