30 Days. 30 Bit Manipulation Tips & Tricks.
For the next 30 days, I’ll share powerful bit hacks, clever tricks, uncommon patterns, and practical optimizations used in CP and real-world problem solving.
No boring theory.
Only useful, practical, and important concepts.
30 Days. 30 Bit Manipulation Tips & Tricks.
For the next 30 days, I’ll share powerful bit hacks, clever tricks, uncommon patterns, and practical optimizations used in CP and real-world problem solving.
No boring theory.
Only useful, practical, and important concepts.
30 Days. 30 Bit Manipulation Tips & Tricks.
For the next 30 days, I’ll share powerful bit hacks, clever tricks, uncommon patterns, and practical optimizations used in CP and real-world problem solving.
No boring theory.
Only useful, practical, and important concepts.
XOR Series Part-7: Linear Basis (XOR Basis / XOR Space)
> One of the most powerful advanced bit manipulation techniques in DSA.
> Instead of exploring all 2^n subsets, XOR Basis compresses the entire subset XOR space into at most 32 (or 64) independent vectors.
> Core Idea: Treat numbers as binary vectors and perform Gaussian Elimination using XOR.
> Every basis vector owns a unique highest set bit (pivot bit).
Insertion Algorithm:
> When inserting a number:
- If it introduces a new pivot bit → add it to the basis.
- If it can be completely reduced to 0 → it is dependent and already representable.
> Only independent vectors contribute new XOR values.
> Insertion order may change the internal basis representation, but never the XOR values that can be generated.
> Basis Size = Number of independent vectors (rank of the XOR space).
> Distinct XOR values that can be generated: 2^(basis size)
> Maximum basis size: 32 for int, 64 for long
> XOR space is closed: If A and B are representable, then A ^ B is also representable.
> Empty subset always contributes XOR = 0.
> Common Applications:
- Maximum Subset XOR
- Count Distinct XOR Values
- K-th Largest XOR
- Graph Cycle XOR
- Path XOR Optimization
> Rule of Thumb:
- Trie → Pair XOR problems
- XOR Basis → Subset XOR problems
Think of XOR Basis as Binary Linear Algebra:
> A compact representation of all possible subset XOR combinations using only a handful of independent vectors.
XOR Series Part-7: Linear Basis (XOR Basis / XOR Space)
> One of the most powerful advanced bit manipulation techniques in DSA.
> Instead of exploring all 2^n subsets, XOR Basis compresses the entire subset XOR space into at most 32 (or 64) independent vectors.
> Core Idea: Treat numbers as binary vectors and perform Gaussian Elimination using XOR.
> Every basis vector owns a unique highest set bit (pivot bit).
Insertion Algorithm:
> When inserting a number:
- If it introduces a new pivot bit → add it to the basis.
- If it can be completely reduced to 0 → it is dependent and already representable.
> Only independent vectors contribute new XOR values.
> Insertion order may change the internal basis representation, but never the XOR values that can be generated.
> Basis Size = Number of independent vectors (rank of the XOR space).
> Distinct XOR values that can be generated: 2^(basis size)
> Maximum basis size: 32 for int, 64 for long
> XOR space is closed: If A and B are representable, then A ^ B is also representable.
> Empty subset always contributes XOR = 0.
> Common Applications:
- Maximum Subset XOR
- Count Distinct XOR Values
- K-th Largest XOR
- Graph Cycle XOR
- Path XOR Optimization
> Rule of Thumb:
- Trie → Pair XOR problems
- XOR Basis → Subset XOR problems
Think of XOR Basis as Binary Linear Algebra:
> A compact representation of all possible subset XOR combinations using only a handful of independent vectors.
30 Days. 30 Bit Manipulation Tips & Tricks.
For the next 30 days, I’ll share powerful bit hacks, clever tricks, uncommon patterns, and practical optimizations used in CP and real-world problem solving.
No boring theory.
Only useful, practical, and important concepts.
XOR Series Part-6: XOR Pattern for N integers
> XOR Pattern in 1 ^ 2 ^ 3 ^ … ^ n
> The XOR of all integers from 1 to n follows a 4-number repeating pattern:
- n % 4 = 0, then XOR (1 -> n) = n
- n % 4 = 1, then XOR (1 -> n) = 1
- n % 4 = 2, then XOR (1 -> n) = n + 1
- n % 4 = 3, then XOR (1 -> n) = 0
> Why does it repeat every 4?
- Look at table below
- Every 4 numbers, the last two bits repeat (01, 10, 11, 00).
- XOR affects bits independently.
- When you XOR sequentially, these repeated patterns in the least significant bits cause the overall XOR value to reset every 4 steps.
> Used in: Prefix XOR problems, Range XOR queries
30 Days. 30 Bit Manipulation Tips & Tricks.
For the next 30 days, I’ll share powerful bit hacks, clever tricks, uncommon patterns, and practical optimizations used in CP and real-world problem solving.
No boring theory.
Only useful, practical, and important concepts.
XOR Series Part-6: XOR Pattern for N integers
> XOR Pattern in 1 ^ 2 ^ 3 ^ … ^ n
> The XOR of all integers from 1 to n follows a 4-number repeating pattern:
- n % 4 = 0, then XOR (1 -> n) = n
- n % 4 = 1, then XOR (1 -> n) = 1
- n % 4 = 2, then XOR (1 -> n) = n + 1
- n % 4 = 3, then XOR (1 -> n) = 0
> Why does it repeat every 4?
- Look at table below
- Every 4 numbers, the last two bits repeat (01, 10, 11, 00).
- XOR affects bits independently.
- When you XOR sequentially, these repeated patterns in the least significant bits cause the overall XOR value to reset every 4 steps.
> Used in: Prefix XOR problems, Range XOR queries
Using rain as an encryption algorithm.
The hard part isn't encryption, it's decryption.
You'd need the exact pattern of raindrops, their sizes, velocities, wind conditions, cloud movement, and atmospheric state at that moment.
If all of that could be measured and reproduced, rain might become a natural cryptographic key.
Maybe rain isn't random. Maybe it's just too complex for us to predict.
Looking to grow my tech circle on X
Into software engineering, Java, AI, startups, backend systems, or just building cool things? Let’s connect.
Drop what you’re building or learning 👇
@kernelKain