What Junior Devs Should Know About Bits and Bytes

Have you ever been confused about why your internet speed is measured in Mega or Giga Bits Per Second (Mbps or Gbps), but your computer’s Hard Drive and RAM are measured in Mega or Giga Bytes (MB or GB)? Or maybe you noticed that you’re paying for 100Mbps but your download speeds top out at around 12MBps? Well fear no more Junior Devs, we’re here to clear the air and help you understand the difference. On top of that, we’ll leave you with a better understanding of what bits and bytes mean to developers and why you should care to know the difference.

The Bit

noun

  1. A small piece, part, or quantity of something.
  2. A basic unit of information used in computing and digital communication.

These definitions don’t do much to resolve any mental conflicts you might have about what a bit really is. In fact, bits are not only some ephemeral measurement of data, because they correspond to actual electrons being ‘stored’ inside of electronic devices. Without pulling this post into the weeds, we’ll try to abstract the process of what’s going on under the hood of a digital device. In essence, every computer, mobile phone, network router, TV, or any other digital device contains millions, and more commonly billions, of tiny little things called “transistors.” These transistors allow for electric currents to be switched either on or off. With this potential, we are able to measure what state a transistor is in: 1 or 0, On or Off, True or False. This also allows us to store information in the state of contiguous transistors, which is how we are able to use the Binary System to store data.

Binary

We have to take a step back from transistors to look at how it’s possible to store and transmit trillions upon trillions upon trillions of bits per day. The term binary literally means “something having two parts.” which is exactly what transistors allow for (0 or 1). The term “bit” is derived from “binary digit”, meaning a unit of measurement with only two possible states. Using a sequence of On/Off relationships, we can do the math to convert the state of electrons into bits, and then into more familiar numbers and symbols. I’m no mathematician, and I’m certainly not the best at explaining math concepts. So instead of me trying to explain how to convert binary, read this post from MathIsFun to get a better understanding.

All done? Let’s proceed!

Now that you know how bits can be changed into familiar decimal numbers, we can talk about how data is stored and retrieved using transistors. Let’s say we have 2 transistors – this allows us to have 2 to the power of 2, or 4, potential states. How? you might ask. Well if you consider that we have 2 transistors and each transistor has 2 possible states (0 or 1), you get 2 x 2. If we have 4 transistors, we have 2 to the power of 4, or 16, possible states. Each transistor added increases the power to which 2 should be taken. Give me 8 transistors and I’ll give you 256 possible states in which you can configure your bits. This explains why memory is usually measured in powers of 2 (8, 16, 32, 64, 128, 256, 512, etc are all powers of 2). You can also see that by adding just a single transistor, we can increase the number of possible states exponentially.

That paragraph was heavy, so let’s take a break from math for a minute

I really don’t want to impose math on anybody, but it is essential to understand the above concepts before you can truly understand bits and bytes. Computers are built atop of math, so if you’re serious about being the best Junior Dev you can be, you should really try to be friendly toward math. To recap, each additional transistor gives us exponentially more states to work with. Most modern computing devices run either 32-bit or 64-bit software and operating systemsFor our purposes, this means that computers can read and store numbers as large as 2 to the power of 64, or 18,446,744,073,709,551,616. You read that correctly, modern computers can count up to HUGE numbers. And with all of the data we store and transmit daily, you shouldn’t be surprised.

Back to your internet service provider…

When your ISP says your download speed is capped at 100Mbps, they’re really saying that your connection is capable of receiving 100,000,000 bits per second. That’s one-hundred-million 0’s and 1’s each second. To put that into perspective, back in the early 2000’s, when I was ‘hacking’ on dial-up, I connected through a 56k modem, which is a scrawny 56,000 bits per second. You can see that transfer rates have come a long way, and so has the technology behind it. Now, you might be asking, where do bytes fit into this picture, and why are there two terms to measure what appears to be the same data? That’s the topic of our next section.

The Byte

What’s in a name?

If a bit is the smallest unit for measuring the state of a computer system, what’s a byte? It’s actually incredibly easy, a byte is just a group of 8 bits.
Let that sink in…
Ok, got it…?
By now, you’re probably thinking “We’ve been lied to our whole lives!” Calm down, it’s not that serious! When computers first hit the scene, they were initially only capable of processing 8 bits at a time. This was largely due to hardware limitations, plus the calculations needed at the time didn’t require 100-million bits to be transfered over a wire within a second of time. Since the term bit was already used to describe the state of a single transistor (actually vacuum tubes at the time), Computer Scientists needed another term to describe a grouping of bits, and byte seemed to be the most logical choice.

A byte is really just 8 bits

So when your ISP says 100Mbps and you see ~10MBps, you’re really just seeing the difference in naming conventions. 100/8 is 12.5, which explains why your speeds might appear slower than what you’re paying for. It’s not that it’s slower, it’s just that your ISP chooses to confuse you on purpose (I might be stretching the truth, but this is what I actually believe 😅). So 1 byte can store up to 2 to the power of 8 or 256 different states. This means a song (which is typically about 5 Mega Bytes) actually hold 40 Mega Bits, or 40,000,000 bits of information. So Jay-Z’s “99 Problems” and DJ E-Z Rock’s “It Takes Two” actually take up 40 Million bits (see what I did there?).

Why should Junior Dev’s care?

As the master of all things digital, it’s your job to manage the flow and storage of bits. As developers, that is essentially what we’re doing. When you create a variable in your given programming language, what you’re doing is requesting a predetermined amount of memory from the Operating System. Each different type has its own requirements for memory. Below are some of the common types and the amount of memory they typically take up. Notice that these are all represented in bytes, not bits.

  • Characters: 1-8 bytes (depends on ASCII or Unicode or other standards)
  • String: Depends on how many Characters are in the string
  • Integers: Minimum 2 bytes
  • Long Integer: Minimum 4 bytes
  • Long Long Integer: Minimum 8 bytes
  • Float: Minimum 4 bytes
  • Double: 8 bytes

These values are only given as an example, and the actual size will depend on the following:

  • Programming Language
  • Compiler Implementation
  • Operating System
  • CPU Architecture

Most programming languages contain built in functions that will give you the size of each variable, so look into that if you really want to know. In many cases, however, you won’t need to worry about these low-level details, as it’s usually all predetermined for you.

Unless your code needs to be fast and efficient

There are some cases in which developers really need to pay attention to the bits and bytes they’re shuffling around in the computer. In C and C++, for instance, struct’s and classes may take up more space than needed if you don’t structure them correctly. We also need to be aware of size constraints on the different devices our program is targeting. If for instance, you’re writing desktop software, you probably don’t need to worry about your app taking up a few GB. On mobile however, where space is limited, you wouldn’t want to take up more than a few hundred MB.

Either way, you should pay attention to your data requirements

Deciding how big or how small your app should be is important, and that decision should influence the implementation details of your data types. If you’re constrained on space, you might consider using Float’s instead of Doubles, or use Integers instead of Long Long Integers. In many cases, this might not be relevant, but truly great developers think of these things no matter what. It’s just a part of the job, and it’s something Junior Dev’s should get in the habit of doing.

Understanding the hardware that your app is running on should be important to you. In our next article, we’ll look at the difference between Hard Drives and RAM, and what Junior Dev’s need to know about Computer Memory. Stay Tuned!

Advertisements

One thought on “What Junior Devs Should Know About Bits and Bytes

Comments are closed.