I'm trying to create the following defines, my source code is shared between an iOS app and a C++ ARM firmware.
#define ASSIGN_MIN_VAL_NB_BITS 7
#define ASSIGN_MIN_VAL_BIT_POS 1
#define ASSIGN_MIN_VAL_BIT_MASK (((2^ASSIGN_MIN_VAL_NB_BITS)-1)<<ASSIGN_MIN_VAL_BIT_POS)
I'm expecting ASSIGN_MIN_VAL_BIT_MASK to be 0b11111110, but it is not. The above 2^ seems to be the problem. How could I declare something similar ? I've tried using pow(x,y) to replace the 2^, it works but I would like to find a way to declare these define without using runtime functions (I assume pow is a runtime function).
Any idea, tip greatly appreciated.
Don't use #define
for constants in C++, use const
variables.
^
is bitwise XOR, not exponentiation. 2 to the power of x
can be represented as 1 << x
.
So in your case, the correct expression would be:
((1 << ASSIGN_MIN_VAL_NB_BITS) - 1) << ASSIGN_MIN_VAL_BIT_POS