Dsa
Bit Manipulation
Medium
Unique Bit Patterns
You are given an integer n, and your task is to find the number of unique bit patterns that can be formed by flipping any number of bits in the binary representation of n. A bit pattern is unique if it differs from another pattern in at least one bit position.
Input:
A single integer n (0 <= n <= 10^6).
Output:
An integer representing the count of unique bit patterns formed by flipping any of the bits in n.
Example:
Input:
5
Output:
8
Explanation:
The binary representation of 5 is '101'. The unique bit patterns include:
'000' (0), '001' (1), '010' (2), '011' (3), '100' (4), '101' (5), '110' (6), and '111' (7).
Key concepts
bit manipulationbinary representationcounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now