This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bit operation

Hello. I use nRF52 , PCA10040 , S132 , SDK 11 & Keil5.

I want nrf to do bit operation. The code about bit operation is following.

unsigned char tmp[2]; unsigned short us; tmp[0] = 0xf8; tmp[1] = 0x59; us = (tmp[0] << 8) | tmp[1]; printf("%d\r\n",us);

I thought the printf("%d\r\n",us); should output 63577, but it output another number. What is my code wrong?

Please help me.

Thanks,

Parents
  • while(1){printf("%d\r\n",us);} It did print out 160, 322, 307, 323.

    In detail my question, I use sensor what communication i2c. tmp is data from the sensor. I try the following code,

    unsigned short us = (tmp[0] << 8) | tmp[1]; printf("%x,%x -> %d\r\n",tmp[0],tmp[1],us);

    It output f8,59,322 .

    But unsigned short us = (0xf8 << 8) | 0x59; printf("%x,%x -> %d\r\n",tmp[0],tmp[1],us); It output f8,59,63577 . What shall I do?

Reply
  • while(1){printf("%d\r\n",us);} It did print out 160, 322, 307, 323.

    In detail my question, I use sensor what communication i2c. tmp is data from the sensor. I try the following code,

    unsigned short us = (tmp[0] << 8) | tmp[1]; printf("%x,%x -> %d\r\n",tmp[0],tmp[1],us);

    It output f8,59,322 .

    But unsigned short us = (0xf8 << 8) | 0x59; printf("%x,%x -> %d\r\n",tmp[0],tmp[1],us); It output f8,59,63577 . What shall I do?

Children
No Data
Related