Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Program to convert KiloBytes to Bytes and Bits
Next article icon

Program to convert KiloBytes to Bytes and Bits

Last Updated : 30 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given Number of Kilobytes. The task is to Convert them into Bytes and Bits.
Bit: A Bit is the basic unit in computer information and has only two different values, normally defined as a 0 or 1. These values can be interpreted as on or off, yes or no, true or false, etc. It just depends on the binary code.
Add 1 bit, double the number of patterns.

1 bit - 2 patterns i.e 0 and 1
2 bits - 4 patterns i.e 00, 01, 10, 11
3 bits - 8 patterns i.e 000, 001, 010, 011, 100, 101, 110, 111 
 

Mathematically: n bits yields 2n patterns.
Bytes: A Byte is just 8 Bits and is the smallest unit of memory that can be addressed in many computer systems.

Important points about Bytes:

  • One byte can store one character, e.g. 'A' or 'x' or '$' etc.
  • 1 byte i.e 8 bits can make 256 different patterns.
  • One byte can hold a number between 0 and 255.
  • Different Form:-
    1. Kilobyte, KB, about 1 thousand bytes.
    2. Megabyte, MB, about 1 million bytes.
    3. Gigabyte, GB, about 1 billion bytes.
    4. Terabyte, TB, about 1 trillion bytes

Examples:

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

Below is the program to convert KilloBytes to Bytes and Bits:

C++
// C++ implementation of above program #include <bits/stdc++.h> 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
<?php // PHP implementation of above program   // Function to calculates the bits  function Bits($kilobytes)  {      $Bits = 0;       // calculates Bits      // 1 kilobytes(s) = 8192 bits      $Bits = $kilobytes * 8192;       return $Bits;  }   // Function to calculates the bytes  function Bytes($kilobytes)  {      $Bytes = 0;       // calculates Bytes      // 1 KB = 1024 bytes      $Bytes = $kilobytes * 1024;       return $Bytes;  }   // Driver code  $kilobytes = 1;   echo $kilobytes; echo (" Kilobytes = "); echo Bytes($kilobytes); echo (" Bytes and "); echo Bits($kilobytes); echo (" Bits.");  // This code is contributed // by Shivi_Aggarwal  ?> 
JavaScript
<script>  // Javascript implementation of above program   function Bits(kilobytes) {     var Bits = 0;      // Calculates Bits     // 1 kilobytes(s) = 8192 bits     Bits = kilobytes * 8192;      return Bits; }  // Function to calculates the bytes function Bytes(kilobytes) {     var Bytes = 0;      // Calculates Bytes     // 1 KB = 1024 bytes     Bytes = kilobytes * 1024;      return Bytes; }  // Driver code var kilobytes = 1;  document.write(kilobytes + " Kilobytes = " +                 Bytes(kilobytes) + " Bytes and " +                 Bits(kilobytes) + " Bits.");                 // This code is contributed by akshitsaxenaa09                 </script> 

Output:
1 Kilobytes = 1024 Bytes and 8192 Bits.

 

Time Complexity: O(1)

Auxiliary Space: O(1)


Next Article
Program to convert KiloBytes to Bytes and Bits

N

Naman_Garg
Improve
Article Tags :
  • Mathematical
  • Computer Science Fundamentals
  • DSA
Practice Tags :
  • Mathematical

Similar Reads

    Gray to Binary and Binary to Gray conversion
    Binary Number is the default way to store numbers, but in many applications, binary numbers are difficult to use and a variety of binary numbers is needed. This is where Gray codes are very useful. Gray code has a property that two successive numbers differ in only one bit because of this property g
    10 min read
    Swap every two bits in bytes
    Swap all the pair of bits in a byte. Before swapping: 11-10-11-01 After swapping: 11-01-11-10Examples: Input : 00000010 Output : 00000001 Input : 00000100 Output : 00001000 Approach: x = ((x & 0x55555555) >> 1) | ((x & 0xAAAAAAAA) <> 1 extracts the high bit position and shifts it
    3 min read
    Python Bin | Count total bits in a number
    Given a positive number n, count total bit in it. Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 We have existing solution for this problem please refer Count total bits in a number link. Approach#1: We can solve this problem quick
    3 min read
    Count total bits in a number
    Given a positive number n, count total bit in it.Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 Method 1 (Using Log) The log2(n) logarithm in base 2 of n, which is the exponent to which 2 is raised to get n only integer and we add
    7 min read
    Count set bits in an integer
    Write an efficient program to count the number of 1s in the binary representation of an integer.Examples : Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 13 is 1101 and has 3 set bits[Naive Approach] - One by One CountingTh
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences