I’m using the low level register access SetBits, but appear to have got a problem setting bit 31 or clearBits 31
PINSEL0.SetBits(3 << 30);
Error 2 Argument 1: cannot convert from ‘int’ to ‘uint’
PINSEL0 has been defined as public const uint. I can write the value without an error
PINSEL0.Write(0xC0000000);
But this will also clobber any other set bits. I have a work around, reading the register then or it with 0xC0000000 then write it back.
Second work around is to create a uint and give it the value 3.
uint bits = 3;
PINSEL0.SetBits(bits << 30);
Is it a known problem? Or am i doing something wrong?
This is normal, the compiler treats integral literals as signed ints, therefore ‘3 << 30’ is a signed integer operation.
Due to a common compiler optimization known as constant folding the expression is evaluated at compile time, resulting in a negative number (the high bit is set) and since converting this to a uint will result in loss of information, the compiler will not perform an automatic type conversion and requires an explicit cast.
An alternative workaround, which will still take advantage of the constant folding optimization is the following
@ Davef, actually I was so focused on explaining the whys and wherefores that I forgot to give you another alternative. You can also override the compilers intent to treat a constant as a signed int by adding the ‘u’ postfix.
PINSEL0.SetBits(3u << 30);
The above tells the compiler to treat the 3 as an unsigned int.