693. Binary Number with Alternating Bits
Difficulty: Easy
Topics: Mid Level, Bit Manipulation
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example 1:
- Input: n = 5
- Output: true
- Explanation: The binary representation of 5 is: 101
Example 2:
- Input: n = 7
- Output: false
- Explanation: The binary representation of 7 is: 111.
Example 3:
- Input: n = 11
- Output: false
- Explanation: The binary representation of 11 is: 1011.
Constraints:
1 <= n <= 2³¹ - 1
Solution:
We need to solve "Binary Number with Alternating Bits". Given positive integer n, check if its binary representation has alternating bits (adjacent bits differ). Constraints up to 2³¹-1.
Approach:
- The solution leverages bitwise XOR to detect differences between adjacent bits.
- Compute
$x = $n ^ ($n >> 1).-
$n >> 1shifts the bits one position to the right, aligning each original bit with its right neighbor. - XOR yields
1where the two bits differ and0where they are the same.
-
- For a number with alternating bits, every adjacent pair differs, so the XOR result will consist entirely of
1bits from the most significant bit down to the least significant bit (e.g.,111...111). - To verify that
$xis all ones, we use the property: a numberxof the form2ᵏ - 1(all ones) satisfiesx & (x + 1) == 0, because adding1turns it into a power of two (100...0), which has no overlapping set bits withx. - Therefore, the function returns
($x & ($x + 1)) == 0.
Let's implement this solution in PHP: 693. Binary Number with Alternating Bits
<?php
/**
* Check if the binary representation of a positive integer has alternating bits.
*
* @param Integer $n
* @return Boolean
*/
function hasAlternatingBits(int $n): bool
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo hasAlternatingBits(5) . "\n"; // Output: true
echo hasAlternatingBits(7) . "\n"; // Output: false
echo hasAlternatingBits(11) . "\n"; // Output: false
?>
Explanation:
Step 1 – Create difference mask
$x = $n ^ ($n >> 1)
Example:n = 5(binary101),n >> 1 = 2(010),$x = 101⁰¹⁰ = 111(all bits set).
Example:n = 7(111),n >> 1 = 3(011),$x = 111⁰¹¹ = 100(not all bits set).-
Step 2 – Check for all‑ones pattern
The condition($x & ($x + 1)) == 0istrueonly when$xis a sequence of consecutive1bits (e.g.,111,1,11).- If
$xis all ones, adding1yields a power of two, and the bitwise AND is zero. - If
$xis not all ones, at least one zero exists; adding1will cause carries that create overlapping1bits, making the AND non‑zero.
- If
-
Why it works
- For alternating bits, every adjacent pair differs →
$xhas1in every bit position that matters →$xis of the form111...111. - The property
x & (x + 1) == 0exactly identifies such numbers.
- For alternating bits, every adjacent pair differs →
-
Edge cases
-
n = 1(binary1):$x = 1⁰ = 1(all ones) →1 & (1+1) = 1 & 2 = 0→true. -
n = 2(10):$x = 10⁰¹ = 11(all ones) →true. -
n = 3(11):$x = 11⁰¹ = 10(not all ones) →false.
-
Complexity
- Time Complexity: O(1) – constant number of bitwise operations.
- Space Complexity: O(1) – only a few integer variables.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)