Handy Bit Manipulation Tricks — DeepSeek Tips & Insights
    Neura MarketNeura Market/DeepSeek
    ChatGPTChatGPTClaudeClaudeGeminiGeminiCursorCursorGrokGrokPerplexityPerplexityDeepSeekDeepSeek
    CoPilotCoPilotStable DiffusionStable DiffusionMidjourneyMidjourney
    View All Directories
    OverviewRulesPromptsMCPsAgentsGamesBlogVideosGuidesCoursesCommunityTrending
    DeepSeekBlogHandy Bit Manipulation Tricks
    Back to Blog
    Handy Bit Manipulation Tricks
    algorithms

    Handy Bit Manipulation Tricks

    Manuj Sankrit February 13, 2026
    0 views

    I was digging through React's source code and stumbled upon some bitmask logic for handling component...

    I was digging through React's source code and stumbled upon some bitmask logic for handling component states. At first glance, it looked cryptic—just a bunch of &, |, and << operators. But once I understood what was happening, it clicked. These operations are ridiculously fast and efficient.

    Here are some tricks I find myself reaching for when performance actually matters.

    Basic Bit Operations

    Setting a Bit

    To set the bit at position pos, use the bitwise OR operator. Think of (1 << pos) as a laser pointer—you're pointing it exactly at one spot, and | is the "turn on" switch:

    function setBit(n: number, pos: number): number {
      return n | (1 << pos);
    }
    

    Example: Setting bit at position 2 in 0b1010 gives us 0b1110

       0b1010
     | 0b0100  (laser pointer at position 2)
    -----------
       0b1110
    

    Clearing a Bit

    To clear a bit at position pos, we AND with the inverted mask:

    function clearBit(n: number, pos: number): number {
      return n & ~(1 << pos);
    }
    

    Example: Clearing bit at position 1 in 0b1110 gives 0b1100

       0b1110
     & 0b1101
    -----------
       0b1100
    

    Toggling a Bit

    Toggling is straightforward with XOR:

    function toggleBit(n: number, pos: number): number {
      return n ^ (1 << pos);
    }
    

    Example: Toggling bit at position 2 in 0b1010 → 0b1110

       0b1010
     ^ 0b0100
    -----------
       0b1110
    

    Checking a Bit

    To check if a bit is set:

    function checkBit(n: number, pos: number): boolean {
      return ((n >> pos) & 1) !== 0;
    }
    

    Example: Checking bit at position 1 in 0b0110

       0b0110
     >>     1
    -----------
       0b0011
     & 0b0001
    -----------
       0b0001 (true)
    

    ⚠️ JS/TS Gotcha: Bitwise operators in JavaScript convert numbers into 32-bit signed integers. If you're working with large numbers (above 2³¹-1), like database IDs or timestamps, this magic will turn into a nightmare. For those cases, stick with BigInt or regular arithmetic.

    Some Clever Tricks

    Check if a Number is a Power of 2

    This one's pretty neat:

    function isPowerOfTwo(n: number): boolean {
      return n > 0 && (n & (n - 1)) === 0;
    }
    

    The trick here is that powers of 2 have exactly one bit set(means one bit is 1). When you subtract 1, all the bits after that flip, so the AND operation gives you 0.

    n = 8 (0b1000)
    n - 1 = 7 (0b0111)
    n & (n - 1) = 0b0000
    

    Count Set Bits (Brian Kernighan's Algorithm)

    function countSetBits(n: number): number {
      let count = 0;
      while (n) {
        n = n & (n - 1);
        count++;
      }
      return count;
    }
    

    This keeps clearing the rightmost set bit until nothing's left. Each iteration bumps the count.

    0b1011 → 0b1010 → 0b1000 → 0b0000
    count: 1      2        3
    

    Find the Rightmost Set Bit

    function rightmostSetBit(n: number): number {
      return n & -n;
    }
    

    This works because -n (two's complement) flips all the bits and adds 1, which isolates just the rightmost set bit.

    n = 0b1010
    -n = 0b0110
    n & -n = 0b0010
    

    Swap Two Numbers Without a Temp Variable

    function swap(a: number, b: number): [number, number] {
      a = a ^ b;
      b = a ^ b;
      a = a ^ b;
      return [a, b];
    }
    

    XOR has this cool property where a ^ a = 0 and a ^ 0 = a, so you can swap without needing extra space.

    Check if a Number is Even

    function isEven(n: number): boolean {
      return (n & 1) === 0;
    }
    

    Way faster than the modulo operator. The least significant bit is 0 for even numbers.

    The Trade-off: Efficiency vs Readability

    Now, here's the thing. Every concept has its pros and cons, and bit manipulation is no exception. Sure, these operations are super efficient and lean, but they definitely compromise the KISS principle.

    If you go overboard with bit tricks in your codebase, you're gonna have a problem. Junior developers—well, the ones that haven't been replaced by AI yet 😁—might struggle to understand what's going on. Heck, even you might come back to your own code six months later and wonder what you were thinking.

    So use bit manipulation wisely:

    • Performance-critical sections: Where every cycle counts
    • Well-documented code: Add comments explaining the bit magic
    • Specific algorithms: Like the ones mentioned above
    • Standard patterns: Stick to common tricks that developers recognize

    But for everyday logic? Just write clear, readable code. n % 2 === 0 is perfectly fine for checking even numbers in most cases. Don't sacrifice code clarity just to show off your bit manipulation skills.

    Where This Actually Matters

    You'll see bit manipulation used in:

    • Permissions systems: Like Unix file permissions (chmod 755)
    • Feature flags: When you need to pack multiple boolean flags into a single integer
    • Subset generation: Iterating through all possible combinations
    • Performance-critical code: These operations literally execute in one CPU cycle

    Quick Reference

    OperationCodeExample
    Set bit<code>n | (1 << pos)</code><code>0b1010 | (1 << 2)</code> → <code>0b1110</code>
    Clear bitn & ~(1 << pos)0b1110 & ~(1 << 1) → 0b1100
    Toggle bitn ^ (1 << pos)0b1010 ^ (1 << 2) → 0b1110
    Check bit(n >> pos) & 1(0b1010 >> 1) & 1 → 1 (set)
    Power of 2n & (n - 1) === 08 & 7 === 0 → true
    Even/Oddn & 1 === 04 & 1 === 0 → true (even)
    Rightmost setn & -n0b1010 & -0b1010 → 0b0010

    These patterns might look weird at first, but after using them a few times, they start to make sense. Just remember: with great power comes great responsibility. Use them where they make sense, not everywhere.

    Pranipat 🙏!

    Tags

    algorithmsprogrammingjavascripttypescript

    Comments

    More Blog

    View all
    Five Gemma-4 models, one accelerator: what porting E2B 31B to AWS Inferentia2 taught megemma

    Five Gemma-4 models, one accelerator: what porting E2B 31B to AWS Inferentia2 taught me

    I ported the whole Gemma-4 family — E2B, E4B, 12B, 31B, and the 26B-A4B MoE — to run on...

    X
    xbill
    Hey DEV, I'm Tobore. Let's actually connect.community

    Hey DEV, I'm Tobore. Let's actually connect.

    Hey DEV, I'm Tobore. Let's actually connect. I've been on here for a while now, mostly writing and...

    L
    Laurina Ayarah
    I burned through thousands of AI tokens. Then a friend did it for freeai

    I burned through thousands of AI tokens. Then a friend did it for free

    (yep, kinda clickbait, just for the funsies 😊) At the beginning of the year, I relaunched my...

    P
    Paulo Henrique
    Claude might be saturating your machineai

    Claude might be saturating your machine

    My laptop was sitting idle with the fan at full tilt. Nothing was running that I knew of. The culprit...

    S
    Sidhant Panda
    Automated GitHub Code Reviews Using Google Geminigithubactions

    Automated GitHub Code Reviews Using Google Gemini

    I Built a Thing! TL;DR — Google Gemini-based Pull Request reviews and Issue Triaging for...

    D
    Darren "Dazbo" Lester
    What is an "agentic harness," actually?ai

    What is an "agentic harness," actually?

    I've been hearing the word "harness" thrown around a lot lately. I assumed it just meant "the IDE" or...

    T
    Tilde A. Thurium

    Stay up to date

    Get the latest DeepSeek prompts, rules, and resources delivered to your inbox weekly.

    Neura Market LogoNeura Market

    Discover the best AI prompts, plugins, and resources for DeepSeek and more.

    Content Types

    • Rules
    • Prompts
    • MCPs
    • Agents
    • Guides

    Platforms

    • ChatGPT Directory
    • Claude Directory
    • Gemini Directory
    • Cursor Directory
    • Grok Directory
    • Perplexity Directory
    • DeepSeek Directory
    • CoPilot Directory
    • Stable Diffusion Directory
    • Midjourney Directory
    • All Directories

    Resources

    • Blog
    • Documentation
    • Help Center
    • Marketplace

    Legal

    • Privacy Policy
    • Terms of Service

    © 2026 Neura Market. All rights reserved.

    |

    Not affiliated with any AI platform vendors.

    Neura Market

    Custom AI Systems & Services

    Our team of experienced AI builders will help build custom AI systems, workflows, and solutions for your business.

    Request custom work

    Ready-made automations for this

    Workflows from the Neura Market marketplace related to this DeepSeek resource

    • Handling Appointment Leads and Follow-Up with Twilio, Cal.com, and AIn8n · $24.99 · Related topic
    • Handling Job Application Submissions with AI and N8n Formsn8n · $14.99 · Related topic
    • Redis Locking for Concurrent Task Handlingn8n · $14.99 · Related topic
    • Automate PDF Manipulation with Adobe PDF Services APIn8n · $14.99 · Related topic
    Browse all workflows