Keeping signed short in low 16 bits unsigned int

I program C on an embedded system. The processor architecture is 32 bits ( sizeof(int)- 32 bits, sizeof(short)- 16 bits). There is a 32-bit variable, which is a managed memory register ( CTRL_REG), which is indicated as soon as the lower 16 bits are used, and they contain a signed 16-bit integer value (writing to higher bits does not affect). Access to memory should be 32-bit, so I canโ€™t just pounce on a pointer to a couple of bytes, and I canโ€™t accept the statement either. I am concerned that auto type advancement will get confused with what I am storing by expanding the character bit to bit 31 instead of leaving it to bit 15 where I want it. What is the best way to keep something in this place?

Here is my original code, which I'm almost certainly mistaken:

#define CTRL_REG   *((volatile unsigned int *)0x4000D008u)
short calibrationValue;

CTRL_REG = -2 * calibrationValue;

Then I tried this, but I think it can still be subjected to integer progress at the destination:

CTRL_REG = (short)(-2 * calibrationValue);

Then finally I thought about it:

CTRL_REG = (unsigned short)(short)(-2 * calibrationValue);

I canโ€™t evaluate these parameters very well, because they all work in my tests, because it calibrationValueturns out to be negative (this is a calibration parameter that is specific to each device, and therefore can be positive on some devices), so after multiplying by -2, I in the end, I keep a positive value, and thus, in fact, I do not encounter the problem that I expect in my tests.

Your help is much appreciated even if it just says "you think too much."

+5
source share
4 answers

, -16 () 16 : "0xFFF0", 33 : "0xFFFFFFF0". - , , , . 16 , , 1 . , 32- , unsigned 32, :

  Int32 calreg= -2L * calibrationValue;
   CTRl_REG = (Uint32)calreg;

0s , 0xFFFF .

+4

unsigned int int 2 ints.

, " " ARM, " " " 1 ".

Rgds,

+3

, 16 , , , . , .

#define CTRL_REG   *((volatile signed int *)0x4000D008u)
short calibrationValue;

CTRL_REG = -2 * calibrationValue;

, CTRL_REG .

0
#define SS2L(sh,sl) ( ((sh) <<16) | ((sl) & 0xffff) )

16- 32-.

; int . , .

0

All Articles