I am trying to have C interpret an inverted unsigned integer as an unsigned integer. How can I invert the bits of a unsigned number without C interpreting it as two’s complement?
Whenever I have an unsigned integer and I use it as a operand with the bitwise NOT operator it evaluates to a negative number as according to two’s complement. However by definition I am using an unsigned integer, a number that should not have a sign.
Below has my sandboxed code. I am currently using GCC version 9.1.1.
#include <stdio.h>
#include <stdint.h>
int main(){
uint8_t x = 1;//0000 0001
printf("NOT(%d) == %d", x, ~x);//1111 1110
}
I want the output of 254 (1111 1110) but C interprets the value as two’s complement so I get -2. Is there a way I can get C to not interpret the inverted byte as two’s complement?