Chapter 1: RAM and Bits

Grogu or Yoda?

Day one: Grogu smashing buttons.
Day one thousand: Yoda explaining why the button matters.

What is a RAM?

8gb-ram-crucial-ddr4.webp
Figure 1: Crucial 8GB 2400MHz DDR4 SODIMM Laptop Memory

RAM (for Random Access Memory) is used to store data structure during runtime data. Figure 1 shows the image of how RAM looks like. Runtime data is the data stored in RAM which is required to run functionality of the computers (more like doing computation in ALU - Arithmetic Logical Unit, and CPU's). For example, Your image is in storage-data (like Hard-disk, SSD), but when you double click on the image, it loads the storage-data into runtime memory (like RAM) which allows us to see the image on screen after its processed.

So what is a Data Structure?

In simple words, Its structuring of data in temporary storage (e.g. RAM - Runtime Storage) or permanent storage (e.g. Solid State Drive) to access it with ease. For example, when you want to store an integer in your storage you can't allocate the space required for a character, or float or image to be stored and vice versa. You require a specific amount of bits and specific amount of structuring of those bits to get your integer stored in your computer for further use. Think of it like, you can't use rat trap to capture elephant. You need bigger and specific trap. The same way you need to allocate space in RAM for integers, character, floats, gender data, food list, etc..

Examples of Data Structures:

Data Structure Values Stored
integer (int) ..., -3, -2, -1, 0, 1, 2, 3, ...
float -3.14, -1.0, 0.5, 1.5, 100.984
character (char) a, A, b, c, ..., $, #, ₹, (, ), ...
Gender Male, Female
Size of a typical RAM?

It is very common for RAM to come in 4 GB, 8 GB, 12 GB, 16 GB, 32 GB, 64 GB, ...

So what is GB in 8 GB of RAM?

8 GBGigaByte8 Bits~Billion1 bit is either 0 or 1
8 GB is approximately a 8,589,934,592 bytes, which is 68,719,476,736 bits and each bit stores either 0 or 1.

Fun Fact

1 bit = 1 transistor; meaning there are 64 Billion to 70 Billion transistors in an 8GB RAM. Nowadays, transistors are very small with size around 10nm to 20nm. (nm is nanometer, which is 1 meter = 1e+9 nm or 1nm is one billionth of a meter)

S8050-Transistor.jpg
This is image of a NPN transistor. to learn more on how a transistor works, refer to this video: How a transistor works

Reality of 0's and 1's: The language of computer?

Storing an Integer {1, 7} in Computers:

Note: Integers use 4 bytes (which is 4 x 8 = 32 bits).

Storing 1

00000000 00000000 00000000 00000001

Storing 7

00000000 00000000 00000000 00000111

What is happening?

2n-annotation.excalidraw.png 800

Bits are stored in group of 8 bits.

What is Array?

Array is a contiguous block of data.

How arrays are stored in RAM?

Note: one of the RAMs functionality is to allocate space for our computational work. RAM can allocate memory or space randomly anywhere within it.

Moving on, lets look at bit manipulation and operations.

Bit Logical" Operations

AND

IN 1 Operation IN 2 OUT
0 & 0 0
0 & 1 0
1 & 0 0
1 & 1 1

OR

IN 1 Operation IN 2 OUT
0 | 0 0
0 | 1 1
1 | 0 1
1 | 1 1

XOR

IN 1 Operation IN 2 OUT
0 ^ 0 0
0 ^ 1 1
1 ^ 0 1
1 ^ 1 0

NOT (also called negation)

IN Operation OUT
0 ~ 1
1 ~ 0

Bit Shifting Operation

Operations Method Output
Load 1011 1011
>> 1011 >> 1 0101
<< 0101 << 1 1010
>> 1010 >> 1 0101
>> 0101 >> 1 0010
>> 0010 >> 1 0001
>> 0001 >> 1 0000

This is bit shifting, again it can be overwhelming.

This is all about RAM and Bits. Below is Coding Problems to solve:

Solve the problems. If stuck use the solution link to pull yourself towards the right path, but don't push yourself. Remember to give yourself break and come back to think on the problem in different way.

  1. LeetCode - 191. Number of 1 Bits - Solution
  2. LeetCode - 338. Counting Bits - Solution
  3. LeetCode - 190. Reverse Bits - Solution

Supplementary Resources

You can read more about how a computer works under the hood in your free time with below resources:

  1. NAND2TETRIS
  2. Build your own 8-bit computer from scratch by Ben Eater

Next?

Goodbye Grogu,
Mrunal.

Powered by Forestry.md