ที่นี่เราจะดูว่าถ้ามีตัวเลข ก็จะหาค่าของ Most Significant Bit ที่เป็น 1 ได้อย่างไร ค่านี้คือกำลังของ 2 เช่นถ้าตัวเลขคือ 10 ค่า MSB จะเป็น 8
เราต้องหาตำแหน่งของ MSB แล้วหาค่าของตัวเลขที่มีบิตตั้งอยู่ที่ตำแหน่ง k
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
int msbBitValue(int n) {
int k = (int)(log2(n));
return (int)(pow(2, k));
}
int main() {
int n = 150;
cout << "MSB bit value is: " << msbBitValue(n);
}
ผลลัพธ์
MSB bit value is: 128