<?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>Forem: stockdale roger</title>
    <description>The latest articles on Forem by stockdale roger (@stockdale_roger_999e292a7).</description>
    <link>https://forem.com/stockdale_roger_999e292a7</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2028994%2Fad7bc167-85d7-4d39-87cb-f43650604129.png</url>
      <title>Forem: stockdale roger</title>
      <link>https://forem.com/stockdale_roger_999e292a7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stockdale_roger_999e292a7"/>
    <language>en</language>
    <item>
      <title>Weekly Blog: Four Interesting Things I Encountered This Week</title>
      <dc:creator>stockdale roger</dc:creator>
      <pubDate>Sat, 07 Sep 2024 08:06:47 +0000</pubDate>
      <link>https://forem.com/stockdale_roger_999e292a7/weekly-blog-four-interesting-things-i-encountered-this-week-eo9</link>
      <guid>https://forem.com/stockdale_roger_999e292a7/weekly-blog-four-interesting-things-i-encountered-this-week-eo9</guid>
      <description>&lt;h2&gt;
  
  
  1. Avoiding Confusing File Names in VS Code
&lt;/h2&gt;

&lt;p&gt;When writing C++ files in VS Code, I named a file first.c.cpp. After completing the program, I encountered errors during execution. After 30 minutes of troubleshooting, I discovered the issue lay in the file name:&lt;br&gt;
The .c extension led the IDE to mistakenly identify it as a C program, causing VS Code to use gcc (the C compiler) instead of g++ (the C++ compiler) to compile my code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falx4f0yt7liam1xzvein.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falx4f0yt7liam1xzvein.png" alt="How fool!" width="420" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Caption: How fool!&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;: In the &lt;code&gt;tasks.json&lt;/code&gt; file, change the 'command' line from &lt;code&gt;gcc&lt;/code&gt; to &lt;code&gt;g++&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lesson learned&lt;/strong&gt;: Use clear &lt;code&gt;.cpp&lt;/code&gt; extensions for C++ files to avoid unnecessary confusion.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Java's Cross-Platform Design Philosophy
&lt;/h2&gt;

&lt;p&gt;Java's design philosophy differs significantly from traditional compiled languages:&lt;/p&gt;
&lt;h3&gt;
  
  
  Traditional Compilation:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; Languages like C++ compile directly into machine code for specific 
platforms (e.g., Windows, Mac, Linux)&lt;/li&gt;
&lt;li&gt;The resulting executable files (.exe) can only run on the target platform&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Java's Approach:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The compiler generates intermediate code (bytecode)&lt;/li&gt;
&lt;li&gt;This bytecode can run on any platform with a Java Virtual Machine (JVM) installed&lt;/li&gt;
&lt;li&gt;The JVM is responsible for translating the bytecode into machine code for the current platform&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This design achieves the goal of "Write Once, Run Anywhere," whereas C++ executables (.exe files) are limited to running on a single platform.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:The same program can run on different computers without modification&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;:The additional step in the process can make compilation slightly slower compared to traditional methods&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Write once, Run anywhere&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                             ---------James Gosling
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  3. Two Common Compilation Modes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Debug mode is oriented towards debugging, with fewer optimizations. It's mainly used for debugging programs. &lt;/li&gt;
&lt;li&gt;Release mode is primarily used for generating the release version, focusing on optimization and only retaining basic debugging functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcigcxgdygc8tqwsgb39u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcigcxgdygc8tqwsgb39u.png" alt="OK!" width="649" height="237"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Understanding Forced Type Casting from a Low-Level Perspective
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Little Endian&lt;/strong&gt;: The least significant byte is stored at the lowest address. This storage method emerged to facilitate CPU memory reading, which occurs from low to high addresses. Interestingly, this is opposite to humans typically write numbers.&lt;br&gt;
&lt;strong&gt;For example&lt;/strong&gt;: &lt;br&gt;
Binary representation of  329933 is    &lt;code&gt;00000000  00000101 00001000  11001101&lt;/code&gt;&lt;br&gt;
Little Endian storage: &lt;code&gt;11001101  00001000  00000101  00000000&lt;/code&gt;&lt;br&gt;
As we can see, Little Endian reverses the order of bytes in the binary representation. However, &lt;strong&gt;it's crucial to note that the bit order within each byte remains unchanged!&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  A Fun Game to Understand Forced Type Casting
&lt;/h3&gt;

&lt;p&gt;My favorite experiment for introducing type casting!&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="cp"&gt;# include &amp;lt;iostream&amp;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="err"&gt; &lt;/span&gt; &lt;span class="err"&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="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &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;329933&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;=&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;q&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;Output:&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="mi"&gt;329933&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;51&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;I'm curious why it outputs -51?&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;(char*)&amp;amp;a&lt;/code&gt; points to the first byte of the int. The first byte &lt;code&gt;11001101&lt;/code&gt;is interpreted as a char.&lt;/li&gt;
&lt;li&gt;The highest bit 1 indicates a negative number, after two's complement conversion, we get -51 (those friends who are familiar with two's complement can verify if it indeed represents -51)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Is this a coincidence? Let's try two more examples&lt;/em&gt;&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;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="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&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="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Try it:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Try running the code mentioned above and observe the output.&lt;/li&gt;
&lt;li&gt;Consider why the second and third bytes produce such output. &lt;strong&gt;Feel free to discuss this in the comments section.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Can you apply forced type casting to other data types? Give it a try!&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Additional Info: Two's Complement
&lt;/h4&gt;

&lt;p&gt;When performing forced type casting, &lt;code&gt;(char)p&lt;/code&gt; will point to the address of the first byte of the four-byte int, which is &lt;code&gt;11001101&lt;/code&gt;.&lt;br&gt;
The leftmost 1 represents the negative sign, indicating it's a negative number. After applying two's complement, we get: &lt;code&gt;0110011&lt;/code&gt; (the last 7 bits)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: For positive numbers, the two's complement is simply the binary representation of the decimal number. For negative numbers, the two's complement is obtained by inverting all bits except the leftmost (highest) bit, then adding 1 to the rightmost bit.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Converting this to decimal gives us -51. Interesting, right?&lt;/p&gt;

&lt;h4&gt;
  
  
  Benefits of Two's Complement:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;It allows both positive and integer types to be calculated using only an adder, eliminating the need for a subtractor and simplifying hardware need.&lt;/li&gt;
&lt;li&gt;It provides a unique binary representation for zero. 10000000 doesn't represent -0, but -128, while 00000000 represents 0, not +0.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Many people wonder why it's -128. If you know the answer, feel free to share it in the comments section. This will not only help others but also help you organize your thoughts.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
      <category>datastructures</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
