<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Abhinav Mishra]]></title><description><![CDATA[Decoding the future: high-level programming, performance mastery, and crypto policy trends]]></description><link>https://blog.abhi.dev</link><image><url>https://substackcdn.com/image/fetch/$s_!khXU!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1445f783-fc4d-4944-a25e-5b68b5019f07_652x652.png</url><title>Abhinav Mishra</title><link>https://blog.abhi.dev</link></image><generator>Substack</generator><lastBuildDate>Wed, 06 May 2026 15:06:24 GMT</lastBuildDate><atom:link href="https://blog.abhi.dev/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Abhinav Mishra]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[abhinavmsra@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[abhinavmsra@substack.com]]></itunes:email><itunes:name><![CDATA[Abhinav Mishra]]></itunes:name></itunes:owner><itunes:author><![CDATA[Abhinav Mishra]]></itunes:author><googleplay:owner><![CDATA[abhinavmsra@substack.com]]></googleplay:owner><googleplay:email><![CDATA[abhinavmsra@substack.com]]></googleplay:email><googleplay:author><![CDATA[Abhinav Mishra]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Nepal’s Policy Problem: Late, Broad, and Afraid]]></title><description><![CDATA[A country that writes policy for yesterday while pretending it is preparing for tomorrow]]></description><link>https://blog.abhi.dev/p/nepals-policy-problem-late-broad</link><guid isPermaLink="false">https://blog.abhi.dev/p/nepals-policy-problem-late-broad</guid><dc:creator><![CDATA[Abhinav Mishra]]></dc:creator><pubDate>Mon, 06 Apr 2026 08:56:58 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!NwQ8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>There is a very Nepali style of economic policymaking: <strong>ignore a sector while it is messy and early, panic once other countries have scaled it, and then announce a policy with the tone of national awakening</strong>. By then, the opportunity is older, the margins are thinner, and the neighbors are already on version 3.0.</p><p>That, in one line, is Nepal&#8217;s policy problem: <strong>late, broad, and afraid</strong>.</p><p>Late, because Nepal usually notices an industry after the market has matured elsewhere. Broad, because it responds with sweeping slogans instead of sharp strategy. Afraid, because when something new shows up, the instinct is often to ban it before understanding it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NwQ8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NwQ8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 424w, https://substackcdn.com/image/fetch/$s_!NwQ8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 848w, https://substackcdn.com/image/fetch/$s_!NwQ8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 1272w, https://substackcdn.com/image/fetch/$s_!NwQ8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NwQ8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:420585,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://blog.abhi.dev/i/193234041?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NwQ8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 424w, https://substackcdn.com/image/fetch/$s_!NwQ8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 848w, https://substackcdn.com/image/fetch/$s_!NwQ8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 1272w, https://substackcdn.com/image/fetch/$s_!NwQ8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9751b8e-fce8-4a0d-a4ef-d79284ba0858_1536x1024.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h2><strong>Late</strong></h2><p>The &#8220;late&#8221; part is the easiest to see.</p><p>Nepal only got a formal data-center and cloud-service directive in 2025. By then, India&#8217;s data-center market had already crossed 1 GW in 2024 and is projected to reach 1.8 GW by 2027. Nepal is formalizing the rulebook while the region has already moved into scale.</p><p>That is the first tell. Nepal keeps confusing <strong>finally recognizing an industry</strong> with <strong>being ready for it</strong>. We act as though issuing a directive means we have entered the future. No. More often, it means we have finally acknowledged the present &#8212; late.</p><p>And this lateness would be bad enough on its own. But Nepal is not arriving into an open field. It is arriving into a market that is already far more competitive and far less forgiving.</p><h2><strong>Broad</strong></h2><p>The &#8220;broad&#8221; part is where Nepal becomes especially fond of sounding visionary.</p><p>Instead of choosing narrow, difficult, high-conviction bets, we love giant national language: digital transformation, IT promotion, startup ecosystem, innovation economy, hub status. The problem is not ambition. The problem is vagueness.</p><p>That same vagueness shows up in how Nepal talks about becoming an &#8220;IT hub&#8221;. It sounds ambitious until you remember what market we are actually entering. In the middle of global AI disruption and tech layoffs, Nepal is still tempted to speak about generic IT outsourcing as if it were some untouched frontier. It isn&#8217;t.</p><p>Global tech firms are cutting tens of thousands of jobs. Automation is repricing routine digital work. Entry-level coding is becoming cheaper, faster, and more automated. In other words, Nepal is not entering a fresh market. It is trying to enter a giant, saturated, AI-restructured market dominated by countries that arrived years ago and built serious scale.</p><p>So when Nepal still speaks in generic outsourcing-era language, it is not just optimistic. It is badly out of date.</p><h2><strong>Afraid</strong></h2><p>And then there is the third part: <strong>afraid</strong>.</p><p>The clearest example is <strong>crypto</strong>. While jurisdictions like Singapore allowed digital-asset activity to develop inside a licensing perimeter, Nepal&#8217;s instinct was much simpler: declare it illegal and move on.</p><p>The posture was never, &#8220;How do we regulate exchanges, custody, payments, investor protection, and risk?&#8221; It was, &#8220;Shut it down.&#8221; Not just trading. Not just payments. Mining too. Blockchain got flattened into the same category of bureaucratic discomfort.</p><p>That difference matters.</p><p>When a state does not understand a technology and responds by banning the whole category, it is not being wise. It is outsourcing the learning curve to other countries. The entrepreneurs leave. The engineers leave. The experiments leave. The ecosystem leaves. Then, years later, the same bureaucracy starts talking about innovation and wonders why there is no domestic depth.</p><p>That is not caution. That is fear dressed up as policy.</p><h2><strong>The data-center fantasy</strong></h2><p>Now, to be fair, Nepal does have real advantages for digital infrastructure. Renewable power is real. Cooler ambient conditions in some places are real. The new directive is real. These things matter.</p><p>But this is exactly where the national conversation becomes unserious.</p><p>We reduce a brutal infrastructure business into a tourism brochure: <em>rivers, mountains, cold climate, opportunity</em>.</p><p>A real data-center business is not a postcard business. It is a power-quality business, a network-resilience business, an uptime business and a capital-allocation business.</p><p>Start with electricity. Nepal&#8217;s hydropower story sounds attractive until you remember the seasonal reality: building &#8220;next to hydropower&#8221; is not the same thing as having firm, bankable, year-round electricity for high-density compute. Rivers are not reliability guarantees.</p><p>Then there is connectivity. Serious digital infrastructure depends on route diversity, peering, redundancy, interconnection quality, and operational maturity &#8212; not just cheap land and patriotic optimism.</p><p>And then there is the economics. Even in countries that know what they are doing, data centers are hard businesses. A meaningful share of power still goes to overhead rather than IT load. AI hardware only raises the difficulty. The future people casually pitch in Nepal is not &#8220;some servers in a cool valley&#8221;. It is a dense electrical, cooling, uptime, and utilization problem with very expensive consequences if you get it wrong.</p><p>So yes, Nepal may have ingredients. But ingredients are not a business model.</p><h2><strong>The serious answer</strong></h2><p>So where does that leave the serious answer?</p><p>It leaves us with a pretty uncomfortable conclusion: Nepal&#8217;s problem is not that it lacks opportunities. It is that it keeps arriving at them in the wrong way.</p><p>Late to the market.</p><p>Broad in the response.</p><p>Afraid of the unfamiliar.</p><p><em><strong>That is why &#8220;become an IT hub&#8221; should probably be retired as a national slogan</strong></em>. It is too broad to guide decisions, too late to be impressive, and too detached from the actual shape of the market.</p><p>If Nepal wants to matter in the AI era, it needs to stop mistaking policy announcements for strategy.</p><p>That means five things.</p><p><strong>First, stop banning unfamiliar technologies by instinct.</strong></p><p>Regulate them properly. Prohibition is not sophistication.</p><p><strong>Second, stop talking about generic IT outsourcing as if it were still a frontier.</strong></p><p>It is not. Nepal needs narrower digital niches where it has an actual edge.</p><p><strong>Third, if Nepal wants data centers, build the boring foundations first.</strong></p><p>Firm power contracts. Import clarity for compute equipment. Route-diverse fiber. Credible compliance. Not slogans. Plumbing.</p><p><strong>Fourth, stop marketing hydropower and cold weather as if they automatically add up to competitiveness.</strong></p><p>They help. They do not replace uptime, customers, utilization, and execution.</p><p><strong>And fifth, measure success by capability, not ceremony.</strong></p><p>Not by whether a ministry launched another framework with the word &#8220;digital&#8221; in it, but by whether Nepal built real depth before the window closed.</p><p>That is the real point.</p><p>We may still build real digital industries. But we will not do so by showing up after everyone else has scaled the market, issuing a directive, and pretending the policy itself is the achievement.</p><p>The achievement is getting there early enough that the market still cares.</p><div><hr></div><blockquote><p><em>Author&#8217;s note: I used ChatGPT as a research and drafting tool while writing this essay. The perspective, conclusions, and final edits are mine.</em></p></blockquote>]]></content:encoded></item><item><title><![CDATA[Cutting the Noise: Why Deref Coercion Makes Rust Code More Expressive]]></title><description><![CDATA[How automatic pointer conversions in Rust simplify APIs and eliminate boilerplate]]></description><link>https://blog.abhi.dev/p/cutting-the-noise-why-deref-coercion</link><guid isPermaLink="false">https://blog.abhi.dev/p/cutting-the-noise-why-deref-coercion</guid><dc:creator><![CDATA[Abhinav Mishra]]></dc:creator><pubDate>Sun, 05 Jan 2025 09:44:55 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!khXU!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1445f783-fc4d-4944-a25e-5b68b5019f07_652x652.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2><strong>The Problem: Too Many Pointer Types!</strong></h2><p>Let&#8217;s suppose we have a Rust function that needs to accept a string. In our codebase, however, we store string data in several ways:</p><ol><li><p><strong>A regular </strong><code>String</code>:</p><ol><li><p>Stored on the heap and fully owned by our code.</p></li><li><p>Ideal for most scenarios where we need to modify or transfer ownership of the text.</p></li></ol></li><li><p><strong>A boxed string (</strong><code>Box&lt;String&gt;</code><strong>)</strong>:</p><ol><li><p>Places a <code>String</code> behind a pointer on the heap.</p></li><li><p>Helpful for <strong>recursive data structures</strong> or <strong>trait objects</strong> where the size of the type must be known at compile time.</p></li><li><p>Can also reduce the stack size for large strings.</p></li></ol></li><li><p><strong>A reference-counted string (</strong><code>Rc&lt;String&gt;</code><strong>)</strong>:</p><ol><li><p>Enables <strong>shared, immutable ownership</strong> of a string in single-threaded contexts.</p></li><li><p>Useful when multiple parts of our code need to read the same string without duplicating it.</p></li></ol></li><li><p><strong>A plain string slice (</strong>&amp;str<strong>)</strong>:</p><ol><li><p>A <strong>borrowed view</strong> into some string data.</p></li><li><p>Excellent for <strong>read-only</strong> operations, requiring minimal overhead and no ownership transfer.</p></li></ol></li></ol><p>Suddenly, now we face the question: </p><p><em><strong>Do I write four separate function overloads?</strong> </em></p><p>One straightforward&#8212;though overly verbose&#8212;solution is to write a separate function for each string type in our codebase:</p><pre><code>fn process_string_owned(s: String) { /* ... */ }
fn process_string_boxed(s: Box&lt;String&gt;) { /* ... */ }
fn process_string_rc(s: Rc&lt;String&gt;) { /* ... */ }
fn process_string_slice(s: &amp;str) { /* ... */ }</code></pre><p>While this covers all our string variants, it clutters the API, forces additional maintenance, and doesn&#8217;t scale well if more pointer or container types appear in the future.</p><p><strong>Enter Rust&#8217;s Deref coercion.</strong></p><div><hr></div><h2><strong>The Key Idea: Take a &amp;str, Let Rust Handle the Rest</strong></h2><p>In Rust, instead of writing four overloads, we can simply have our function accept a reference to a string slice&#8212;<code>&amp;str</code>&#8212;and let the compiler handle the conversions automatically. This automatic pointer conversion is called <strong>Deref coercion</strong>.</p><p><strong>Step 1: A Single Function Signature</strong></p><pre><code>fn process_string(s: &amp;str) {
    // process string
    println!("Processed: {}", s);
}</code></pre><p>Here, we&#8217;re <strong>not</strong> specifying <code>String</code>, <code>Box&lt;String&gt;</code>, or <code>Rc&lt;String&gt;</code>&#8212;just <code>&amp;str</code>. That choice gives our code the ability to accept multiple pointer or reference types that dereference into a <code>str</code>.</p><p><strong>Step 2: Passing Multiple Pointer Types</strong></p><pre><code>use std::rc::Rc;

