"The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set)."- In 2's complement, a value composed of all-bits-clear represents a 0.
- In 1's complement, a value composed of all-bits-clear represents a 0.
- In 2's complement, a value composed of all-bits-set represents a -1.
- In 1's complement, a value composed of all-bits-set represents a -0.
Google can also help.
Code:
#include <stdio.h>
#include <limits.h>
char *bitdisp(char *dst, unsigned int value)
{
char *start = dst;
unsigned int bit;
for ( bit = ~(~0U >> 1) + 1; bit; bit >>= 1 )
{
*dst++ = (value & bit) ? '1' : '0';
}
*dst = '\0';
return start;
}
int main ( void )
{
const char fmt[] = "The value represented by the bits \"%s\" is %d.\n";
int u = 0;
char s [ CHAR_BIT * sizeof(u) + 1 ];
printf(fmt, bitdisp(s,u), u);
u = ~0;
printf(fmt, bitdisp(s,u), u);
return 0;
}
/* my output
The value represented by the bits "00000000000000000000000000000000" is 0.
The value represented by the bits "11111111111111111111111111111111" is -1.
*/