i would check that "unsigned int" is actualy a 16 bit type as you have asumed in your code. In most c-compilers ive worked with for microcontrollers, int is actualy only 8 bit (0-256). This is becaus the cpu in a microcontroller is 8 bit arcitechute, and by standards, int is then 8 bit.
On a 16 bit cpu (old days) it should be 16 bit, on a 32 bit it should be 32 bit and so on...
Altho i know this is not realy true for c on compuiters (int kinda stayed 16 bit in most c compilers for pc, when 32 bit pc cpu's ,arived), it is true for example in the ccs compiler.
Try defining your "x" variable as an "unsigned long" and that should fix your problem.