Binary Number with Alternating Bits

🏠 ⬅️ ➑️

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 <= 231 - 1

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer :: n
    logical :: result

    ! Example 1
    n = 5
    result = alternating_bits(n)
    write (*, '(A, L1)') 'Example 1: ', result

    ! Example 2
    n = 7
    result = alternating_bits(n)
    write (*, '(A, L1)') 'Example 2: ', result

    ! Example 3
    n = 11
    result = alternating_bits(n)
    write (*, '(A, L1)') 'Example 3: ', result

contains

    function alternating_bits(n) result(result)
        implicit none
        integer, intent(in) :: n
        logical :: result

        integer :: i, j
        logical :: bit_diff

        bit_diff = .false.
        do i = 1, 8
            j = mod(i-1, 8) + 1
            if (bit(n, i) /= bit(n, j)) then
                bit_diff = .true.
                exit
            end if
        end do

        result = bit_diff
    end function alternating_bits

end program main
❌ Compiled
❌ Executed
❌ Correct
```
program alternating_bits
    implicit none
    integer :: n, i, j
    logical :: has_alternating_bits

    ! Read input
    read *, n

    ! Initialize variables
    i = 0
    j = 1
    has_alternating_bits = .true.

    ! Check if the binary representation of n has alternating bits
    do while (i < 32)
        if (mod(n, 2) == 0) then
            if (j == 1) then
                has_alternating_bits = .false.
                exit
            end if
        else
            if (j == 0) then
                has_alternating_bits = .false.
                exit
            end if
        end if
        i = i + 1
        j = mod(n, 2)
        n = n / 2
    end do

    ! Output result
    if (has_alternating_bits) then
        print *, "Yes"
    else
        print *, "No"
    end if
end program alternating_bits
```
🌐 Data from online sources
def has_alternating_bits(n):
    prev_bit = n % 2
    n //= 2
    while n > 0:
        curr_bit = n % 2
        if curr_bit == prev_bit:
            return False
        prev_bit = curr_bit
        n //= 2
    return True
The algorithm initializes `prevBit` with the least significant bit of `n`. Then, it divides `n` by 2 to eliminate the least significant bit. While `n` is greater than 0, the algorithm retrieves the least significant bit of `n` and compares it to `prevBit`. If they are equal, it means the adjacent bits are not alternating, so the function returns false. Otherwise, it assigns the least significant bit to `prevBit` and divides `n` by 2 again to move on to the next bit. When `n` becomes 0, it means all adjacent bits in the given integer are alternating, and the function returns true.
🌐 Data from online sources
bool hasAlternatingBits(int n) {
    int prevBit = n % 2;
    n /= 2;
    while (n > 0) {
        int currBit = n % 2;
        if (currBit == prevBit) return false;
        prevBit = currBit;
        n /= 2;
    }
    return true;
}
The algorithm initializes `prevBit` with the least significant bit of `n`. Then, it divides `n` by 2 to eliminate the least significant bit. While `n` is greater than 0, the algorithm retrieves the least significant bit of `n` and compares it to `prevBit`. If they are equal, it means the adjacent bits are not alternating, so the function returns false. Otherwise, it assigns the least significant bit to `prevBit` and divides `n` by 2 again to move on to the next bit. When `n` becomes 0, it means all adjacent bits in the given integer are alternating, and the function returns true.