Binary in a nutshell 
Tags: computerscience, hardware, binary
Published: 2021-07-04 20:51:42

> There are only 10 types of people in the world: Those who understand binary and those who don't

## The sense of curiosity 

When I got my first computer, I couldn't know at first that the Mario that I was seeing on my screen was actually composed of a bunch of 0s and 1s - How come silly combinations of numbers can handle the magic behind video games and web applications? 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eigeywx4v3162ni1i2pw.png)

## Systems of counting 

Along the years, as civilizations started to grow, many systems of counting (Or numeral systems) were created, such as:
- Tally marks
- Base ten positional → Decimal system
- Base two positional → Binary system

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibzkogt0ly8pcchqaa3t.png)
 
 
## What is binary?

Binary is a numeral system based o two digits (0 and 1), which represents numbers based with a base of 2. It's used to write **binary code** which is the "language" to communicate instructions to a computer processor.

In a nutshell:

- Uses base 2
- Greater than 1 → New digit
- Extremely useful for electricity
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhmzz7ngd6teh4xt9l4h.png)
 
## Why to use binary instead of decimal?

Humans decided to use decimal mainly because it's easier to demonstrate multiple combinations of values and it's also more high level in a way that makes it easier to read and exchange information. 
Therefore, the decision to use binary for computer hardware is due to reasons related to **physics and data processing**. 

- **Simplicity**: It's easier and simpler to rely on two states of characters in order to perform processing, than having to deal with a huge amount of combinations.
- **Electricity integrations**: As mentioned previously about combinations of numbers, having just 0s and 1s, easily enables to detect of electrical signals such as off (false) and on (true)
- **Magnetism:** Binary can be represented via positive and negative poles of magnetic media and in fact, this is how hard disks works in order to store data in a non-volatile way.
- **Circuits control**: Circuits contain switches with control logical flows via electrical signals and binary is the most efficient system to accomplish so.

## Characters representations

Now that the definition of binary and its use cases are clarified, you might be wondering how letters and other characters are displayed in software since all data is stored with 0s and 1s on the computer memory.

There are many things to consider, such as each language has a variety of characters to be supported - and also the first computer creators didn't expect that someday we would use things such as emojis. Consequently, along the years, many encoding systems from decimal numbers to other representations have arisen.

Examples of encodings:

- [ASCII (American Standard Code for Info Exchange)](https://pt.wikipedia.org/wiki/ASCII)
- [Unicode](https://en.wikipedia.org/wiki/Unicode)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmsdfi12jhqveuw3jm0k.png)

## How binary is stored on memory?

Consider the following main mechanisms for data storage:

- RAM (Volatile)
- HDD (Non-volatile)
- SSD (Non-volatile)

Each of the above handles binary data in a different way. Let's start from RAM:

### RAM

Stores binary data through **capacitors**, which contain two states when interacting with electricity: 

- Charged: **1**
- Un-charged: **0**

Each "0" and "1" is considered a [bit](https://en.wikipedia.org/wiki/Bit) and 8 bits lead to 1 [byte](https://en.wikipedia.org/wiki/Byte). Typically, a computer have 8 gigabytes of memory and consensually, 8X8e+9 or (64 billion) RAM memory cells.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwycy4fwny69j992en6z.png)

### HDD

Stores binary data through **magnetic lines** from a spinning metal disk called as "platter" and it's covered by magnetic material. 

A read/write head that contains an electromagnet on its tip, enables to write magnetically charged lines on the surface of the platter. When reading, if a line is magnetically attractive it is considered a "0" and if it's repulsive, then it's considered a "1".  

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bc2s3crlc050tlff05h.png)
 
### SSD

Stores binary data through **transistors**, which contain two states when interacting with electricity: 

- Charged: **1**
- Un-charged: **0**

Unlike RAM capacitors, transistors are able to keep their charge even after powering off a computer, leading to not data-lost. 

## Conclusion

Binary code allows representing **physical states via abstractions** with 0's and 1's. Used with the following mechanisms:

- **RAM**: Electric charge with capacitors
- **HDD**: Magnetic charge with lines
- **SSD:** Electric change with transistors

## Resources

- [Magnetic media with binary](https://www.computerhope.com/jargon/m/magnmedi.htm)
- [Binary numbers](https://sites.google.com/site/syhsguzmancsp/creative-projects/binary-numbers)
- [Numeral system](https://www.google.com/url?sa=i&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNumeral_system&psig=AOvVaw1_p2c3nzIu5ZkeKjj_aM-H&ust=1625514759675000&source=images&cd=vfe&ved=0CAsQjhxqFwoTCLC6zMeYyvECFQAAAAAdAAAAABAD)

---- 

Thanks for reading so far. I would like to mention that I'm planning to do a series of blog posts of topics related to CS fundamentals.

The goal is not to go too deep into these topics but to try to give a quick summary with higher-level concepts (And that's why I named the title of this post with "In a nutshell").