<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shankar L</title>
    <description>The latest articles on DEV Community by Shankar L (@polyvexr).</description>
    <link>https://dev.to/polyvexr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3547911%2Fdeada772-6751-4ab6-ab89-af98c11c0e32.jpeg</url>
      <title>DEV Community: Shankar L</title>
      <link>https://dev.to/polyvexr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/polyvexr"/>
    <language>en</language>
    <item>
      <title>Compilation vs Interpretation</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Tue, 11 Aug 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/compilation-vs-interpretation-59fp</link>
      <guid>https://dev.to/polyvexr/compilation-vs-interpretation-59fp</guid>
      <description>&lt;h2&gt;
  
  
  Compilation vs Interpretation
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;You write code in a programming language such as Java, Python, C, or JavaScript.&lt;/p&gt;

&lt;p&gt;But the CPU does not understand these languages directly.&lt;/p&gt;

&lt;p&gt;Something has to translate your human-readable code into instructions the computer can execute.&lt;/p&gt;

&lt;p&gt;That translation can happen in different ways.&lt;/p&gt;

&lt;p&gt;Two of the most important concepts are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Compilation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpretation&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding the difference helps you understand why programming languages behave differently, why some programs start quickly, why some run faster, and where tools such as the JVM and JIT compiler fit into the picture.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Consider this simple program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello World&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CPU cannot directly execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello World")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It needs instructions that match the CPU's instruction set.&lt;/p&gt;

&lt;p&gt;So we need a translation process.&lt;/p&gt;

&lt;p&gt;There are two traditional approaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code
    ↓
Compiler
    ↓
Machine Code
    ↓
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code
    ↓
Interpreter
    ↓
Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But modern programming languages are more complicated than this simple distinction suggests.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Compilation
&lt;/h3&gt;

&lt;p&gt;A compiler translates source code into another form before the program runs.&lt;/p&gt;

&lt;p&gt;A simplified C compilation process looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C Source Code
      ↓
Compiler
      ↓
Machine Code
      ↓
Executable
      ↓
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A compiler can translate this into machine instructions appropriate for the target CPU.&lt;/p&gt;

&lt;p&gt;The resulting executable can then be run without recompiling the source code every time.&lt;/p&gt;




&lt;h3&gt;
  
  
  Interpretation
&lt;/h3&gt;

&lt;p&gt;An interpreter executes a program by reading and processing its instructions at runtime.&lt;/p&gt;

&lt;p&gt;A simplified model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code
      ↓
Interpreter
      ↓
Execute
      ↓
Next instruction
      ↓
Execute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python is commonly described as an interpreted language, although modern Python implementations first compile source code into bytecode before executing it.&lt;/p&gt;

&lt;p&gt;This is an important distinction:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Compiled" and "interpreted" describe execution strategies, not permanent categories that every language fits into perfectly.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;Think about translating a book.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compilation
&lt;/h3&gt;

&lt;p&gt;Imagine translating the entire book into another language before giving it to the reader.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Entire book
    ↓
Translator
    ↓
Translated book
    ↓
Reader
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The translation happens beforehand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpretation
&lt;/h3&gt;

&lt;p&gt;Now imagine a translator sitting next to the reader and translating each sentence as the reader encounters it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sentence
   ↓
Translator
   ↓
Reader
   ↓
Next sentence
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The translation happens during reading.&lt;/p&gt;

&lt;p&gt;That is the basic difference.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine a restaurant menu written in French.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compilation
&lt;/h3&gt;

&lt;p&gt;You translate the entire menu into English before customers arrive.&lt;/p&gt;

&lt;p&gt;Every customer can immediately read the translated version.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpretation
&lt;/h3&gt;

&lt;p&gt;A translator stands beside each customer and translates each item when they ask about it.&lt;/p&gt;

&lt;p&gt;The first approach requires preparation.&lt;/p&gt;

&lt;p&gt;The second approach performs translation during interaction.&lt;/p&gt;

&lt;p&gt;Programming language execution has similar trade-offs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Consider this simple C program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a compiler such as GCC, you can compile it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc main.c &lt;span class="nt"&gt;-o&lt;/span&gt; main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This produces an executable.&lt;/p&gt;

&lt;p&gt;You can then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The general process is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.c
  ↓
GCC
  ↓
Executable
  ↓
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now consider Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You normally run it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Python runtime handles the execution process.&lt;/p&gt;

&lt;p&gt;The important point is that the details are more nuanced than simply saying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C = compiled
Python = interpreted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modern language implementations often combine several techniques.&lt;/p&gt;




&lt;h2&gt;
  
  
  Java Is Different
&lt;/h2&gt;

&lt;p&gt;Java provides one of the clearest examples of a hybrid execution model.&lt;/p&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You compile it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;javac Main.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Main.class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This contains &lt;strong&gt;Java bytecode&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The process becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Java Source
    ↓
javac
    ↓
Bytecode
    ↓
JVM
    ↓
Machine Code
    ↓
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JVM can interpret bytecode and can also use a &lt;strong&gt;Just-In-Time compiler&lt;/strong&gt;, commonly called a JIT compiler, to compile frequently executed code into native machine instructions at runtime.&lt;/p&gt;

&lt;p&gt;This gives Java a mixture of compilation and interpretation techniques.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1: "Compiled languages are always faster"
&lt;/h3&gt;

&lt;p&gt;Not necessarily.&lt;/p&gt;

&lt;p&gt;Performance depends on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compiler optimizations&lt;/li&gt;
&lt;li&gt;Runtime behavior&lt;/li&gt;
&lt;li&gt;CPU architecture&lt;/li&gt;
&lt;li&gt;Memory access&lt;/li&gt;
&lt;li&gt;Garbage collection&lt;/li&gt;
&lt;li&gt;Algorithms&lt;/li&gt;
&lt;li&gt;Data structures&lt;/li&gt;
&lt;li&gt;Workload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A language's execution model is only one part of performance.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 2: "Python is purely interpreted"
&lt;/h3&gt;

&lt;p&gt;This is an oversimplification.&lt;/p&gt;

&lt;p&gt;For example, CPython typically performs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python Source
     ↓
Bytecode
     ↓
Python Virtual Machine
     ↓
Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So even Python involves a compilation step.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 3: "Java is interpreted"
&lt;/h3&gt;

&lt;p&gt;Saying Java is simply interpreted is also incomplete.&lt;/p&gt;

&lt;p&gt;Java uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source
  ↓
Bytecode
  ↓
JVM
  ↓
Interpreter and/or JIT Compiler
  ↓
Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JVM can dynamically optimize frequently executed code.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4: "Compilation happens only once"
&lt;/h3&gt;

&lt;p&gt;It depends on the language and execution environment.&lt;/p&gt;

&lt;p&gt;Traditional compiled programs may be compiled before distribution.&lt;/p&gt;

&lt;p&gt;JIT compilation happens while the program is running.&lt;/p&gt;

&lt;p&gt;Therefore, compilation can happen both before and during execution.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Ahead-of-Time Compilation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;AOT compilation&lt;/strong&gt; means code is compiled before execution.&lt;/p&gt;

&lt;p&gt;The general process is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source
  ↓
Compiler
  ↓
Native Machine Code
  ↓
Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C and C++ commonly use this model.&lt;/p&gt;

&lt;p&gt;Advantages include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast startup&lt;/li&gt;
&lt;li&gt;Strong compile-time optimization&lt;/li&gt;
&lt;li&gt;Direct native execution&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Just-In-Time Compilation
&lt;/h3&gt;

&lt;p&gt;JIT compilation happens while the program is running.&lt;/p&gt;

&lt;p&gt;A simplified process is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source
   ↓
Intermediate Representation
   ↓
Runtime
   ↓
JIT Compiler
   ↓
Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The runtime can observe how the program behaves and optimize frequently executed code.&lt;/p&gt;

&lt;p&gt;This is used by technologies such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JVM&lt;/li&gt;
&lt;li&gt;.NET runtime&lt;/li&gt;
&lt;li&gt;JavaScript engines&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Bytecode
&lt;/h3&gt;

&lt;p&gt;Bytecode is an intermediate representation.&lt;/p&gt;

&lt;p&gt;It sits between source code and machine code.&lt;/p&gt;

&lt;p&gt;For Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Java Source
    ↓
Java Bytecode
    ↓
JVM
    ↓
Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The advantage is portability.&lt;/p&gt;

&lt;p&gt;The same Java bytecode can run on different operating systems and CPU architectures as long as a compatible JVM exists.&lt;/p&gt;

&lt;p&gt;This is the idea behind:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Write once, run anywhere.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Compilation vs Interpretation
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Compilation&lt;/th&gt;
&lt;th&gt;Interpretation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Translation&lt;/td&gt;
&lt;td&gt;Before or during build&lt;/td&gt;
&lt;td&gt;During execution&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical output&lt;/td&gt;
&lt;td&gt;Machine code or intermediate code&lt;/td&gt;
&lt;td&gt;Runtime execution&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Startup&lt;/td&gt;
&lt;td&gt;Often faster after compilation&lt;/td&gt;
&lt;td&gt;Can have runtime overhead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Optimization&lt;/td&gt;
&lt;td&gt;Can happen before execution&lt;/td&gt;
&lt;td&gt;Can happen dynamically&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Examples&lt;/td&gt;
&lt;td&gt;C, C++&lt;/td&gt;
&lt;td&gt;Traditional interpreters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Modern reality&lt;/td&gt;
&lt;td&gt;Often combined with other techniques&lt;/td&gt;
&lt;td&gt;Often combined with bytecode/JIT&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The table is intentionally simplified because modern runtimes frequently combine compilation, interpretation, bytecode, and JIT techniques.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bigger Picture
&lt;/h2&gt;

&lt;p&gt;The most useful mental model is not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compiled vs Interpreted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How does this language implementation
transform source code into executable work?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Different languages can choose different strategies.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C
Source
  ↓
Compiler
  ↓
Native Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Java
Source
  ↓
Bytecode
  ↓
JVM
  ↓
JIT
  ↓
Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python
Source
  ↓
Bytecode
  ↓
Python Runtime
  ↓
Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript
Source
  ↓
JavaScript Engine
  ↓
Interpretation + JIT
  ↓
Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact implementation depends on the runtime and version.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Compilation and interpretation are two different approaches for turning source code into executable behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compilation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source
  ↓
Compiler
  ↓
Machine Code
  ↓
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Interpretation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source
  ↓
Interpreter
  ↓
Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But modern systems are more sophisticated.&lt;/p&gt;

&lt;p&gt;Java uses bytecode and the JVM.&lt;/p&gt;

&lt;p&gt;Python implementations may compile source into bytecode.&lt;/p&gt;

&lt;p&gt;JavaScript engines commonly combine interpretation and JIT compilation.&lt;/p&gt;

&lt;p&gt;The most important lesson is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Programming languages are not simply "compiled" or "interpreted." Their implementations can use multiple execution techniques.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding this distinction gives you a much better foundation for learning compilers, virtual machines, programming language runtimes, and performance optimization.&lt;/p&gt;

</description>
      <category>coding</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Registers, Cache, RAM Explained</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Mon, 10 Aug 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/registers-cache-ram-explained-2chk</link>
      <guid>https://dev.to/polyvexr/registers-cache-ram-explained-2chk</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;Your CPU can process instructions incredibly quickly.&lt;/p&gt;

&lt;p&gt;But there is a problem.&lt;/p&gt;

&lt;p&gt;The data the CPU needs is not always immediately available.&lt;/p&gt;

&lt;p&gt;Different types of memory exist because &lt;strong&gt;speed, capacity, and cost are trade offs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding registers, cache, and RAM helps explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why CPUs are so fast.&lt;/li&gt;
&lt;li&gt;Why RAM is slower than cache.&lt;/li&gt;
&lt;li&gt;Why cache has multiple levels.&lt;/li&gt;
&lt;li&gt;Why memory locality matters.&lt;/li&gt;
&lt;li&gt;Why some programs are much faster than others.&lt;/li&gt;
&lt;li&gt;How the CPU gets the data it needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key idea is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The closer memory is to the CPU, the faster it generally is.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine the CPU needs this value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where should it get it from?&lt;/p&gt;

&lt;p&gt;It could be stored in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Register
Cache
RAM
SSD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are not equally fast.&lt;/p&gt;

&lt;p&gt;A CPU that had to wait for RAM every time it needed a value would spend a significant amount of time waiting.&lt;/p&gt;

&lt;p&gt;Instead, computers use a &lt;strong&gt;memory hierarchy&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fastest
   ↓
Registers
   ↓
L1 Cache
   ↓
L2 Cache
   ↓
L3 Cache
   ↓
RAM
   ↓
SSD
   ↓
Slowest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we move downward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Capacity generally increases.&lt;/li&gt;
&lt;li&gt;Cost per byte generally decreases.&lt;/li&gt;
&lt;li&gt;Access latency generally increases.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;Let's understand the three important levels.&lt;/p&gt;

&lt;h3&gt;
  
  
  Registers
&lt;/h3&gt;

&lt;p&gt;Registers are tiny storage locations located directly inside a CPU core.&lt;/p&gt;

&lt;p&gt;They hold values that the CPU is actively working with.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Register A = 10
Register B = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CPU can perform:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 + 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;using those values.&lt;/p&gt;

&lt;p&gt;Registers are extremely fast, but there are very few of them compared with RAM.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cache
&lt;/h3&gt;

&lt;p&gt;CPU cache sits between registers and RAM.&lt;/p&gt;

&lt;p&gt;Its job is to keep frequently or recently used data close to the CPU.&lt;/p&gt;

&lt;p&gt;Modern processors commonly have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;L1 Cache
L2 Cache
L3 Cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;L1 is generally the smallest and fastest.&lt;/p&gt;

&lt;p&gt;L3 is generally larger and slower than L1.&lt;/p&gt;

&lt;p&gt;A simplified hierarchy looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU Core
   |
  L1
   |
  L2
   |
  L3
   |
  RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  RAM
&lt;/h3&gt;

&lt;p&gt;RAM stands for &lt;strong&gt;Random Access Memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It holds programs and data that the operating system is currently using.&lt;/p&gt;

&lt;p&gt;For example, when you open a browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SSD
 ↓
RAM
 ↓
CPU Cache
 ↓
Registers
 ↓
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program is stored permanently on the SSD, but the CPU works with data loaded into RAM and then brought closer through the cache hierarchy.&lt;/p&gt;

&lt;p&gt;RAM is much larger than cache, but significantly slower.&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;Think about working at a desk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your hand
    ↓
Desk
    ↓
Drawer
    ↓
Cupboard
    ↓
Storage room
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need a pen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your hand: extremely fast.&lt;/li&gt;
&lt;li&gt;On the desk: very fast.&lt;/li&gt;
&lt;li&gt;In the drawer: slower.&lt;/li&gt;
&lt;li&gt;In the cupboard: slower.&lt;/li&gt;
&lt;li&gt;In the storage room: much slower.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The CPU works in a similar way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Registers → Cache → RAM → Storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CPU tries to keep the data it needs as close as possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine a restaurant kitchen.&lt;/p&gt;

&lt;p&gt;The chef is the &lt;strong&gt;CPU&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ingredients being actively used are &lt;strong&gt;registers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ingredients placed on the kitchen counter are &lt;strong&gt;cache&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ingredients inside the refrigerator are &lt;strong&gt;RAM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ingredients stored in the warehouse are &lt;strong&gt;storage&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The chef does not want to walk to the warehouse every time an ingredient is needed.&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Warehouse
    ↓
Refrigerator
    ↓
Kitchen Counter
    ↓
Chef's Hands
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same principle applies to computer memory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Consider this loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At a high level, the program repeatedly accesses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i
sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CPU needs these values repeatedly.&lt;/p&gt;

&lt;p&gt;The exact behavior depends on the compiler, JVM, CPU architecture, and optimization level, but frequently accessed values may spend time in CPU registers and cache rather than requiring a trip to RAM for every operation.&lt;/p&gt;

&lt;p&gt;This is one reason &lt;strong&gt;data locality&lt;/strong&gt; matters.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1: Thinking cache is just another type of RAM
&lt;/h3&gt;

&lt;p&gt;Cache and RAM are both memory, but they serve different roles.&lt;/p&gt;

&lt;p&gt;Cache is much smaller and designed to provide very fast access close to the CPU.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 2: Thinking more cache always means a faster CPU
&lt;/h3&gt;

&lt;p&gt;More cache can help, but performance depends on the workload.&lt;/p&gt;

&lt;p&gt;A larger cache does not automatically make every program faster.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 3: Thinking registers are the same as variables
&lt;/h3&gt;

&lt;p&gt;A programming language variable is an abstraction.&lt;/p&gt;

&lt;p&gt;The compiler may keep a value in a register, place it on the stack, optimize it away, or store it elsewhere depending on the program and optimization.&lt;/p&gt;

&lt;p&gt;You should not assume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means there is literally a hardware register called &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4: Thinking the CPU always reads RAM
&lt;/h3&gt;

&lt;p&gt;The CPU normally checks the cache hierarchy first.&lt;/p&gt;

&lt;p&gt;A simplified model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU needs data
      ↓
L1 Cache?
      ↓
L2 Cache?
      ↓
L3 Cache?
      ↓
RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the data is found in cache, it is called a &lt;strong&gt;cache hit&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If it is not found, it is a &lt;strong&gt;cache miss&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Cache Hit
&lt;/h3&gt;

&lt;p&gt;Suppose the CPU requests data and it is already in cache.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU
 ↓
Cache
 ↓
Data found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a &lt;strong&gt;cache hit&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The CPU can continue quickly.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cache Miss
&lt;/h3&gt;

&lt;p&gt;If the requested data is not in the cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU
 ↓
L1 Miss
 ↓
L2 Miss
 ↓
L3 Miss
 ↓
RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The processor must obtain the data from a lower level.&lt;/p&gt;

&lt;p&gt;This takes longer.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cache Locality
&lt;/h3&gt;

&lt;p&gt;Programs often access data in predictable patterns.&lt;/p&gt;

&lt;p&gt;There are two important types of locality.&lt;/p&gt;

&lt;h4&gt;
  
  
  Temporal Locality
&lt;/h4&gt;

&lt;p&gt;If a program accesses something now, it is likely to access it again soon.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;sum&lt;/code&gt; is repeatedly used inside a loop.&lt;/p&gt;

&lt;h4&gt;
  
  
  Spatial Locality
&lt;/h4&gt;

&lt;p&gt;If a program accesses one memory location, it is likely to access nearby locations.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program accesses array elements sequentially.&lt;/p&gt;

&lt;p&gt;This pattern is generally cache-friendly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Registers vs Cache vs RAM
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Registers&lt;/th&gt;
&lt;th&gt;Cache&lt;/th&gt;
&lt;th&gt;RAM&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Location&lt;/td&gt;
&lt;td&gt;CPU core&lt;/td&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;Main memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Tiny&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Fastest&lt;/td&gt;
&lt;td&gt;Very fast&lt;/td&gt;
&lt;td&gt;Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main purpose&lt;/td&gt;
&lt;td&gt;Active calculations&lt;/td&gt;
&lt;td&gt;Frequently used data&lt;/td&gt;
&lt;td&gt;Running programs and data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Managed by&lt;/td&gt;
&lt;td&gt;Compiler/CPU&lt;/td&gt;
&lt;td&gt;Hardware&lt;/td&gt;
&lt;td&gt;Operating system + hardware&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The important trade-off is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;More Speed
    ↓
Less Capacity
    ↓
Higher Cost per Byte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And generally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;More Capacity
    ↓
Lower Speed
    ↓
Lower Cost per Byte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  One Important Detail
&lt;/h2&gt;

&lt;p&gt;You may see diagrams like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Registers
   ↓
Cache
   ↓
RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But modern CPUs are more complicated.&lt;/p&gt;

&lt;p&gt;There are multiple cache levels, memory controllers, translation lookaside buffers, hardware prefetchers, out-of-order execution, and other mechanisms.&lt;/p&gt;

&lt;p&gt;The simplified hierarchy is useful because it gives you the correct mental model without overwhelming you with implementation details.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Registers, cache, and RAM are different layers of the computer's memory hierarchy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              CPU
               ↓
          Registers
               ↓
            L1 Cache
               ↓
            L2 Cache
               ↓
            L3 Cache
               ↓
              RAM
               ↓
            Storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Registers&lt;/strong&gt; hold values the CPU is actively working with.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache&lt;/strong&gt; keeps frequently needed data close to the CPU.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAM&lt;/strong&gt; holds programs and data currently being used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache hits&lt;/strong&gt; are faster than cache misses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Locality&lt;/strong&gt; is important for performance.&lt;/li&gt;
&lt;li&gt;Faster memory is generally smaller and more expensive per byte.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next time your program runs a loop over an array, remember that the CPU is not simply "reading memory."&lt;/p&gt;

&lt;p&gt;It is constantly moving data through a carefully designed hierarchy to keep the processor busy.&lt;/p&gt;

</description>
      <category>ram</category>
      <category>cpu</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>CPU Architecture Simplified</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Sun, 09 Aug 2026 07:16:22 +0000</pubDate>
      <link>https://dev.to/polyvexr/cpu-architecture-simplified-26gp</link>
      <guid>https://dev.to/polyvexr/cpu-architecture-simplified-26gp</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;You write code.&lt;/p&gt;

&lt;p&gt;The compiler translates it.&lt;/p&gt;

&lt;p&gt;But who actually executes it?&lt;/p&gt;

&lt;p&gt;The answer is the &lt;strong&gt;CPU&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every program you run eventually becomes a sequence of instructions that the CPU executes.&lt;/p&gt;

&lt;p&gt;Understanding CPU architecture helps explain why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some programs are faster than others.&lt;/li&gt;
&lt;li&gt;RAM is slower than CPU cache.&lt;/li&gt;
&lt;li&gt;Your CPU has multiple cores.&lt;/li&gt;
&lt;li&gt;A program can use multiple threads.&lt;/li&gt;
&lt;li&gt;Low-level optimizations sometimes matter.&lt;/li&gt;
&lt;li&gt;Programming concepts like loops, function calls, and variables eventually become CPU instructions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need to become a hardware engineer to understand CPU architecture.&lt;/p&gt;

&lt;p&gt;You just need to understand the main components and how they work together.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Consider this simple Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks extremely simple.&lt;/p&gt;

&lt;p&gt;But the CPU doesn't understand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int
a
b
result
+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler and JVM eventually translate your program into instructions that the processor can execute.&lt;/p&gt;

&lt;p&gt;At the hardware level, the CPU performs operations such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load data
Perform calculation
Store result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what exactly exists inside a CPU that allows it to do this?&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;A CPU contains several important components.&lt;/p&gt;

&lt;p&gt;The most important ones to understand initially are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              CPU
               |
      +--------+--------+
      |                 |
 Control Unit          ALU
      |                 |
      +--------+--------+
               |
            Registers
               |
             Cache
               |
              RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each component has a different responsibility.&lt;/p&gt;

&lt;p&gt;The main components are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Control Unit&lt;/li&gt;
&lt;li&gt;ALU&lt;/li&gt;
&lt;li&gt;Registers&lt;/li&gt;
&lt;li&gt;CPU Cache&lt;/li&gt;
&lt;li&gt;Clock&lt;/li&gt;
&lt;li&gt;CPU Cores&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand them one by one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Control Unit
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Control Unit&lt;/strong&gt; coordinates the CPU.&lt;/p&gt;

&lt;p&gt;It determines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which instruction should be executed.&lt;/li&gt;
&lt;li&gt;What operation needs to happen.&lt;/li&gt;
&lt;li&gt;Where data should come from.&lt;/li&gt;
&lt;li&gt;Where the result should go.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can think of it as the CPU's coordinator.&lt;/p&gt;




&lt;h3&gt;
  
  
  ALU
&lt;/h3&gt;

&lt;p&gt;ALU stands for &lt;strong&gt;Arithmetic Logic Unit&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It performs operations such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Addition
Subtraction
AND
OR
XOR
Comparisons
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 + 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ALU performs the actual arithmetic operation.&lt;/p&gt;




&lt;h3&gt;
  
  
  Registers
&lt;/h3&gt;

&lt;p&gt;Registers are extremely small and extremely fast storage locations inside the CPU.&lt;/p&gt;

&lt;p&gt;They temporarily hold:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Addresses&lt;/li&gt;
&lt;li&gt;Instructions&lt;/li&gt;
&lt;li&gt;Intermediate results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Register A = 10
Register B = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ALU can then operate on these values.&lt;/p&gt;

&lt;p&gt;Registers are much faster than RAM because they are located directly inside the processor.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cache
&lt;/h3&gt;

&lt;p&gt;CPU cache stores frequently used data and instructions close to the CPU.&lt;/p&gt;

&lt;p&gt;Modern processors typically have multiple cache levels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;L1 Cache
   ↓
L2 Cache
   ↓
L3 Cache
   ↓
RAM
   ↓
SSD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you move downward, capacity generally increases while access becomes slower.&lt;/p&gt;

&lt;p&gt;L1 cache is extremely fast but very small.&lt;/p&gt;

&lt;p&gt;L3 cache is larger but slower than L1.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine a chef preparing a meal.&lt;/p&gt;

&lt;p&gt;The chef is the &lt;strong&gt;CPU&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The ingredients stored in the refrigerator are like &lt;strong&gt;data in storage&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The kitchen counter is like &lt;strong&gt;RAM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A small plate beside the chef is like the &lt;strong&gt;CPU cache&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The chef's hands are like &lt;strong&gt;registers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The cooking instructions are like &lt;strong&gt;CPU instructions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The chef doesn't repeatedly walk to the refrigerator for every ingredient.&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Refrigerator
     ↓
Kitchen Counter
     ↓
Small Plate
     ↓
Chef's Hands
     ↓
Cooking
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The closer something is to the chef, the faster it can be accessed.&lt;/p&gt;

&lt;p&gt;This is similar to the CPU's memory hierarchy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fetch, Decode, Execute
&lt;/h2&gt;

&lt;p&gt;One of the most important concepts in CPU architecture is the &lt;strong&gt;instruction cycle&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The CPU repeatedly performs three fundamental steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fetch
  ↓
Decode
  ↓
Execute
  ↓
Fetch
  ↓
Decode
  ↓
Execute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1. Fetch
&lt;/h3&gt;

&lt;p&gt;The CPU retrieves the next instruction from memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Decode
&lt;/h3&gt;

&lt;p&gt;The CPU determines what that instruction means.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ADD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;might tell the processor to perform an addition.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Execute
&lt;/h3&gt;

&lt;p&gt;The CPU performs the operation.&lt;/p&gt;

&lt;p&gt;This cycle happens extremely quickly.&lt;/p&gt;

&lt;p&gt;A modern CPU can perform billions of clock cycles per second.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conceptually, the CPU may perform operations similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOAD 10
LOAD 20
ADD
STORE result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual instructions depend on the processor architecture, compiler, JVM, and optimization level.&lt;/p&gt;

&lt;p&gt;The important idea is that high-level code eventually becomes low-level instructions.&lt;/p&gt;

&lt;p&gt;You can see this more clearly with C.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A compiler may translate this into assembly instructions similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov eax, edi
add eax, esi
ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact assembly depends on the CPU architecture and compiler.&lt;/p&gt;

&lt;p&gt;But the underlying idea is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get values
   ↓
Perform addition
   ↓
Return result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  CPU Cores
&lt;/h2&gt;

&lt;p&gt;Modern CPUs usually contain multiple cores.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU
├── Core 1
├── Core 2
├── Core 3
└── Core 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each core can execute its own stream of instructions.&lt;/p&gt;

&lt;p&gt;This allows multiple tasks to make progress concurrently.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Core 1 → Browser
Core 2 → Music Player
Core 3 → Code Compilation
Core 4 → Background Tasks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one reason modern operating systems can run many applications at the same time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1: Thinking GHz means performance
&lt;/h3&gt;

&lt;p&gt;A CPU running at 4 GHz is not automatically twice as fast as one running at 2 GHz.&lt;/p&gt;

&lt;p&gt;Performance also depends on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU architecture&lt;/li&gt;
&lt;li&gt;Instructions per cycle&lt;/li&gt;
&lt;li&gt;Cache&lt;/li&gt;
&lt;li&gt;Number of cores&lt;/li&gt;
&lt;li&gt;Memory performance&lt;/li&gt;
&lt;li&gt;Compiler optimizations&lt;/li&gt;
&lt;li&gt;Workload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Clock speed is only one factor.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 2: Thinking more cores always means faster
&lt;/h3&gt;

&lt;p&gt;More cores help when the workload can be divided into multiple tasks.&lt;/p&gt;

&lt;p&gt;A single-threaded program may not benefit significantly from additional cores.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 cores ≠ automatically 4× performance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Parallelism has overhead and depends heavily on the workload.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 3: Thinking RAM is inside the CPU
&lt;/h3&gt;

&lt;p&gt;RAM is normally outside the CPU package and connected through the memory subsystem.&lt;/p&gt;

&lt;p&gt;Registers and CPU caches are much closer to the processor execution units.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4: Thinking the CPU directly executes Java or Python
&lt;/h3&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;p&gt;High-level languages use compilers, interpreters, virtual machines, or combinations of these mechanisms to eventually produce instructions the processor can execute.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Instruction Set Architecture
&lt;/h3&gt;

&lt;p&gt;A CPU doesn't just execute arbitrary instructions.&lt;/p&gt;

&lt;p&gt;It follows an &lt;strong&gt;Instruction Set Architecture&lt;/strong&gt;, commonly called an ISA.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x86-64
ARM64
RISC-V
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ISA defines things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Available instructions&lt;/li&gt;
&lt;li&gt;Registers&lt;/li&gt;
&lt;li&gt;Data types&lt;/li&gt;
&lt;li&gt;Memory operations&lt;/li&gt;
&lt;li&gt;Instruction formats&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why software compiled for one architecture may not directly run on another architecture.&lt;/p&gt;




&lt;h3&gt;
  
  
  Pipeline
&lt;/h3&gt;

&lt;p&gt;Modern CPUs don't necessarily wait for one instruction to completely finish before starting the next.&lt;/p&gt;

&lt;p&gt;They use &lt;strong&gt;instruction pipelining&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Instruction 1 → Fetch → Decode → Execute
Instruction 2          → Fetch → Decode → Execute
Instruction 3                   → Fetch → Decode → Execute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows different stages of multiple instructions to be processed simultaneously.&lt;/p&gt;




&lt;h3&gt;
  
  
  Branch Prediction
&lt;/h3&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CPU may need to determine which path the program will take.&lt;/p&gt;

&lt;p&gt;Modern CPUs use &lt;strong&gt;branch prediction&lt;/strong&gt; to guess the likely path.&lt;/p&gt;

&lt;p&gt;If the prediction is correct, execution can continue efficiently.&lt;/p&gt;

&lt;p&gt;If it is wrong, the CPU may need to discard speculative work and recover.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cache Hierarchy
&lt;/h3&gt;

&lt;p&gt;A simplified memory hierarchy looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        CPU Registers
             ↓
          L1 Cache
             ↓
          L2 Cache
             ↓
          L3 Cache
             ↓
            RAM
             ↓
           SSD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The closer the data is to the execution units, the faster it can generally be accessed.&lt;/p&gt;

&lt;p&gt;This is one reason algorithms and data structures that have good &lt;strong&gt;cache locality&lt;/strong&gt; can perform significantly better.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A CPU is much more than a component that "does calculations."&lt;/p&gt;

&lt;p&gt;It contains multiple systems working together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              CPU
               |
      +--------+--------+
      |                 |
 Control Unit          ALU
      |                 |
      +--------+--------+
               |
           Registers
               |
             Cache
               |
              RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CPU repeatedly performs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fetch
  ↓
Decode
  ↓
Execute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most important concepts to remember are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Control Unit&lt;/strong&gt; coordinates instruction execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ALU&lt;/strong&gt; performs arithmetic and logical operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Registers&lt;/strong&gt; provide extremely fast temporary storage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache&lt;/strong&gt; keeps frequently accessed data close to the CPU.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cores&lt;/strong&gt; allow multiple instruction streams to execute concurrently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ISA&lt;/strong&gt; defines the instructions a processor understands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pipelining&lt;/strong&gt; allows multiple instructions to be processed at different stages simultaneously.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;there is an enormous amount of engineering happening underneath that single line.&lt;/p&gt;

&lt;p&gt;The more you understand that hidden layer, the easier it becomes to reason about performance, operating systems, compilers, and low-level programming.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>algorithms</category>
      <category>cpu</category>
      <category>architecture</category>
    </item>
    <item>
      <title>How Memory Works (RAM vs Storage)</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Sat, 08 Aug 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/how-memory-works-ram-vs-storage-4ihh</link>
      <guid>https://dev.to/polyvexr/how-memory-works-ram-vs-storage-4ihh</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;If you've ever bought a laptop or smartphone, you've probably seen specifications like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;8 GB RAM&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;512 GB SSD&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1 TB Storage&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many beginners assume RAM and storage are the same thing because both are measured in gigabytes.&lt;/p&gt;

&lt;p&gt;They are not.&lt;/p&gt;

&lt;p&gt;Understanding the difference between RAM and storage helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose the right computer for your needs.&lt;/li&gt;
&lt;li&gt;Understand why applications become slow.&lt;/li&gt;
&lt;li&gt;Learn how operating systems manage programs.&lt;/li&gt;
&lt;li&gt;Build a strong foundation for operating systems and computer architecture.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine you have a laptop with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;16 GB RAM
512 GB SSD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A common question is:&lt;/p&gt;

&lt;p&gt;"If my SSD is much larger, why can't the computer use it as RAM?"&lt;/p&gt;

&lt;p&gt;Another question is:&lt;/p&gt;

&lt;p&gt;"Why do applications close when the computer loses power, but my files remain?"&lt;/p&gt;

&lt;p&gt;To answer these questions, we need to understand what RAM and storage actually do.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;A computer has two main places where data is kept.&lt;/p&gt;

&lt;h3&gt;
  
  
  RAM (Random Access Memory)
&lt;/h3&gt;

&lt;p&gt;RAM is the computer's &lt;strong&gt;temporary working memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you open an application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The operating system loads it from storage into RAM.&lt;/li&gt;
&lt;li&gt;The CPU executes instructions from RAM.&lt;/li&gt;
&lt;li&gt;Data stays there only while the computer is running.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When power is turned off:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RAM → Everything is erased.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Storage (SSD/HDD)
&lt;/h3&gt;

&lt;p&gt;Storage is the computer's &lt;strong&gt;permanent memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Operating system&lt;/li&gt;
&lt;li&gt;Applications&lt;/li&gt;
&lt;li&gt;Photos&lt;/li&gt;
&lt;li&gt;Videos&lt;/li&gt;
&lt;li&gt;Documents&lt;/li&gt;
&lt;li&gt;Games&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike RAM, storage keeps data even when the computer is turned off.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SSD/HDD → Data remains.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;Think of your computer like a student studying for an exam.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;bookshelf&lt;/strong&gt; contains all the books.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;study table&lt;/strong&gt; is where the student actually works.&lt;/p&gt;

&lt;p&gt;In this analogy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bookshelf = Storage

Study Table = RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't study directly from the bookshelf.&lt;/p&gt;

&lt;p&gt;You first bring the required books to the table.&lt;/p&gt;

&lt;p&gt;Similarly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Storage

↓

RAM

↓

CPU

↓

Output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Programs must be loaded into RAM before the CPU can execute them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine you're cooking dinner.&lt;/p&gt;

&lt;p&gt;The refrigerator stores all your ingredients.&lt;/p&gt;

&lt;p&gt;The kitchen counter is where you prepare the meal.&lt;/p&gt;

&lt;p&gt;The chef works on the counter, not inside the refrigerator.&lt;/p&gt;

&lt;p&gt;In this analogy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refrigerator = Storage&lt;/li&gt;
&lt;li&gt;Kitchen Counter = RAM&lt;/li&gt;
&lt;li&gt;Chef = CPU&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you're finished cooking, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put unused ingredients back into the refrigerator.&lt;/li&gt;
&lt;li&gt;Clean the counter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The counter becomes empty.&lt;/p&gt;

&lt;p&gt;Likewise, RAM is cleared when the computer shuts down.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Consider this simple Java program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens internally?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Java program is stored on the SSD.&lt;/li&gt;
&lt;li&gt;When you run it, the operating system loads it into RAM.&lt;/li&gt;
&lt;li&gt;The JVM starts executing the program from RAM.&lt;/li&gt;
&lt;li&gt;The CPU performs the calculations.&lt;/li&gt;
&lt;li&gt;The output appears on the screen.&lt;/li&gt;
&lt;li&gt;When the program closes, its memory is released.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The file remains on the SSD even after execution.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1
&lt;/h3&gt;

&lt;p&gt;Thinking RAM stores files permanently.&lt;/p&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;p&gt;Anything stored only in RAM disappears after power is lost.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 2
&lt;/h3&gt;

&lt;p&gt;Believing more storage makes the computer faster.&lt;/p&gt;

&lt;p&gt;A larger SSD gives you more space.&lt;/p&gt;

&lt;p&gt;It does not necessarily improve performance.&lt;/p&gt;

&lt;p&gt;Performance depends on factors such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU&lt;/li&gt;
&lt;li&gt;RAM&lt;/li&gt;
&lt;li&gt;SSD speed&lt;/li&gt;
&lt;li&gt;Software optimization&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Mistake 3
&lt;/h3&gt;

&lt;p&gt;Thinking RAM and storage perform the same job.&lt;/p&gt;

&lt;p&gt;They serve completely different purposes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RAM

Temporary
Fast
Working Memory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Storage

Permanent
Slower
File Storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Mistake 4
&lt;/h3&gt;

&lt;p&gt;Closing an application does not delete it.&lt;/p&gt;

&lt;p&gt;Closing an application removes it from RAM.&lt;/p&gt;

&lt;p&gt;The application itself still exists on storage.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why Is RAM Faster?
&lt;/h3&gt;

&lt;p&gt;RAM is designed for extremely fast access.&lt;/p&gt;

&lt;p&gt;Typical access times:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Device&lt;/th&gt;
&lt;th&gt;Approximate Access Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU Cache&lt;/td&gt;
&lt;td&gt;Few nanoseconds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RAM&lt;/td&gt;
&lt;td&gt;Tens of nanoseconds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SSD&lt;/td&gt;
&lt;td&gt;Tens to hundreds of microseconds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HDD&lt;/td&gt;
&lt;td&gt;Several milliseconds&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because RAM is much faster than storage, the CPU can execute programs efficiently.&lt;/p&gt;




&lt;h3&gt;
  
  
  What Happens When RAM Is Full?
&lt;/h3&gt;

&lt;p&gt;If RAM becomes full:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The operating system moves less frequently used data to storage.&lt;/li&gt;
&lt;li&gt;This process is called &lt;strong&gt;paging&lt;/strong&gt; or &lt;strong&gt;swapping&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Because storage is much slower than RAM, the computer becomes noticeably slower.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  RAM Is Volatile
&lt;/h3&gt;

&lt;p&gt;RAM is &lt;strong&gt;volatile memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Power Off

↓

Data Lost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Storage is &lt;strong&gt;non-volatile&lt;/strong&gt;, meaning it keeps data without electricity.&lt;/p&gt;




&lt;h3&gt;
  
  
  SSD vs HDD
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SSD&lt;/th&gt;
&lt;th&gt;HDD&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No moving parts&lt;/td&gt;
&lt;td&gt;Mechanical disks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Faster&lt;/td&gt;
&lt;td&gt;Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Silent&lt;/td&gt;
&lt;td&gt;Makes noise&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;More durable&lt;/td&gt;
&lt;td&gt;More fragile&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lower access time&lt;/td&gt;
&lt;td&gt;Higher access time&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most modern computers use SSDs because they significantly reduce application and boot times.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;RAM and storage work together but serve different purposes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application Stored on SSD

↓

Operating System Loads It

↓

RAM

↓

CPU Executes Instructions

↓

Program Runs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Remember these key differences:
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;RAM&lt;/th&gt;
&lt;th&gt;Storage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Temporary&lt;/td&gt;
&lt;td&gt;Permanent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Very Fast&lt;/td&gt;
&lt;td&gt;Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stores running programs&lt;/td&gt;
&lt;td&gt;Stores files and applications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data lost when power is off&lt;/td&gt;
&lt;td&gt;Data remains after shutdown&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A good way to remember:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Storage remembers everything. RAM remembers only what the computer is using right now.&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>ram</category>
      <category>data</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Bits, Bytes, KB, MB, GB, TB Explained Simply</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Fri, 07 Aug 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/bits-bytes-kb-mb-gb-tb-explained-simply-1mhp</link>
      <guid>https://dev.to/polyvexr/bits-bytes-kb-mb-gb-tb-explained-simply-1mhp</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A photo is &lt;strong&gt;3 MB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;A movie is &lt;strong&gt;2 GB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Your laptop has &lt;strong&gt;16 GB RAM&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;An SSD is &lt;strong&gt;512 GB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Internet speed is advertised as &lt;strong&gt;100 Mbps&lt;/strong&gt; instead of &lt;strong&gt;100 MBps&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These units appear everywhere in computing, yet they're often misunderstood.&lt;/p&gt;

&lt;p&gt;Understanding the difference between &lt;strong&gt;bits&lt;/strong&gt;, &lt;strong&gt;bytes&lt;/strong&gt;, and larger storage units will help you understand how computers store data, measure memory, and transfer information over networks.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Suppose someone tells you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This file is 8000 bits.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another person says:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This file is 1000 bytes.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Are they different?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;They represent the same amount of data.&lt;/p&gt;

&lt;p&gt;Many beginners confuse &lt;strong&gt;bits&lt;/strong&gt; and &lt;strong&gt;bytes&lt;/strong&gt;, leading to mistakes when comparing storage sizes and internet speeds.&lt;/p&gt;

&lt;p&gt;Let's fix that once and for all.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;Everything inside a computer is stored as &lt;strong&gt;binary&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The smallest unit of information is a &lt;strong&gt;bit&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bit

0
or
1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A single bit can represent only one of two values.&lt;/p&gt;

&lt;p&gt;To store useful information, computers group bits together.&lt;/p&gt;

&lt;p&gt;The most common group is called a &lt;strong&gt;byte&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 Byte = 8 Bits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A byte can represent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2⁸ = 256
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;different values.&lt;/p&gt;

&lt;p&gt;This is enough to store one ASCII character.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'A' → 01000001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;Think of bits as individual LEGO bricks.&lt;/p&gt;

&lt;p&gt;One brick alone cannot build much.&lt;/p&gt;

&lt;p&gt;When you combine eight bricks, you can build something useful.&lt;/p&gt;

&lt;p&gt;Similarly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 Bit

↓

8 Bits

↓

1 Byte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Larger groups of bytes are used to measure storage.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1 Byte (B)&lt;/td&gt;
&lt;td&gt;8 Bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Kilobyte (KB)&lt;/td&gt;
&lt;td&gt;1024 Bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Megabyte (MB)&lt;/td&gt;
&lt;td&gt;1024 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Gigabyte (GB)&lt;/td&gt;
&lt;td&gt;1024 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Terabyte (TB)&lt;/td&gt;
&lt;td&gt;1024 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every step is multiplied by &lt;strong&gt;1024&lt;/strong&gt;, not 1000, because computers use binary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine you're transporting books.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One &lt;strong&gt;page&lt;/strong&gt; is like a &lt;strong&gt;bit&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;One &lt;strong&gt;book&lt;/strong&gt; is like a &lt;strong&gt;byte&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;bookshelf&lt;/strong&gt; is like a &lt;strong&gt;kilobyte&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;library&lt;/strong&gt; is like a &lt;strong&gt;gigabyte&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As the amount of information grows, we use larger units to avoid writing huge numbers.&lt;/p&gt;

&lt;p&gt;Instead of saying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1,073,741,824 Bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we simply say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 GB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much easier to read.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Java provides constants that make it easy to calculate storage sizes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="no"&gt;KB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;KB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="no"&gt;GB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="no"&gt;TB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;GB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1 KB = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;KB&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" Bytes"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1 MB = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" Bytes"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1 GB = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;GB&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" Bytes"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1 TB = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;TB&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" Bytes"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 KB = 1024 Bytes
1 MB = 1048576 Bytes
1 GB = 1073741824 Bytes
1 TB = 1099511627776 Bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1
&lt;/h3&gt;

