Solving LeetCode 476: Number Complement with Bit Manipulation

💻 Day 387 of #500DaysOfCode 🧩 LeetCode 476: Number Complement Difficulty: Easy Today’s problem was about flipping all bits of a number’s binary representation to find its complement. For example, 5 → 101 (binary) Complement → 010 = 2 The key idea is to create a bitmask of all 1s having the same length as the binary form of the number and then XOR it with the number — this efficiently flips all bits. 🧠 Approach Summary: 1️⃣ Build a mask with all bits = 1 (same length as num). 2️⃣ Use XOR (^) between num and mask to flip the bits. ✅ Example: num = 5 → 2 num = 1 → 0 Every day, a new concept — one bit at a time ⚙️ #CodingChallenge #LeetCode #Java #ProblemSolving #100DaysOfCode #500DaysOfCode #BitManipulation

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories