C++程式:將千位元組轉換為位元組和位元
給定輸入為千位元組,任務是將給定的輸入轉換為位元組數和位元數。
位元 - 在計算機中,位元是最小的單位,用兩個整數 0 和 1 表示,計算機中的所有資訊都以這兩個數字的序列進行處理。
N 位 = 2 ^ N 種模式,其中 N 可以是任何從 1 開始的整數。
位元組 - 在計算機中,位元組用 8 個位元表示。位元組能夠儲存一個字元到 0 到 255 之間的數字。
1 位元組 = 8 位元
這意味著 2 ^ 8 種模式,等於 256
位元組有多種形式 -
1 千位元組(KB) = 1024 位元組
1 兆位元組(MB) = 1048576 位元組
1 千兆位元組 = 1073741824 位元組
示例
Input 1-: kilobytes = 10 Output -: 10 Kilobytes = 10240 Bytes and 81920 Bits Input 2-: kilobytes = 1 Output -: 1 Kilobytes = 1024 Bytes and 8192 Bits
下面使用的方案如下 -
- 以千位元組為單位輸入資料
應用公式將千位元組轉換為位元組
位元組 = 千位元組 * 1024
應用公式將千位元組轉換為位元
位元 = 千位元組 * 8192
演算法
Start Step 1-> Declare function to convert into bits long Bits(int kilobytes) set long Bits = 0 set Bits = kilobytes * 8192 return Bits step 2-> Declare function to convert into bytes long Bytes(int kilobytes) set long Bytes = 0 set Bytes = kilobytes * 1024 return Bytes step 3-> In main() declare int kilobytes = 10 call Bits(kilobytes) call Bytes(kilobytes) Stop
示例
#include <bits/stdc++.h>
using namespace std;
//convert into bits
long Bits(int kilobytes) {
long Bits = 0;
Bits = kilobytes * 8192;
return Bits;
}
//convert into bytes
long Bytes(int kilobytes) {
long Bytes = 0;
Bytes = kilobytes * 1024;
return Bytes;
}
int main() {
int kilobytes = 10;
cout << kilobytes << " Kilobytes = " << Bytes(kilobytes) << " Bytes and " << Bits(kilobytes) << " Bits";
return 0;
}輸出
如果我們執行以上程式碼,它將生成以下輸出
10 Kilobytes = 10240 Bytes and 81920 Bits
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP