
bitwise negation
hi ,
my question is related to C 's bitwise negation (~) when i do bitwise negation to 0(zero) it gives 1 as a result . i want to how internally machine ( using 2's compliment and 1's compliment )computes can anybody please help with an example (using bit representation of number)
thanx in advance
Code:
Code:
int main() { int a=~0; printf("%s",a); }

"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 allbitsclear represents a 0.
 In 1's complement, a value composed of allbitsclear represents a 0.
 In 2's complement, a value composed of allbitsset represents a 1.
 In 1's complement, a value composed of allbitsset 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.
*/