qfedu-basic-level/day6/d7.cpp

19 lines
578 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
按位取反时,如果是有符号的,包含符号位。
将0取为1 将1取为0
【小技巧】数据的取反先加1再反符号反值。
如: -9的按位取反的结果 8, 10的按位取反的结果 -11、、
*/
#include <iostream>
using namespace std;
int main()
{
int n = 10;
cout << "~10 = " << ~n << endl; // -11 : ~ 位非运算符: 二进制取反: 0000 1010 => 1111 0101 => -11
n = -9;
cout << "~(-9) = " << ~n << endl; // 8 : ~ 位非运算符: 二进制取反: 1000 1001 => 0111 0110 => 8
return 0;
}