&lt;p&gt;Confusing &lt;strong&gt;bits&lt;/strong&gt; with &lt;strong&gt;bytes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Remember:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b = bit
B = Byte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 Mbps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is &lt;strong&gt;not&lt;/strong&gt; the same as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 MB/s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Mistake 2
&lt;/h3&gt;

&lt;p&gt;Thinking internet speed and file size use the same units.&lt;/p&gt;

&lt;p&gt;Storage devices usually use &lt;strong&gt;bytes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Network speeds usually use &lt;strong&gt;bits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Download Speed

100 Mbps

↓

Actual Maximum Speed

About 12.5 MB/s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 bits = 1 byte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Mistake 3
&lt;/h3&gt;

&lt;p&gt;Believing KB always means exactly 1000 bytes.&lt;/p&gt;

&lt;p&gt;Traditionally in computing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 KB = 1024 Bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, storage manufacturers sometimes use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 KB = 1000 Bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why the available storage on a new hard drive may appear slightly smaller than advertised.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4
&lt;/h3&gt;

&lt;p&gt;Thinking one character always uses one byte.&lt;/p&gt;

&lt;p&gt;Not always.&lt;/p&gt;

&lt;p&gt;ASCII uses one byte.&lt;/p&gt;

&lt;p&gt;Unicode encodings like UTF-8 may use one to four bytes depending on the character.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Binary Prefixes
&lt;/h3&gt;

&lt;p&gt;To avoid confusion, international standards introduced binary prefixes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decimal Prefix&lt;/th&gt;
&lt;th&gt;Binary Prefix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;KB = 1000 Bytes&lt;/td&gt;
&lt;td&gt;KiB = 1024 Bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MB = 1000 KB&lt;/td&gt;
&lt;td&gt;MiB = 1024 KiB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GB = 1000 MB&lt;/td&gt;
&lt;td&gt;GiB = 1024 MiB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TB = 1000 GB&lt;/td&gt;
&lt;td&gt;TiB = 1024 GiB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Operating systems and manufacturers do not always display these prefixes consistently.&lt;/p&gt;




&lt;h3&gt;
  
  
  RAM
&lt;/h3&gt;

&lt;p&gt;When you buy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;16 GB RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it means your computer can temporarily store roughly sixteen billion bytes of data while programs are running.&lt;/p&gt;

&lt;p&gt;RAM is temporary storage.&lt;/p&gt;

&lt;p&gt;Its contents disappear when the computer is turned off.&lt;/p&gt;




&lt;h3&gt;
  
  
  Storage Devices
&lt;/h3&gt;

&lt;p&gt;Typical storage sizes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Device&lt;/th&gt;
&lt;th&gt;Capacity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;USB Drive&lt;/td&gt;
&lt;td&gt;16 GB to 256 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SSD&lt;/td&gt;
&lt;td&gt;256 GB to 4 TB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HDD&lt;/td&gt;
&lt;td&gt;1 TB to 20 TB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These values represent how much data can be stored.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why 1024?
&lt;/h3&gt;

&lt;p&gt;Since computers work in binary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2¹⁰ = 1024
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the closest binary value to 1000, making it a natural choice for measuring memory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Everything stored inside a computer is ultimately made of bits.&lt;/p&gt;

&lt;p&gt;Those bits are grouped into bytes, and bytes are grouped into larger storage units.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 Bit

↓

8 Bits

↓

1 Byte

↓

1024 Bytes

↓

1 KB

↓

1024 KB

↓

1 MB

↓

1024 MB

↓

1 GB

↓

1024 GB

↓

1 TB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Remember these key facts:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A bit is the smallest unit of data.&lt;/li&gt;
&lt;li&gt;One byte contains eight bits.&lt;/li&gt;
&lt;li&gt;Storage is usually measured in bytes.&lt;/li&gt;
&lt;li&gt;Internet speed is usually measured in bits.&lt;/li&gt;
&lt;li&gt;Larger units help us represent huge amounts of data more conveniently.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these units makes it much easier to compare storage devices, estimate download times, and understand how computers manage information.&lt;/p&gt;




</description>
      <category>network</category>
      <category>data</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hexadecimal and Why Programmers Use It</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Thu, 06 Aug 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/hexadecimal-and-why-programmers-use-it-5dk8</link>
      <guid>https://dev.to/polyvexr/hexadecimal-and-why-programmers-use-it-5dk8</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;After learning binary, you'll quickly notice a problem.&lt;/p&gt;

&lt;p&gt;Binary numbers become very long and difficult to read.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1111111111111111
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you tell what number that is at a glance?&lt;/p&gt;

&lt;p&gt;Probably not.&lt;/p&gt;

&lt;p&gt;This is why programmers often use &lt;strong&gt;Hexadecimal (Hex)&lt;/strong&gt;. It is much shorter, easier to read, and maps perfectly to binary.&lt;/p&gt;

&lt;p&gt;You'll encounter hexadecimal in many places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory addresses&lt;/li&gt;
&lt;li&gt;Colors in web development&lt;/li&gt;
&lt;li&gt;Debugging tools&lt;/li&gt;
&lt;li&gt;Machine code&lt;/li&gt;
&lt;li&gt;Network protocols&lt;/li&gt;
&lt;li&gt;Cryptography&lt;/li&gt;
&lt;li&gt;File formats&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding hexadecimal is an essential skill for every programmer.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Computers store everything in binary.&lt;/p&gt;

&lt;p&gt;Humans, however, struggle to read long binary numbers.&lt;/p&gt;

&lt;p&gt;Consider this binary value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1111000010101101
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is correct, but not very readable.&lt;/p&gt;

&lt;p&gt;Now look at the same value in hexadecimal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;F0AD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much shorter.&lt;/p&gt;

&lt;p&gt;Both represent exactly the same data.&lt;/p&gt;

&lt;p&gt;Hexadecimal exists to make binary easier for humans to work with.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;Hexadecimal is a &lt;strong&gt;Base 16&lt;/strong&gt; number system.&lt;/p&gt;

&lt;p&gt;Instead of using ten symbols like decimal, it uses sixteen.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decimal&lt;/th&gt;
&lt;th&gt;Hex&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;After 9, hexadecimal continues with letters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;The reason hexadecimal is so useful is simple.&lt;/p&gt;

&lt;p&gt;One hexadecimal digit represents exactly &lt;strong&gt;4 binary bits&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary&lt;/th&gt;
&lt;th&gt;Hex&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0000&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0001&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0010&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0011&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0100&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0101&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0110&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0111&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1000&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1001&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1010&lt;/td&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1011&lt;/td&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1100&lt;/td&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1101&lt;/td&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1110&lt;/td&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1111&lt;/td&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Binary

1111 0000 1010 1101
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Split into groups of four bits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1111   0000   1010   1101
  F      0      A      D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1111000010101101₂ = F0AD₁₆
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No complicated calculations are required.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine writing a phone number.&lt;/p&gt;

&lt;p&gt;Which is easier to remember?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9876543210
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9 876 543 210
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both contain the same information.&lt;/p&gt;

&lt;p&gt;The second version is easier to read because it is grouped.&lt;/p&gt;

&lt;p&gt;Hexadecimal does something similar for binary.&lt;/p&gt;

&lt;p&gt;Instead of looking at sixteen individual bits, programmers look at four hexadecimal digits.&lt;/p&gt;

&lt;p&gt;The information is identical, but the representation is much cleaner.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Convert a decimal number to hexadecimal in Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toHexString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Convert hexadecimal back to decimal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"FF"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;255
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also display hexadecimal values with the common &lt;code&gt;0x&lt;/code&gt; prefix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;255
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1
&lt;/h3&gt;

&lt;p&gt;Thinking hexadecimal is another way of storing data.&lt;/p&gt;

&lt;p&gt;It is not.&lt;/p&gt;

&lt;p&gt;Computers still store everything in binary.&lt;/p&gt;

&lt;p&gt;Hexadecimal is simply a human-friendly representation of binary.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 2
&lt;/h3&gt;

&lt;p&gt;Treating A through F as characters.&lt;/p&gt;

&lt;p&gt;In hexadecimal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They are digits, not letters.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 3
&lt;/h3&gt;

&lt;p&gt;Forgetting the base.&lt;/p&gt;

&lt;p&gt;The number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;has different meanings depending on its base.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10₂  = 2
10₁₀ = 10
10₁₆ = 16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always know which number system you are working with.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4
&lt;/h3&gt;

&lt;p&gt;Ignoring the &lt;code&gt;0x&lt;/code&gt; prefix.&lt;/p&gt;

&lt;p&gt;Many programming languages write hexadecimal numbers like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x1A
0xFF
0xABC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prefix tells the compiler that the value is hexadecimal.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Memory Addresses
&lt;/h3&gt;

&lt;p&gt;Most debuggers display memory addresses in hexadecimal.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x7FFE9A34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hexadecimal makes addresses much shorter than binary.&lt;/p&gt;




&lt;h3&gt;
  
  
  Web Colors
&lt;/h3&gt;

