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
  • gcc (at least this version) treats printf as an ordinary function with lots of arguments, so the first few go in registers, the next go on the stack, but all promoted to 32 bit as per the usual ARM ABI. Keil, I believe from what I have read, treats printf as a va_arg function and concatenates all the arguments into a block of memory then passes that and printf reads it with the equivalent of va_start() .. etc. So if you get the format specifier wrong on keil, you read (in this case) two extra bytes of random junk.

Reply
  • gcc (at least this version) treats printf as an ordinary function with lots of arguments, so the first few go in registers, the next go on the stack, but all promoted to 32 bit as per the usual ARM ABI. Keil, I believe from what I have read, treats printf as a va_arg function and concatenates all the arguments into a block of memory then passes that and printf reads it with the equivalent of va_start() .. etc. So if you get the format specifier wrong on keil, you read (in this case) two extra bytes of random junk.

Children
No Data
Related