~ operator in C

后端 未结 5 1444
星月不相逢
星月不相逢 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:25

    it's a bitwise operator (one's complement ) that works this way :

    ~00000101 // 5
    =11111010 // 250
    

    so the 1s become 0s and vise versa but in some cases the sign bit is activated and you'll get unexpected results

提交回复
热议问题