Compare commits

..

3 Commits

Author SHA1 Message Date
flykhan 6f86054fb6 添加取反说明技巧 2023-06-19 15:25:02 +08:00
flykhan 428eaec33c 按位取反 2023-06-19 15:23:37 +08:00
flykhan ce8fa14892 位与&&位或 2023-06-19 15:14:11 +08:00
2 changed files with 45 additions and 0 deletions

26
day6/d6.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
int main()
{
// 定义 Linux 的文件权限
int r = 4, w = 2, x = 1;
int n = 0;
// 为 n 值添加 r 和 w 权限
n = r | w; // 6: | 位或运算符
cout << "n = " << n << endl; // 6
// 为 n 值添加 x 权限
n |= x; // 7
cout << "n = " << n << endl; // 7
// 验证 n 是否包含 w 权限
if (n & w == w) // 7 & 2 == 2: & 位与运算符
{
cout << "n 包含 w 权限" << endl;
}
return 0;
}

19
day6/d7.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
01 10
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;
}