fn main() {
    let regular_string = String::from("I am a String");
    let boxed_string = Box::new(String::from("I am a Box&lt;String&gt;"));
    let rc_string = Rc::new(String::from("I am an Rc&lt;String&gt;"));

    // All of these work without complaining or extra method calls:
    process_string(&amp;regular_string);
    process_string(&amp;boxed_string);
    process_string(&amp;rc_string);
    process_string("I am a string literal");
}</code></pre><p><strong>Why does this work?</strong> Because each pointer-like type (<code>String</code>, <code>Box&lt;String&gt;</code>, <code>Rc&lt;String&gt;</code>) implements the <a href="https://doc.rust-lang.org/std/ops/trait.Deref.html">Deref</a> trait, whose Target is str (directly or indirectly). Rust sees that <code>&amp;Box&lt;String&gt;</code> should behave like <code>&amp;String</code>, which in turn behaves like <code>&amp;str</code>. The compiler automatically <strong>coerces</strong> the pointer type step by step until it becomes <code>&amp;str</code>.</p><div><hr></div><h2><strong>Under the Hood: What Is Deref Coercion?</strong></h2><p><strong>Deref coercion</strong> is a feature in Rust that allows the compiler to insert <em>* (dereference) </em>operations on our behalf in certain contexts. Specifically:</p><ul><li><p>When we call a function or method with an argument of type <code>&amp;T</code>, and T implements <code>Deref&lt;Target = U&gt;</code>, Rust can coerce <code>&amp;T</code> into <code>&amp;U</code>.</p></li><li><p>This means less boilerplate. Instead of typing <code>process_string(&amp;(*rc_string))</code>, we can just write <code>process_string(&amp;rc_string)</code>.</p></li></ul><p></p><div><hr></div><h2><strong>Benefits of Deref Coercion</strong></h2><ol><li><p><strong>Eliminates Boilerplate</strong></p><p>No need to manually write multiple function overloads or pepper code with manual dereference operators. We keep our function signature minimal by focusing on the <em>underlying</em> type (often <code>&amp;str</code>).</p></li><li><p><strong>Promotes Flexible APIs</strong></p><p>Want to switch from a <code>String</code> to a <code>Box&lt;String&gt;</code> in some part of our code for performance or memory reasons? We can do so without refactoring every function call site. As long as the underlying type derefs to <code>str</code>, our original function keeps working.</p></li><li><p><strong>No Performance Overhead</strong></p><p>Deref coercion is a <strong>compile-time convenience</strong>. The compiler rewrites our calls to include the necessary dereferences, which translates into the same low-level instructions we would have written by hand. There&#8217;s no runtime penalty.</p></li><li><p><strong>Method Resolution Goodness</strong></p><p>It&#8217;s not just about function calls. When we call methods on a type that implements Deref, Rust will also attempt to dereference it to see if the target type has the method we are calling. This makes using smart pointers feel as smooth as working with the underlying data.</p><p></p></li></ol><div><hr></div><h2><strong>A Quick Peek at Deref Implementation</strong></h2><p>We typically don&#8217;t have to implement Deref for standard pointer types (<code>Box&lt;T&gt;</code>, <code>Rc&lt;T&gt;</code>, <code>Arc&lt;T&gt;</code>), as Rust&#8217;s standard library does that for us. But if we create a custom pointer-like type, here&#8217;s a simplified example:</p><pre><code>use std::ops::Deref;

struct Greeting(String);

impl Greeting {
    fn new(message: &amp;str) -&gt; Self {
        Greeting(message.to_string())
    }
}

// Implement Deref so that &amp;Greeting behaves like &amp;str
impl Deref for Greeting {
    type Target = str;

    fn deref(&amp;self) -&gt; &amp;Self::Target {
        &amp;self.0
    }
}

fn print_message(message: &amp;str) {
    println!("Here&#8217;s the message: {message}");
}

fn main() {
    let greeting = Greeting::new("Hello, Rust!");

    // We can call any method that 'str' has directly on 'Greeting'
    println!("Greeting length: {}", greeting.len());
    println!("Greeting in uppercase: {}", greeting.to_uppercase());

    // Even treat it as a &amp;str in function calls:
    print_message(&amp;greeting);
}</code></pre><div><hr></div><h2><strong>Conclusion: One Signature to Rule Them All</strong></h2><p>Thanks to <strong>deref coercion</strong>, we can <strong>drastically reduce</strong> the complexity of our Rust APIs. Instead of a tangle of function overloads for <code>Box&lt;T&gt;</code>, <code>Rc&lt;T&gt;</code>, <code>Arc&lt;T&gt;</code>, or plain references, we focus on the <em>core type</em> our function cares about. Rust handles the pointer conversions automatically, freeing us to write more expressive, flexible, and maintainable code.</p><p>In other words:</p><ul><li><p><strong>Stop writing multiple overloads</strong> for different pointer types.</p></li><li><p><strong>Embrace deref coercion</strong> by choosing the simplest reference parameter we actually need (often <code>&amp;T</code> or <code>&amp;str</code>).</p></li><li><p><strong>Enjoy the best of all worlds</strong>: safety, performance, and clean code.</p></li></ul><p><strong>Happy coding, and remember</strong>&#8212;less boilerplate means more time to focus on solving real problems rather than wrangling pointer syntax!</p>]]></content:encoded></item><item><title><![CDATA[Bits and Pieces: Essential Bit Manipulation Techniques]]></title><description><![CDATA[Unraveling the Magic of Binary Tricks for Smarter Code]]></description><link>https://blog.abhi.dev/p/bits-and-pieces-essential-bit-manipulation</link><guid isPermaLink="false">https://blog.abhi.dev/p/bits-and-pieces-essential-bit-manipulation</guid><dc:creator><![CDATA[Abhinav Mishra]]></dc:creator><pubDate>Fri, 03 Jan 2025 08:07:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!khXU!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1445f783-fc4d-4944-a25e-5b68b5019f07_652x652.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Bit manipulation always felt like a mysterious art to me&#8212;a combination of math, binary, and low-level magic. When I started diving deeper into <em><strong>Data Structures and Algorithms (DSA)</strong></em>, I realized how powerful these techniques could be for solving complex problems efficiently. Here&#8217;s a collection of essential techniques and insights I discovered along the way. </p><h2>Understanding Numbers at the Bit Level</h2><h3>Even or Odd?</h3><ul><li><p><strong>Even Number:</strong> Its least significant bit (LSB) is 0. </p><p><code>( n &amp; 1 = 0 ) </code></p><p></p></li><li><p><strong>Odd Number:</strong> Its least significant bit (LSB) is 1. </p><p><code>( n &amp; 1 = 1 )</code></p></li></ul><div><hr></div><h3><strong>What Happens When Subtracting 1?</strong></h3><p>When you subtract 1 from a number:</p><ul><li><p>All bits after the rightmost set bit, including the set bit itself, are flipped.</p></li></ul><p><strong>Example 1: </strong>n = 12 <strong>(Binary: 1100)</strong></p><ul><li><p>Subtract 1: n - 1 = 11 (Binary: 1011)</p></li><li><p><strong>Explanation</strong>: The rightmost set bit (1) and all bits to the right of it are flipped: 1100 becomes 1011.</p></li></ul><p><strong>Example 2: </strong>n = 7 <strong>(Binary: 0111)</strong></p><ul><li><p>Subtract 1: n - 1 = 6 (Binary: 0110)</p></li><li><p><strong>Explanation</strong>: The rightmost set bit (1) and all bits to the right of it are flipped: 0111 becomes 0110.</p><p></p></li></ul><p><strong>Implications:</strong></p><p><code>[ n &amp; (n - 1) ]</code></p><p>This operation clears the rightmost set bit.</p><p><strong>Example:</strong></p><p>For n = 12 (binary: 1100)</p><ul><li><p><code>n - 1 = 11</code> (binary: 1011)</p></li><li><p><code>( n &amp; (n - 1) ) = 1100 &amp; 1011 = 1000</code></p></li></ul><p></p><div><hr></div><h3>Power of 2</h3><p>A number is a power of 2 <em>if and only if it has exactly one bit set in its binary representation</em> (e.g., 1, 2, 4, 8, 16, &#8230;)</p><p><em><strong>Check if a number is a power of 2:</strong></em></p><p><code>[ n &amp; (n - 1) == 0 ]</code></p><p><em>Why does this work?</em> &#8594; Subtracting 1 flips all the bits after the rightmost set bit, including the bit itself. Performing <code>( n &amp; (n - 1) )</code> clears this set bit, resulting in zero if there was only one bit set initially.</p><p>It also implies that when we write a number <code>n</code> in binary, its least significant bit (rightmost 1) represents the smallest power of 2 that divides it. </p><p><strong>Example:</strong></p><ul><li><p>For n = 12 (which is <code>1100</code> in binary), the least significant 1 is at the second position, which corresponds to <code>2^2</code> .</p></li><li><p>For n = 6 (which is <code>0110</code> in binary), the least significant 1 is at the first position, corresponding to <code>2^1</code> .</p></li><li><p>For n = 7 (which is <code>0111</code> in binary), the least significant 1 is at the zero position, corresponding to <code>2^0</code> .</p></li></ul><p><strong>Note:</strong> The operation <code>( n &amp; (n - 1) )</code> exploits the same bit-flipping nature of subtraction as presented above.</p><p></p><div><hr></div><h3><strong>Multiplication and Division by Powers of 2</strong></h3><p>Efficiently multiply or divide numbers using bit shifting:</p><ul><li><p><strong>Multiply</strong>: <code>n &#171; k</code> (shifts bits to the left by k ) &#8212; equivalent to <code>n * 2^k</code> .</p></li><li><p><strong>Divide</strong>: <code>n &#187; k</code> (shifts bits to the right by k ) &#8212; equivalent to <code>n / 2^k</code> .</p></li></ul><p></p><div><hr></div><h3>Negative Numbers &amp; Two&#8217;s Complement</h3><p>In computer systems, negative numbers are represented using <strong>two&#8217;s complement</strong> notation. This approach simplifies arithmetic operations and ensures that addition, subtraction, and bitwise operations work seamlessly across positive and negative integers.</p><h4><strong>How Two&#8217;s Complement Works:</strong></h4><p>To find the two&#8217;s complement of a number:</p><ul><li><p>Start with the binary representation of the number&#8217;s absolute value.</p></li><li><p>Invert all the bits (flip 0s to 1s and 1s to 0s).</p></li><li><p>Add 1 to the result.</p></li></ul><h4>Mental Model of Two&#8217;s Complement</h4><ul><li><p>Start from the least significant bit (LSB).</p></li><li><p>Keep the bits unchanged until the first 1 is encountered.</p></li><li><p>After encountering the first 1, flip all remaining bits.</p></li></ul><p><strong>The Most Significant Bit (MSB) Represents the Sign:</strong></p><ul><li><p><strong>0</strong>: The number is <strong>non-negative</strong> (positive or zero).</p></li><li><p><strong>1</strong>: The number is <strong>negative</strong>.</p></li></ul><h3>Implications</h3><p>This operation isolates the rightmost set bit of:</p><p><code>[ n &amp; -n ]</code></p><p><strong>Example 1:</strong></p><p>For n = 6 (binary: 0110),</p><ul><li><p>(two&#8217;s complement of 0110) &#8594; <code>1010</code></p></li><li><p><code>n &amp; -n = 0110 &amp; 1010 = 0010</code></p></li></ul><p><strong>Example 2:</strong></p><p>For n = 7 (binary: 0111),</p><ul><li><p>(two&#8217;s complement of 0111) &#8594; 1001</p></li><li><p><code>n &amp; -n = 0111 &amp; 1001 = 0001</code></p></li></ul><p>This can be visualized as <em>the smallest power of 2 that divides n</em>.</p><div><hr></div><h3>XOR: The &#8220;Difference Detector&#8221;</h3><p>The XOR operator (^) compares each bit of two numbers:</p><ul><li><p>If the bits are <strong>different</strong>, the result is 1.</p></li><li><p>If the bits are <strong>the same</strong>, the result is 0.</p></li></ul><h4><strong>Implications of XOR</strong></h4><ul><li><p><code>a ^ a = 0 </code>: XORing a number with itself results in 0.</p></li><li><p><code>a ^ 0 = a</code>  : XORing a number with 0 leaves it unchanged.</p></li><li><p><code>a ^ b &lt; 0</code>  : This is true if a and b have <strong>opposite signs</strong>.</p><p></p></li></ul><h4><strong>Swapping Two Numbers Without a Temporary Variable</strong></h4><p>Using XOR, you can swap two numbers in three steps:</p><ul><li><p><code>a = a ^ b</code></p></li><li><p><code>b = a ^ b</code> is equivalent to </p><p><code>b = (a ^ b) ^ b = a ^ (b ^ b) = a ^ 0 = a</code></p></li><li><p><code>a = a ^ b</code> is equivalent to </p><p><code>a = (a ^ b) ^ a = (a ^ a) ^ b = 0 ^ b = b</code></p></li></ul><p><strong>Example:</strong></p><p>For a = 5 (0101) and b = 3 (0011):</p><p><code>1. a = 0101 ^ 0011 = 0110</code></p><p><code>2. b = 0110 ^ 0011 = 0101</code></p><p><code>3. a = 0110 ^ 0101 = 0011</code></p><p>Now a = 3 and b = 5 . Magic, no temporary variable required!</p><p></p><div><hr></div><h3><strong>Conclusion</strong></h3><p>Bit manipulation is a cornerstone of efficient algorithms and problem-solving. Whether it&#8217;s for toggling bits, isolating flags, or performing quick calculations, these techniques can transform how you approach challenges in <strong>Data Structures and Algorithms (DSA)</strong> and low-level programming.</p><p>What&#8217;s your favorite bit manipulation trick? Share it in the comments!</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://blog.abhi.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item></channel></rss>