From a53db5f59b9996f42f5e0ed033199b82cceb7018 Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 19 Jun 2023 16:20:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=8D=E8=BF=90=E7=AE=97=E7=9A=84=E7=BB=BC?= =?UTF-8?q?=E5=90=88=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day6/case1.cpp | 15 +++++++++++++++ day6/case2.cpp | 16 ++++++++++++++++ day6/case3.cpp | 15 +++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 day6/case1.cpp create mode 100644 day6/case2.cpp create mode 100644 day6/case3.cpp diff --git a/day6/case1.cpp b/day6/case1.cpp new file mode 100644 index 0000000..9f82bac --- /dev/null +++ b/day6/case1.cpp @@ -0,0 +1,15 @@ +// 案例1: data 为 1 字节 将 data 的第 3, 4 位清 0 其他位保持不变 +#include +#include + +using namespace std; + +int main() +{ + char n = 0b01011011; // 0x5b = 0101 1011 + cout << "原值: " << bitset<8>(n) << endl; + // 1000 | 10000 = 0001 1000 ; ~00011000 = 11100111 + n &= ~(0x1 << 3 | 0x1 << 4); + cout << "结果: " << bitset<8>(n) << endl; + return 0; +} \ No newline at end of file diff --git a/day6/case2.cpp b/day6/case2.cpp new file mode 100644 index 0000000..456be4a --- /dev/null +++ b/day6/case2.cpp @@ -0,0 +1,16 @@ +// 案例2: data 为 1 字节, 将 data 的第 5, 6 为置 1 其他位保持不变 +#include +#include + +using namespace std; + +int main() +{ + char n = 0b10001011; // 0x8b = 1000 1011 + cout << "原值: " << bitset<8>(n) << endl; + + // 1000 | 10000 = 0001 1000 + n |= (0x1 << 5 | 0x1 << 6); // 将第 5, 6 位置为 1 + cout << "结果: " << bitset<8>(n) << endl; + return 0; +} \ No newline at end of file diff --git a/day6/case3.cpp b/day6/case3.cpp new file mode 100644 index 0000000..aa75cea --- /dev/null +++ b/day6/case3.cpp @@ -0,0 +1,15 @@ +// 案例3: data 为 1 字节, 将 data 的第 3, 4 位清0, 5, 6 置 1 其他位保持不变 +#include +#include + +using namespace std; + +int main() +{ + char data = 0b10011011; + cout << "原值: " << bitset<8>(data) << endl; + + data = (data & ~(0x1 << 3 | 0x1 << 4)) | (0x1 << 5 | 0x1 << 6); + cout << "结果: " << bitset<8>(data) << endl; + return 0; +} \ No newline at end of file