&lt;p&gt;Every color on a web page is written in hexadecimal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#FFFFFF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;White&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Black&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#FF0000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Red&lt;/p&gt;

&lt;p&gt;Each pair represents the intensity of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Red&lt;/li&gt;
&lt;li&gt;Green&lt;/li&gt;
&lt;li&gt;Blue&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Bytes and Hex
&lt;/h3&gt;

&lt;p&gt;One byte contains 8 bits.&lt;/p&gt;

&lt;p&gt;Since one hexadecimal digit represents 4 bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 Bits = 2 Hex Digits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11111111

↓

FF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This relationship makes hexadecimal ideal for representing bytes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Debugging
&lt;/h3&gt;

&lt;p&gt;When debugging applications, you will often see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error codes&lt;/li&gt;
&lt;li&gt;Memory dumps&lt;/li&gt;
&lt;li&gt;CPU registers&lt;/li&gt;
&lt;li&gt;Machine instructions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are usually displayed in hexadecimal because it is compact while still maintaining a direct relationship with binary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Binary is the language computers understand.&lt;/p&gt;

&lt;p&gt;Hexadecimal is the language programmers use to read binary more easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Decimal

255

↓

Binary

11111111

↓

Hexadecimal

FF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember these key facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hexadecimal is Base 16.&lt;/li&gt;
&lt;li&gt;It uses digits 0 to 9 and letters A to F.&lt;/li&gt;
&lt;li&gt;One hexadecimal digit equals four binary bits.&lt;/li&gt;
&lt;li&gt;Two hexadecimal digits equal one byte.&lt;/li&gt;
&lt;li&gt;Computers store binary, not hexadecimal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you understand hexadecimal, topics like memory, networking, operating systems, and low-level programming become much easier to explore.&lt;/p&gt;




&lt;p&gt;Suggested additions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Programs for converting between decimal, binary, and hexadecimal.&lt;/li&gt;
&lt;li&gt;Practice problems with solutions.&lt;/li&gt;
&lt;li&gt;A cheat sheet showing every 4-bit binary group and its hexadecimal equivalent.&lt;/li&gt;
&lt;li&gt;Diagrams explaining how bytes map to hexadecimal values.&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>java</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Binary Numbers Explained Without Confusion</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Wed, 05 Aug 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/binary-numbers-explained-without-confusion-5d4m</link>
      <guid>https://dev.to/polyvexr/binary-numbers-explained-without-confusion-5d4m</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;Every piece of data inside a computer is stored using binary. Whether it is a photo, a video, a game, a document, or the program you wrote yesterday, everything eventually becomes a sequence of &lt;strong&gt;0s&lt;/strong&gt; and &lt;strong&gt;1s&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding binary is one of the first steps toward learning how computers really work. It also makes topics like memory, networking, data structures, operating systems, and computer architecture much easier to understand.&lt;/p&gt;

&lt;p&gt;If you have ever wondered why computers use only two numbers instead of ten, this article is for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Humans naturally count using decimal numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 2 3 4 5 6 7 8 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Computers, however, use only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This raises a few obvious questions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why only two numbers?&lt;/li&gt;
&lt;li&gt;How can a computer represent large numbers with only 0 and 1?&lt;/li&gt;
&lt;li&gt;How can letters, images, music, and videos all be represented using binary?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The answer starts with understanding how computer hardware works.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;A computer is built from billions of tiny electronic switches called &lt;strong&gt;transistors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each transistor has only two stable states.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OFF
ON
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We represent these states as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OFF = 0
ON  = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because hardware naturally works with two states, computers use the &lt;strong&gt;Binary Number System&lt;/strong&gt;, also known as &lt;strong&gt;Base 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike our decimal system, which has ten digits, binary has only two.&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;Think of binary as another way of counting.&lt;/p&gt;

&lt;p&gt;Decimal uses powers of 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10³ 10² 10¹ 10⁰
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Binary uses powers of 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2⁷ 2⁶ 2⁵ 2⁴ 2³ 2² 2¹ 2⁰
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every position represents a power of two.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Binary: 1011

Position: 2³ 2² 2¹ 2⁰
Values:     8  4  2  1
Digits:     1  0  1  1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now multiply each digit by its value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 × 8 = 8
0 × 4 = 0
1 × 2 = 2
1 × 1 = 1

Total = 11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1011₂ = 11₁₀
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The small numbers indicate the number system being used.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine four light switches.&lt;/p&gt;

&lt;p&gt;Each switch can only be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OFF
ON
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose each switch has a value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 4 2 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now turn on only the switches worth 8, 2, and 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ON  OFF  ON  ON

1    0    1    1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The total becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 + 2 + 1 = 11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly how binary numbers work.&lt;/p&gt;

&lt;p&gt;Every bit is simply another switch.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Java makes it easy to work with binary numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toBinaryString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1101
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To convert a binary string back into a decimal number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1101"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1
&lt;/h3&gt;

&lt;p&gt;Reading binary like a decimal number.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;not&lt;/strong&gt; one thousand and ten.&lt;/p&gt;

&lt;p&gt;It represents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 + 2 = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Mistake 2
&lt;/h3&gt;

&lt;p&gt;Ignoring place values.&lt;/p&gt;

&lt;p&gt;Each position in binary represents a power of two, not a power of ten.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;128 64 32 16 8 4 2 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always start counting from the rightmost bit.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 3
&lt;/h3&gt;

&lt;p&gt;Thinking more digits always mean a larger decimal number.&lt;/p&gt;

&lt;p&gt;Compare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1111 = 15

10000 = 16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding one more bit increases the range dramatically.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4
&lt;/h3&gt;

&lt;p&gt;Confusing bits and bytes.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;bit&lt;/strong&gt; is a single binary digit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;strong&gt;byte&lt;/strong&gt; contains &lt;strong&gt;8 bits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11001010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Binary Prefixes
&lt;/h3&gt;

&lt;p&gt;Computers group binary digits into larger units.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1 Bit&lt;/td&gt;
&lt;td&gt;0 or 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Nibble&lt;/td&gt;
&lt;td&gt;4 Bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Byte&lt;/td&gt;
&lt;td&gt;8 Bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Kilobyte&lt;/td&gt;
&lt;td&gt;1024 Bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Megabyte&lt;/td&gt;
&lt;td&gt;1024 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 Gigabyte&lt;/td&gt;
&lt;td&gt;1024 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Why 8 Bits?
&lt;/h3&gt;

&lt;p&gt;Eight bits can represent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2⁸ = 256
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;different values.&lt;/p&gt;

&lt;p&gt;That is enough to store characters, colours, and many small numbers efficiently.&lt;/p&gt;




&lt;h3&gt;
  
  
  Signed Numbers
&lt;/h3&gt;

&lt;p&gt;Computers also represent negative numbers.&lt;/p&gt;

&lt;p&gt;Most modern systems use &lt;strong&gt;Two's Complement&lt;/strong&gt;, allowing positive and negative numbers to be stored efficiently using the same binary representation.&lt;/p&gt;

&lt;p&gt;This is a topic worth learning once you are comfortable with basic binary.&lt;/p&gt;




&lt;h3&gt;
  
  
  Binary Beyond Numbers
&lt;/h3&gt;

&lt;p&gt;Binary is not just used for integers.&lt;/p&gt;

&lt;p&gt;Everything eventually becomes binary.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Characters use ASCII or Unicode.&lt;/li&gt;
&lt;li&gt;Images store colour values as binary.&lt;/li&gt;
&lt;li&gt;Audio stores sound samples as binary.&lt;/li&gt;
&lt;li&gt;Videos are sequences of binary encoded frames.&lt;/li&gt;
&lt;li&gt;Programs themselves are binary instructions executed by the CPU.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Binary is the universal language of computers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Binary may look unfamiliar at first, but it is simply another number system.&lt;/p&gt;

&lt;p&gt;Instead of using ten digits, it uses only two.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Decimal
0 1 2 3 4 5 6 7 8 9

Binary
0 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each binary digit represents a power of two.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;128 64 32 16 8 4 2 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding binary helps explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How computers store numbers.&lt;/li&gt;
&lt;li&gt;How memory works.&lt;/li&gt;
&lt;li&gt;How processors execute instructions.&lt;/li&gt;
&lt;li&gt;How files and programs are represented internally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Master binary once, and many advanced computer science topics become much easier to understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  Topics to explore next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Decimal to Binary Conversion&lt;/li&gt;
&lt;li&gt;Binary to Decimal Conversion&lt;/li&gt;
&lt;li&gt;Hexadecimal Numbers&lt;/li&gt;
&lt;li&gt;Octal Number System&lt;/li&gt;
&lt;li&gt;Bits vs Bytes&lt;/li&gt;
&lt;li&gt;ASCII and Unicode&lt;/li&gt;
&lt;li&gt;Two's Complement&lt;/li&gt;
&lt;li&gt;Bitwise Operators&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>algorithms</category>
      <category>development</category>
      <category>programming</category>
      <category>programmers</category>
    </item>
    <item>
      <title>How a Computer Actually Runs Your Code</title>
      <dc:creator>Shankar L</dc:creator>
      <pubDate>Sun, 19 Jul 2026 04:30:00 +0000</pubDate>
      <link>https://dev.to/polyvexr/how-a-computer-actually-runs-your-code-3klb</link>
      <guid>https://dev.to/polyvexr/how-a-computer-actually-runs-your-code-3klb</guid>
      <description>&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;Every developer writes code every day, but very few understand what actually happens after pressing the &lt;strong&gt;Run&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;Whether you write Java, Python, C++, or JavaScript, your code does not magically execute. It goes through several transformations before the CPU can understand it.&lt;/p&gt;

