BEST SITE FOR WEB DEVELOPERS
Java programming language. Lessons for beginners from hyperskill

Ua

Units of information


Theory

Throughout life, we always find something to measure: the amount of food we need to cook for the family, the length and width of that couch you want to put in the room, our weight and height. The latter is especially exciting: it's really cool to learn that in just one year you grew by a full 2 inches!

Each measurement requires an instrument and its own unit of measurement. For example, body weight is measured with scales in kilograms (or pounds), time is measured with clocks in seconds, etc. But how does one measure information?


§1. Bit: the smallest unit of information

The information entered into the computer should be specific and unambiguous. For a long time, people have used ciphers. The simplest and most convenient of them were digital. Any information from the names of flowers to the days of the week can be presented in the form of numbers. When processed using a traditional computer, data is encoded numerically and stored in files, ultimately represented by the electrical signals that dictate the computer's operational speed.

For the convenience of distinguishing, signals of two levels are used in classical electronic computers. One of them corresponds to the number 1, and the other to 0. Any letter, sound, or image on a computer is represented by a set of numbers. The numbers 1 and 0 are called binary. These are the symbols that make up the language understood and used by the computer. Any information on the computer is represented by binary digits: 1, meaning "there is a signal" or "there is a high signal" and 0, meaning "no signal" or "there is a low signal".

The smallest unit of information is the bit (b).

Each digit of the machine binary code carries the amount of information equal to one bit. It can take only one of two values: either 1 or 0. It is very inconvenient to measure information in bits because the numbers come out too big.


§2. Byte: a sequence of eight bits

Like people do not consider the mass of ships in grams, larger and hence more convenient units were invented for measuring information as well.

The processing of information takes place in the processor. This is a device that can work with several bits at once (8, 16, 32, 64, ...). Most computers nowadays process 8 bits of information simultaneously, so we needed a new unit of measurement which was called a byte (B), which means 8 bits.

Bit marks are easily confused with byte marks. Note that the abbreviations for bit numbers use the lowercase letter "b", while bytes are denoted using the capital "B".


§3. Large units of information

Given the advanced capabilities of modern computers, the storage capacity has expanded to accommodate significantly larger units of information, surpassing the traditional byte scale.

The computer industry has historically used the units kilobyte, megabyte, and gigabyte in at least two slightly different measurement systems, which are slightly contradictory to each other.

  • The first one is a decimal-based system, which uses bytes in powers of ten: kilobyte (10^3 bytes), megabyte (10^6 bytes), gigabyte (10^9 bytes), and so on. These units are used by the International System of Units (SI).
  • The second one is a binary-based system that uses bytes in powers of two: kibibyte (2^10 bytes), mebibyte (2^20 bytes), gibibyte (2^30 bytes), and so on. This system was actively used to describe computer memory.

To resolve this confusion, the International Electrotechnical Commission (IEC) suggested using prefixes kilo, mega, and giga only for the decimal-based system and using new prefixes kibi, mebi, and gibi for the binary-based system. Here bi means binary: kibibyte is kilo binary byte.

Here is a table with commonly used units of information according to modern international standards.

SI metric Symbol Powers of ten IEC metric Symbol Powers of two
Kilobyte kB 10^3 B (1000 B) Kibibyte KiB 2^10 B (or 1024 B)
Megabyte MB 10^6 B (1000 kB) Mebibyte MiB 2^20 B (or 1024 KiB)
Gigabyte GB 10^9 B (1000 MB) Gibibyte GiB 2^30 B (or 1024 MiB)
Terabyte TB 10^12 B (1000 GB) Tebibyte TiB 2^40 B (or 1024 GiB)
Petabyte PB 10^15 B (1000 TB) Pebibyte PiB 2^50 B (or 1024 TiB)

Of course, not all units of measurement are listed here. We hope this classification will not confuse you.

Note that some people and organizations still prefer kilo, mega, and giga to describe powers of two. In this course, we follow the recommendations of the IEC and use the modern prefixes kibi, mebi, and gibi.


§4. Measurement units conversion

To strengthen your newly obtained knowledge, let's look at the solution of a rather typical problem where you need to convert 1 GiB to KiB. When we convert bigger units into smaller ones, we need to resort to an arithmetic operation called multiplication:

1 GiB = 1KiB * 1024 * 1024 = 1048576 KiB

Accordingly, when you need to convert small units into big ones, you use division. Let's try to convert 16384 bits to KiB:

16384 bits = (16384 / 8) / 1024 = 2 KiB

If you want to convert 1 GB to kB, you should multiply the number by a thousand twice:

1 GB = 1kB * 1000 * 1000 = 1000000 kB

Congratulations, now you have studied one of the basic topics of computer science and are ready to reach new dizzying heights of knowledge.


