Série: C++
cpp
85 linhas
· Atualizado 2026-06-15
bitwise.cpp
C++/Part1_基礎入門/Ch02_變數與資料型別/bitwise.cpp
// ============================================================
// Ch02 - 位元運算子、constexpr、固定寬度型別、cast 示範
// 編譯:g++ -std=c++17 -Wall -Wextra -o bitwise bitwise.cpp
// 執行:./bitwise
// ============================================================
#include <iostream>
#include <bitset> // std::bitset 方便以二進位顯示
#include <cstdint> // 固定寬度整數型別
// 用 bitset 把整數印成 8 位元二進位字串
static std::string toBinary8(unsigned int value) {
return std::bitset<8>(value).to_string();
}
int main() {
// ══════════════════════════════════════════
// 1. 六個位元運算子
// ══════════════════════════════════════════
std::cout << "=== 位元運算子 ===" << std::endl;
unsigned int a = 0b1100; // 12
unsigned int b = 0b1010; // 10
std::cout << "a = " << toBinary8(a) << " (" << a << ")" << std::endl;
std::cout << "b = " << toBinary8(b) << " (" << b << ")" << std::endl;
std::cout << "a & b = " << toBinary8(a & b) << " (" << (a & b) << ") AND" << std::endl;
std::cout << "a | b = " << toBinary8(a | b) << " (" << (a | b) << ") OR" << std::endl;
std::cout << "a ^ b = " << toBinary8(a ^ b) << " (" << (a ^ b) << ") XOR" << std::endl;
std::cout << "~a = " << toBinary8(~a) << " NOT(低 8 位)" << std::endl;
// ══════════════════════════════════════════
// 2. 位移運算子(乘除 2 的次方)
// ══════════════════════════════════════════
std::cout << "\n=== 位移運算子 ===" << std::endl;
int x = 5;
std::cout << "5 << 1 = " << (x << 1) << "(左移 1 位 = ×2)" << std::endl; // 10
std::cout << "5 << 3 = " << (x << 3) << "(左移 3 位 = ×8)" << std::endl; // 40
std::cout << "40 >> 2 = " << (40 >> 2) << "(右移 2 位 = ÷4)" << std::endl; // 10
// ══════════════════════════════════════════
// 3. 位元旗標(flags)的實際應用
// ══════════════════════════════════════════
std::cout << "\n=== 位元旗標 ===" << std::endl;
constexpr unsigned int READ = 1u << 0; // 0b001
constexpr unsigned int WRITE = 1u << 1; // 0b010
constexpr unsigned int EXECUTE = 1u << 2; // 0b100
unsigned int permission = 0;
permission |= READ; // 開啟讀取
permission |= WRITE; // 開啟寫入
std::cout << "權限位元:" << toBinary8(permission) << std::endl;
std::cout << "可讀取? " << std::boolalpha << static_cast<bool>(permission & READ) << std::endl;
std::cout << "可執行? " << static_cast<bool>(permission & EXECUTE) << std::endl;
permission &= ~WRITE; // 關閉寫入
std::cout << "關閉寫入後:" << toBinary8(permission) << std::endl;
// ══════════════════════════════════════════
// 4. constexpr 編譯期常數
// ══════════════════════════════════════════
std::cout << "\n=== constexpr ===" << std::endl;
constexpr int BUFFER_SIZE = 8 * 1024; // 編譯期就算好
int buffer[BUFFER_SIZE] = {}; // 陣列大小需要編譯期常數
std::cout << "BUFFER_SIZE = " << BUFFER_SIZE
<< ",buffer 第一個元素 = " << buffer[0] << std::endl;
// ══════════════════════════════════════════
// 5. 固定寬度型別與 static_cast
// ══════════════════════════════════════════
std::cout << "\n=== 固定寬度型別 ===" << std::endl;
uint8_t byteVal = 200;
int32_t big = 2000000000;
// 注意:uint8_t 本質是 char,直接輸出會被當字元,需轉成 int
std::cout << "uint8_t 值:" << static_cast<int>(byteVal) << std::endl;
std::cout << "int32_t 值:" << big << "(" << sizeof(big) << " bytes)" << std::endl;
return 0;
}
Artigos relacionados
C++
c
Atualizado 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/deviceAlpha.h).
Ler artigo →
C++
c
Atualizado 2026-07-21
finalproject.c
finalproject.c — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.c).
Ler artigo →
C++
cpp
Atualizado 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
Ler artigo →
C++
c
Atualizado 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/deviceAlpha.h).
Ler artigo →
C++
c
Atualizado 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
Ler artigo →
C++
cpp
Atualizado 2026-07-21
lab8.cpp
lab8.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.cpp).
Ler artigo →