CHAR_BIT เป็น Macro Constant ที่กำหนดใน Header <climits> (หรือ <limits.h>) ระบุจำนวนบิตใน 1 ไบต์ (Byte)
ค่ามาตรฐาน
- บนระบบเกือบทุกตัวในปัจจุบัน: 8 bits/byte
- C++ Standard กำหนดขั้นต่ำ: 8 bits
- ระบบเก่า/Embedded บางตัวอาจเป็น 9, 16, 32 bits/byte
การใช้งาน
#include <iostream>
#include <climits> // หรือ <limits.h>
#include <bitset>
using namespace std;
int main() {
cout << "CHAR_BIT = " << CHAR_BIT << endl;
cout << "sizeof(char) = " << sizeof(char) << " byte" << endl;
cout << "Bits in char = " << CHAR_BIT * sizeof(char) << endl;
// แสดงขนาดของ Type ต่างๆ ใน Bits
cout << "\nType sizes in bits:" << endl;
cout << "char: " << CHAR_BIT * sizeof(char) << endl;
cout << "short: " << CHAR_BIT * sizeof(short) << endl;
cout << "int: " << CHAR_BIT * sizeof(int) << endl;
cout << "long: " << CHAR_BIT * sizeof(long) << endl;
cout << "long long: " << CHAR_BIT * sizeof(long long) << endl;
cout << "float: " << CHAR_BIT * sizeof(float) << endl;
cout << "double: " << CHAR_BIT * sizeof(double) << endl;
cout << "pointer: " << CHAR_BIT * sizeof(void*) << endl;
return 0;
}
ผลลัพธ์ทั่วไป (64-bit System)
CHAR_BIT = 8 sizeof(char) = 1 byte Bits in char = 8 Type sizes in bits: char: 8 short: 16 int: 32 long: 64 long long: 64 float: 32 double: 64 pointer: 64
ตัวอย่างประยุกต์: แปลง Integer เป็น Binary String
#include <iostream>
#include <climits>
#include <stack>
using namespace std;
string toBinary(int x) {
if (x == 0) return "0";
bool negative = x < 0;
unsigned int n = negative ? -static_cast<unsigned int>(x) : x;
stack<int> bits;
int totalBits = CHAR_BIT * sizeof(int);
for (int i = 0; i < totalBits; i++) {
bits.push(n & 1);
n >>= 1;
}
string result;
bool leading = true;
while (!bits.empty()) {
int bit = bits.top(); bits.pop();
if (leading && bit == 0) continue;
leading = false;
result += (bit ? '1' : '0');
}
if (negative) result = "-" + result;
return result.empty() ? "0" : result;
}
int main() {
int testValues[] = {0, 1, 5, 28, -28, 255, -1, INT_MAX, INT_MIN};
for (int val : testValues) {
cout << val << " = " << toBinary(val) << endl;
}
return 0;
}
ผลลัพธ์
0 = 0 1 = 1 5 = 101 28 = 11100 -28 = -11100 255 = 11111111 -1 = -11111111111111111111111111111111 2147483647 = 1111111111111111111111111111111 -2147483648 = -10000000000000000000000000000000
CHAR_BIT กับ std::numeric_limits (Modern C++)
#include <iostream>
#include <limits>
#include <climits>
using namespace std;
int main() {
// C-style Macro
cout << "CHAR_BIT = " << CHAR_BIT << endl;
// C++ Style (Type-safe)
cout << "numeric_limits<char>::digits = "
<< numeric_limits<char>::digits << endl; // Value bits (exclude sign)
cout << "numeric_limits<signed char>::digits = "
<< numeric_limits<signed char>::digits << endl;
cout << "numeric_limits<unsigned char>::digits = "
<< numeric_limits<unsigned char>::digits << endl;
// CHAR_BIT == digits + (is_signed ? 1 : 0)
return 0;
}
ผลลัพธ์
CHAR_BIT = 8 numeric_limits::digits = 7 numeric_limits ::digits = 7 numeric_limits ::digits = 8
การประยุกต์ใช้งาน
1. Bit Manipulation แบบ Portable
// สร้าง Mask ที่มี 1 บิตเดียวที่ตำแหน่ง n
#define BIT_MASK(n) (1 << (n)) // สมมติ CHAR_BIT=8
// Portable Version
constexpr unsigned int bitMask(int n) {
return 1u << n;
}
2. คำนวณขนาด Buffer สำหรับ Bits
// ต้องการเก็บ N bits ต้องใช้กี่ Bytes
constexpr size_t bitsToBytes(size_t bits) {
return (bits + CHAR_BIT - 1) / CHAR_BIT;
}
// ใช้กับ std::vector<unsigned char> เป็น Bit Array
vector<unsigned char> bitArray(bitsToBytes(1000));
3. Serialization / Network Programming
// เขียน Integer เป็น Bytes (Endianness Aware)
void writeInt32(ostream& os, int32_t val) {
for (int i = 0; i < 32; i += CHAR_BIT) {
os.put(static_cast<char>((val >> i) & 0xFF));
}
}
ข้อจำกัด
- CHAR_BIT เป็น Compile-time Constant (Macro)
- ไม่สามารถเปลี่ยนได้ขณะ Runtime
- บน Platform ใด Platform หนึ่ง ค่านี้คงที่ตลอด
- C++ ไม่รับประกัน CHAR_BIT == 8 (แต่ทุก Platform ทำเป็น 8)