📜  将千字节转换为字节和位的程序

📅  最后修改于: 2021-04-22 07:25:33             🧑  作者: Mango

给定的千字节数。任务是将它们转换为字节和位。
位:位是计算机信息中的基本单位,只有两个不同的值,通常定义为0或1 。这些值可以解释为on或off,yes或no,true或false等。它仅取决于二进制代码。
加1位,将模式数量加倍。

数学上:n位产生2 n个模式。
字节:字节仅为8位,是可在许多计算机系统中寻址的最小内存单元。

关于字节的要点:

  • 一个字节可以存储一个字符,例如’A’或’x’或’$’等。
  • 1个字节(即8位)可以构成256个不同的模式。
  • 一个字节可以容纳0到255之间的数字。
  • 不同形式:-
    1. 千字节KB,大约一千个字节。
    2. MB,大约100万字节。
    3. GB,大约10亿字节。
    4. TB TB,大约1万亿字节

例子:

Input: kilobytes = 1
Output: 1 Kilobytes = 1024 Bytes and 8192 Bits.

Input: kilobytes = 8
Output: 8 Kilobytes = 8192 Bytes and 65536 Bits.

下面是将千字节转换为字节和位的程序:

C++
// C++ implementation of above program
#include 
using namespace std;
 
// Function to calculates the bits
long Bits(int kilobytes)
{
    long Bits = 0;
 
    // calculates Bits
    // 1 kilobytes(s) = 8192 bits
    Bits = kilobytes * 8192;
 
    return Bits;
}
 
// Function to calculates the bytes
long Bytes(int kilobytes)
{
    long Bytes = 0;
 
    // calculates Bytes
    // 1 KB = 1024 bytes
    Bytes = kilobytes * 1024;
 
    return Bytes;
}
 
// Driver code
int main()
{
    int kilobytes = 1;
 
    cout << kilobytes << " Kilobytes = "
         << Bytes(kilobytes) << " Bytes and "
         << Bits(kilobytes) << " Bits.";
    return 0;
}


Java
// Java implementation of above program
 
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
 
 
class GFG
{
  
// Function to calculates the bits
static BigInteger Bits(int kilobytes)
{
    BigInteger  Bits = new BigInteger("0");
  
    // calculates Bits
    // 1 kilobytes(s) = 8192 bits
     
    BigInteger kilo = BigInteger.valueOf(kilobytes);
    Bits = kilo.multiply(BigInteger.valueOf(8192));
  
    return Bits;
}
  
// Function to calculates the bytes
static BigInteger Bytes(int kilobytes)
{
    BigInteger Bytes = new BigInteger("0");
  
    // calculates Bytes
    // 1 KB = 1024 bytes
     
   BigInteger kilo = BigInteger.valueOf(kilobytes);
   Bytes = kilo.multiply(BigInteger.valueOf(1024));
  
    return Bytes;
}
  
// Driver code
public static void main(String args[])
{
    int kilobytes = 1;
  
    System.out.print(kilobytes + " Kilobytes = "
         + Bytes(kilobytes) + " Bytes and "
         + Bits(kilobytes) + " Bits.");
}
}


Python3
# Python implementation of above program
 
# Function to calculates the bits
def Bits(kilobytes) :
 
    # calculates Bits
    # 1 kilobytes(s) = 8192 bits
    Bits = kilobytes * 8192
 
    return Bits
 
# Function to calculates the bytes
def Bytes(kilobytes) :
 
    # calculates Bytes
    # 1 KB = 1024 bytes
    Bytes = kilobytes * 1024
 
    return Bytes
 
# Driver code
if __name__ == "__main__" :
 
    kilobytes = 1
 
    print(kilobytes, "Kilobytes =",
    Bytes(kilobytes) , "Bytes and",
    Bits(kilobytes), "Bits")
 
# This code is contributed by ANKITRAI1


C#
// C# implementation of above program
using System;
 
class GFG
{
     
// Function to calculates the bits
static long Bits(int kilobytes)
{
    long Bits = 0;
 
    // calculates Bits
    // 1 kilobytes(s) = 8192 bits
    Bits = kilobytes * 8192;
 
    return Bits;
}
 
// Function to calculates the bytes
static long Bytes(int kilobytes)
{
    long Bytes = 0;
 
    // calculates Bytes
    // 1 KB = 1024 bytes
    Bytes = kilobytes * 1024;
 
    return Bytes;
}
 
// Driver code
static public void Main ()
{
    int kilobytes = 1;
 
    Console.WriteLine (kilobytes +" Kilobytes = "+
                 Bytes(kilobytes) + " Bytes and "+
                  Bits(kilobytes) + " Bits.");
}
}
 
// This code is contributed by Sach_Code


PHP


Javascript


输出:
1 Kilobytes = 1024 Bytes and 8192 Bits.