Confusion with C ~ operator (bitwise Not) and comparing char variables

给你一囗甜甜゛ 提交于 2019-11-29 13:48:40

Because integral promotion causes the math on the right side to be done as int. If you assigned the result back to a char like unsigned char Z = ~X those upper bits would be truncated off again and Y == Z.

The ~ operator causes its operands to be promoted to int before being complemented. ~15 is not 240 but some other value, depending on the size of int.

Just use if (X + Y == 255) and it should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!