~ operator in C

后端 未结 5 1445
星月不相逢
星月不相逢 2021-01-28 00:33

The output of this program is -13. I have never fully understood ~ operator in C. Why does it give -13 as output? How to limit ~ operator to just 4 bits of a number?

<         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-28 01:16

    The ~ operator acts as a binary NOT, that is, it flips all the bits in the number. Negative numbers in signed integers are stored as two's complement.

提交回复
热议问题