§5. Conclusion

To wrap-up:

  • The smallest unit of information is a bit.
  • One byte consists of 8 bits.
  • For convenience in computer science, we use binary-based units of information such as bytes, kibibytes, mebibytes, etc.
  • To convert one kibibyte to bytes, we need to multiply 1 by 210 to convert one mebibyte, we need to multiply 1 by 220, etc.

You can also view Lesson on hyperskill.org.


Practical tasks and answers

Tasks and answer options are given. The correct option is highlighted in blue color.

№1. Convert bytes into bits

Question: How many bits are there in 12 bytes?

How many bits are there in 12 bytes? If the answer is a decimal fraction, separate the parts with the dot symbol:

Answer: Enter a short text: 96


№2. From smallest to largest

Question: Sort the units of information from the smallest to the largest. The largest unit should be at the bottom of the list.

Put the items in the correct order:

  • bit
  • byte
  • kilobyte
  • megabyte

№3. Convert bits into bytes

Question: How many bytes are in 256 bits? If the answer is a decimal fraction, separate the parts with the dot symbol.

Enter a short text: 32


№4. The smallest unit of information

Question: What is the smallest unit of information called?

Select one option from the list:

  • kilobyte
  • byte
  • microbyte
  • bit ✔
  • microbit
  • bite
  • minibit

№5. Statements

Question: Choose the right statements about decimal-based and binary-based systems.

Select one or more options from the list:

  • decimal-based system uses kibibyte, mebibyte, gibibyte
  • decimal-based system uses kilobyte, megabyte, gigabyte ✔
  • binary-based system uses kibibyte, mebibyte, gibibyte ✔
  • binary-based system uses kilobyte, megabyte, gigabyte

№6. Convert bytes into kibibytes

Question: How many KiB are there in 1536 B? If the answer is a decimal fraction, separate the parts with the dot symbol. [HINT]Remember the difference between KiB and KB.[/HINT]

Enter a short text: 1.5


№7. Convert GiB into MiB

Question: How many MiB are there in 0.5 GiB?

Enter a short text: 512


№8. 1 and 0

Question: Any information in a classical computer is represented by binary digits: 1 and 0. But what do they mean? Select all correct statements.

Select one or more options from the list:

  • 1 is not used for presentation of information
  • 0 means "no signal" ✔
  • 0 is not used for presentation of information
  • 0 means "there is a signal"
  • 1 means "no signal"
  • 1 means "there is a signal" ✔

№9. The units of information

Question: Match the units of information with their values:

Match the items from left and right columns:

  • 1 kilobyte = 1000 bytes
  • 1 megabyte = 1000 kilobytes
  • 1 gigabyte = 1000 megabytes
  • 1 terabyte = 1000 gigabytes
  • 1 petabyte = 1000 terabytes

№10. Convert bytes to kilobytes in a function

Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!

Question: Given a certain number of bytes, your task is to convert them to kilobytes. However, the function to do so appears to be unfinished. Your task is to fill in the blanks in the provided Python function to accurately convert the given amount of bytes to kilobytes. This function takes an integer as an argument, representing the number of bytes, and returns a formatted string representation of the kilobytes value.

Fill in the gaps with the relevant code elements:

def bytes_to_kilobytes(bytes):
   return f"{bytes / 1024} Kilobytes"

print(bytes_to_kilobytes(5000))

Variants: bytes, 1024, kilobytes.


№11. Completing a function for file size conversion

Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!

Question: You have received a code snippet that is intended to convert a file size from bytes to kilobytes and print the result. However, some parts of the code are missing. Your task is to determine what values or code statements will correctly fill the blanks in the code (marked with '▭'), so the function operates properly. Remember that 1 Kilobyte equals 1024 bytes:

Fill in the gaps with the relevant code elements:

def convert_bytes_to_kilobytes():
     kilobytes = bytes /
     return str(kilobytes) + " "

bytes_size = 5120
print(convert_bytes_to_kilobytes(bytes_size))

Variants: KB, bytes, 1024.

Solution:

The correct code to fill in the gaps would be:

def convert_bytes_to_kilobytes(bytes):
     kilobytes = bytes / 1024
     return str(kilobytes) + " KB"

bytes_size = 5120
print(convert_bytes_to_kilobytes(bytes_size))

Explanation. This will define a function convert_bytes_to_kilobytes() that takes a bytes argument and returns the size in kilobytes as a string with "KB" appended. The function calculates the size in kilobytes by dividing the number of bytes by 1024. The bytes_size variable is set to 5120, which is the number of bytes to be converted to kilobytes. Finally, the print() function is used to call convert_bytes_to_kilobytes() with the bytes_size argument and print the result.