&lt;p&gt;Understanding this process helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write better and more efficient code.&lt;/li&gt;
&lt;li&gt;Debug errors more effectively.&lt;/li&gt;
&lt;li&gt;Learn programming languages faster.&lt;/li&gt;
&lt;li&gt;Understand compilers, interpreters, virtual machines, and operating systems.&lt;/li&gt;
&lt;li&gt;Build a strong foundation for system design, operating systems, and computer architecture.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Programming becomes much easier when you know what is happening behind the scenes.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine writing the following Java program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You click &lt;strong&gt;Run&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A second later, your terminal prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how?&lt;/p&gt;

&lt;p&gt;Your computer only understands electrical signals represented as binary instructions consisting of 0s and 1s.&lt;/p&gt;

&lt;p&gt;It does not understand words like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;cout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So who translates your code into something the processor can execute?&lt;/p&gt;




&lt;h2&gt;
  
  
  The Concept
&lt;/h2&gt;

&lt;p&gt;Every program follows a pipeline before it reaches the CPU.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code
      ↓
Compiler / Interpreter
      ↓
Machine Instructions
      ↓
Operating System
      ↓
CPU Execution
      ↓
Output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each stage has a specific responsibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: You write source code
&lt;/h3&gt;

&lt;p&gt;This is the human readable program.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Humans understand this language.&lt;/p&gt;

&lt;p&gt;Computers do not.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Translation
&lt;/h3&gt;

&lt;p&gt;This stage depends on the programming language.&lt;/p&gt;

&lt;p&gt;Compiled languages like C++ convert the entire program into machine code before execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C++ Code
      ↓
Compiler
      ↓
Executable (.exe)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interpreted languages like Python execute the program one statement at a time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python Code
      ↓
Interpreter
      ↓
Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Languages like Java use a hybrid approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Java Code
      ↓
Java Compiler
      ↓
Bytecode
      ↓
JVM
      ↓
Machine Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 3: Operating System
&lt;/h3&gt;

&lt;p&gt;The operating system loads the program into memory.&lt;/p&gt;

&lt;p&gt;It provides the program with resources such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RAM&lt;/li&gt;
&lt;li&gt;CPU time&lt;/li&gt;
&lt;li&gt;File access&lt;/li&gt;
&lt;li&gt;Keyboard input&lt;/li&gt;
&lt;li&gt;Display output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without an operating system, most applications would not know how to communicate with hardware.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 4: CPU Execution
&lt;/h3&gt;

&lt;p&gt;The CPU executes instructions using a continuous cycle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fetch
Decode
Execute
Repeat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every instruction, the processor:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fetches the next instruction from memory.&lt;/li&gt;
&lt;li&gt;Decodes what the instruction means.&lt;/li&gt;
&lt;li&gt;Executes it.&lt;/li&gt;
&lt;li&gt;Moves to the next instruction.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This cycle repeats billions of times every second.&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Explanation
&lt;/h2&gt;

&lt;p&gt;Think of programming languages as different spoken languages.&lt;/p&gt;

&lt;p&gt;You may speak English.&lt;/p&gt;

&lt;p&gt;Someone else speaks Japanese.&lt;/p&gt;

&lt;p&gt;The CPU speaks only one language.&lt;/p&gt;

&lt;p&gt;Machine code.&lt;/p&gt;

&lt;p&gt;Every programming language must eventually be translated into machine instructions before execution.&lt;/p&gt;

&lt;p&gt;No exceptions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real world Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine ordering food at a restaurant.&lt;/p&gt;

&lt;p&gt;You tell the waiter:&lt;/p&gt;

&lt;p&gt;"I want a pizza."&lt;/p&gt;

&lt;p&gt;The waiter writes your order in a format the kitchen understands.&lt;/p&gt;

&lt;p&gt;The chef prepares the pizza.&lt;/p&gt;

&lt;p&gt;The waiter brings it back to you.&lt;/p&gt;

&lt;p&gt;In this analogy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are the programmer.&lt;/li&gt;
&lt;li&gt;Your source code is your order.&lt;/li&gt;
&lt;li&gt;The compiler or interpreter is the waiter.&lt;/li&gt;
&lt;li&gt;The CPU is the chef.&lt;/li&gt;
&lt;li&gt;The finished dish is the program output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You never communicate directly with the chef.&lt;/p&gt;

&lt;p&gt;Similarly, your code never communicates directly with the CPU.&lt;/p&gt;

&lt;p&gt;A translator is always involved.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example
&lt;/h2&gt;

&lt;p&gt;Consider this simple C program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler converts this into thousands of machine instructions.&lt;/p&gt;

&lt;p&gt;Conceptually, the CPU performs operations similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nasm"&gt;&lt;code&gt;&lt;span class="nf"&gt;LOAD&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="nf"&gt;LOAD&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="nf"&gt;ADD&lt;/span&gt;
&lt;span class="nf"&gt;STORE&lt;/span&gt; &lt;span class="nv"&gt;RESULT&lt;/span&gt;
&lt;span class="nf"&gt;PRINT&lt;/span&gt; &lt;span class="nv"&gt;RESULT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual machine instructions are much more complex, but the idea remains the same.&lt;/p&gt;

&lt;p&gt;Every high level instruction eventually becomes low level CPU instructions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1
&lt;/h3&gt;

&lt;p&gt;Believing the CPU understands Java, Python, or C++ directly.&lt;/p&gt;

&lt;p&gt;Reality:&lt;/p&gt;

&lt;p&gt;The CPU only understands machine code.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 2
&lt;/h3&gt;

&lt;p&gt;Thinking compiled languages are always faster.&lt;/p&gt;

&lt;p&gt;Reality:&lt;/p&gt;

&lt;p&gt;Performance depends on many factors including compiler optimizations, runtime environment, memory management, and the algorithm itself.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 3
&lt;/h3&gt;

&lt;p&gt;Confusing the compiler with the operating system.&lt;/p&gt;

&lt;p&gt;The compiler translates code.&lt;/p&gt;

&lt;p&gt;The operating system manages resources and executes programs.&lt;/p&gt;

&lt;p&gt;They solve different problems.&lt;/p&gt;




&lt;h3&gt;
  
  
  Mistake 4
&lt;/h3&gt;

&lt;p&gt;Assuming one line of code equals one CPU instruction.&lt;/p&gt;

&lt;p&gt;In reality, one line of source code may become dozens or even hundreds of machine instructions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Notes
&lt;/h2&gt;

&lt;p&gt;Once you understand the basic pipeline, you can explore what happens internally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiler Optimizations
&lt;/h3&gt;

&lt;p&gt;Modern compilers rewrite your program to make it faster without changing its behavior.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dead code elimination&lt;/li&gt;
&lt;li&gt;Function inlining&lt;/li&gt;
&lt;li&gt;Constant folding&lt;/li&gt;
&lt;li&gt;Loop unrolling&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Virtual Machines
&lt;/h3&gt;

&lt;p&gt;Java and C# use virtual machines.&lt;/p&gt;

&lt;p&gt;Instead of producing machine code immediately, they generate intermediate code.&lt;/p&gt;

&lt;p&gt;The virtual machine later converts this into optimized machine instructions during execution.&lt;/p&gt;

&lt;p&gt;This technique is known as Just In Time compilation.&lt;/p&gt;




&lt;h3&gt;
  
  
  CPU Cache
&lt;/h3&gt;

&lt;p&gt;The processor stores frequently used instructions and data in small, high speed memory called cache.&lt;/p&gt;

&lt;p&gt;Accessing cache is much faster than accessing RAM.&lt;/p&gt;

&lt;p&gt;Understanding cache becomes important when optimizing performance.&lt;/p&gt;




&lt;h3&gt;
  
  
  System Calls
&lt;/h3&gt;

&lt;p&gt;Whenever your program reads a file, accesses the internet, or prints text to the terminal, it requests the operating system to perform those tasks through system calls.&lt;/p&gt;

&lt;p&gt;User programs cannot directly control hardware.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Every program follows a journey before producing output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You write code
        ↓
Compiler or Interpreter translates it
        ↓
Operating System loads it
        ↓
CPU executes machine instructions
        ↓
Output appears
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you understand this pipeline, many advanced topics become easier to learn, including operating systems, compilers, computer architecture, memory management, and system design.&lt;/p&gt;

&lt;p&gt;Every programming language may look different on the surface, but underneath they all follow the same fundamental path to reach the CPU.&lt;/p&gt;




</description>
      <category>architecture</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
