c - Why is !0 equal to 1 and not -1? -
the following code
printf("!%d = %d\n", 0, !0); printf("!%d = %d\n", 1, !1); printf("!%d = %d\n", -1, !-1);
gives
!0 = 1 !1 = 0 !-1 = 0
now, considering 0 = 0x00000000
, shouldn't !0 = 0xffffffff = -1
(for signed representation)? messes using int
/ long
in bitfield , inverting @ once.
what reason behind this? avoid !1
considered boolean true
?
the reason in standard c, has been specified operators returning boolean return either 1 or 0. !0 calculates logical not of 0, i.e. 1. logical not of 1 0.
what want use bitwise not operator, i.e. ~0
should 0xffffffff == -1
.
Comments
Post a Comment