C 言語の not 演算子には2種類ある。 ~
と !
。論理学では ~
を否定で使うことが多いけれど C 言語での論理否定は !
、 ~
はビットごとの否定。
// bitwise-not.c
#include <stdbool.h>
int main() { return ~false; }
$ make bitwise-not && ./bitwise-not; echo $? # ==> 255
false
の定義は 0
(int) のため、これをビットごとに否定すると -1
になる。
main
の戻り値 int はシェルで1バイトに切り詰められるため exit code を $?
で参照して表示すると 255
になる。
// logical-not.c
#include <stdbool.h>
int main() { return !false; }
$ make logical-not && ./logical-not; echo $? # ==> 1
論理否定をとると 1
が表示される。
ただしわたしの記憶が確かならば C 言語での false
の論理否定は 0
でない値という定義だったはずで、処理系依存。
ここでは ~
と !
の否定の結果の値は異なることがある、ということが確かめられたという程度の話。