Following situation:
I have macros for storing the complement of a variable together with its original value within a structure. With another macro I want to check if the original value is equal to the complement of the stored complement value. But strangely I do not get the results I may expect. Simplifying the operation, this results in following situation:
#include <stdbool.h>
#include <stdint.h>
#define CHECKVAR(var__, compl__) ((var__^compl__)==-1);
int main()
{
uint8_t var;
uint8_t complementvar;
var=3;
complementvar=~var;
bool checkOK=CHECKVAR(var,complementvar); /*-> I expect that all bits are set and hereby it is equal to "-1", but instead i get "false" */
return 0;
}
My expectation would be (e.g. on a 32bit system :
- after the complement operation
complementvar
has the value0xfffffffc
(after integer promotion caused by the ~ operator to int and assigning the lvalue tocomplementvar
by implicitly casting it down touint8
). - now to the checking macro:
the bitwise operation leads to an integer promotion on both sides?:
- 2a).
var: 0000 0000 0000 0000 0000 0000 0000 0011
;complementvar: 1111 1111 1111 1111 1111 1111 1111 1100
- 2b)\ now XOR-ing both var and complementvar shoudl result in
1111 1111 1111 1111 1111 1111 1111 1111
- 2a).
- checking the results of the XOR-operation together with -1 (represented as an integer)
1111 1111 1111 1111 1111 1111 1111 1111==1111 1111 1111 1111 1111 1111 1111 1111
shall result in true, but instead i always receive false, because the result of XOR-operation (strangely for me) is 0x000000ff?
What compiler am I using? It's the MSVC++11, but actually the solution should be as compiler independent as possible. As I have to be portable too, would the results be different with a C compiler?