Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ แปลงกิโลไบต์ (KB) เป็นไบต์ (Bytes) และบิต (Bits)


บทนำ

แปลงค่ากิโลไบต์ (KB) เป็นไบต์และบิต

หน่วยความจำ

  • 1 Bit = 0 หรือ 1 (หน่วยเล็กที่สุด)
  • 1 Byte = 8 Bits
  • 1 KB = 1024 Bytes
  • 1 MB = 1,048,576 Bytes
  • 1 GB = 1,073,741,824 Bytes

สูตร

  • Bytes = KB × 1024
  • Bits = KB × 8192 (เพราะ 1024 × 8)

ตัวอย่าง

  • 10 KB → 10,240 Bytes, 81,920 Bits
  • 1 KB → 1,024 Bytes, 8,192 Bits

ตัวอย่างโค้ด C++

#include <bits/stdc++.h>
using namespace std;

long Bits(int kilobytes) {
    return kilobytes * 8192L;
}

long Bytes(int kilobytes) {
    return kilobytes * 1024L;
}

int main() {
    int kilobytes = 10;
    cout << kilobytes << " Kilobytes = " << Bytes(kilobytes) 
         << " Bytes and " << Bits(kilobytes) << " Bits";
    return 0;
}

ผลลัพธ์

10 Kilobytes = 10240 Bytes and 81920 Bits