Program to Check if a Number has Bits in Alternate Pattern

Python Program to Check if a Number has Bits in Alternate Pattern

In the previous article, we have discussed Python Program to Find XOR of Two Numbers Without Using XOR operator

Given a number, the task is to check if the given Number has the bits in an alternate pattern

For example, the number 42 has an alternate pattern, which is 101010 (after 1 we are getting 0 and next 1 so on).
If it has an alternate bit pattern, print “Yes,” else “No.”

Examples:

Example1:

Input:

Given Number= 42

Output:

Yes, the given number{ 42 } has an alternate bit pattern

Example2:

Input:

Given Number= 14

Output:

No, the given number{ 14 } doesn't have an alternate bit pattern

Program to Check if a Number has Bits in Alternate Pattern in Python

Below are the ways to check if a given number has bits in an alternate pattern in python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Create a function to say chek_alterntebit which takes the given number as an argument and returns true if it has an alternate bit pattern else returns False.
  • Apply given number modulus 2 to get the last bit and store it in a variable.
  • Divide the given number by 2 and store it in the same variable gvn_numbr.
  • Loop until the given number is greater than 0 using a while loop.
  • Inside the loop, calculate the current bit by applying the given number modulus 2 and store it in another variable.
  • Check if the current bit is equal to the last bit using the if conditional statement.
  • If the statement is true, then return false.
  • Assign the current bit to the last bit.
  • Divide the given number by 2 and store it in the same variable gvn_numbr.
  • Return True. (out of while loop)
  • Pass the gvn_numbr as an argument to the chek_alterntebit() function and check whether it returns true or false using the if conditional statement.
  • If the statement is true, print “Yes, the given number has an alternate bit pattern”.
  • Else print “No, the given number doesn’t have an alternate bit pattern”.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say chek_alterntebit which takes the given number as an
# argument and returns true if it has an alternate bit pattern else returns False.


def chek_alterntebit(gvn_numbr):
    # Apply given number modulus 2 to get the last bit and store it in a variable.
    lst_bit = gvn_numbr % 2
    # Divide the given number by 2 and store it in the same variable gvn_numbr.
    gvn_numbr = gvn_numbr // 2
    # Loop until the given number is greater than 0 using a while loop.
    while (gvn_numbr > 0):
      # Inside the loop, calculate the current bit by applying the given number modulus 2
      # and store it in another variable.

        prsent_bit = gvn_numbr % 2
      # Check if the current bit is equal to the last bit using the if conditional
      # statement.
        if (prsent_bit == lst_bit):
          # If the statement is true, then return false.
            return False
        # Assign the current bit to the last bit.
        lst_bit = prsent_bit
        # Divide the given number by 2 and store it in the same variable gvn_numbr.
        gvn_numbr = gvn_numbr // 2
    # Return True. (out of while loop)
    return True


# Give the number as static input and store it in a variable.
gvn_numbr = 42
# Pass the given number as an argument to the chek_alterntebit function.
# Check if the chek_alterntebit(gvn_numbr) using the if conditional statement.
if(chek_alterntebit(gvn_numbr)):
  # If the statement is true, print "Yes, the given number has an alternate bit pattern".
    print("Yes, the given number{", gvn_numbr,
          "} has an alternate bit pattern")
else:
  # Else print "No, the given number doesn't have an alternate bit pattern".
    print("No, the given number{", gvn_numbr,
          "} doesn't have an alternate bit pattern")

Output:

Yes, the given number{ 42 } has an alternate bit pattern

Method #2: Using While loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Create a function to say chek_alterntebit which takes the given number as an argument and returns true if it has an alternate bit pattern else returns False.
  • Apply given number modulus 2 to get the last bit and store it in a variable.
  • Divide the given number by 2 and store it in the same variable gvn_numbr.
  • Loop until the given number is greater than 0 using a while loop.
  • Inside the loop, calculate the current bit by applying the given number modulus 2 and store it in another variable.
  • Check if the current bit is equal to the last bit using the if conditional statement.
  • If the statement is true, then return false.
  • Assign the current bit to the last bit.
  • Divide the given number by 2 and store it in the same variable gvn_numbr.
  • Return True. (out of while loop)
  • Pass the gvn_numbr as an argument to the chek_alterntebit() function and check whether it returns true or false using the if conditional statement.
  • If the statement is true, print “Yes, the given number has an alternate bit pattern”.
  • Else print “No, the given number doesn’t have an alternate bit pattern”.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say chek_alterntebit which takes the given number as an
# argument and returns true if it has an alternate bit pattern else returns False.


def chek_alterntebit(gvn_numbr):
    # Apply given number modulus 2 to get the last bit and store it in a variable.
    lst_bit = gvn_numbr % 2
    # Divide the given number by 2 and store it in the same variable gvn_numbr.
    gvn_numbr = gvn_numbr // 2
    # Loop until the given number is greater than 0 using a while loop.
    while (gvn_numbr > 0):
      # Inside the loop, calculate the current bit by applying the given number modulus 2
      # and store it in another variable.

        prsent_bit = gvn_numbr % 2
      # Check if the current bit is equal to the last bit using the if conditional
      # statement.
        if (prsent_bit == lst_bit):
          # If the statement is true, then return false.
            return False
        # Assign the current bit to the last bit.
        lst_bit = prsent_bit
        # Divide the given number by 2 and store it in the same variable gvn_numbr.
        gvn_numbr = gvn_numbr // 2
    # Return True. (out of while loop)
    return True


# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numbr = int(input('Enter some random number = '))
# Pass the given number as an argument to the chek_alterntebit function.
# Check if the chek_alterntebit(gvn_numbr) using the if conditional statement.
if(chek_alterntebit(gvn_numbr)):
  # If the statement is true, print "Yes, the given number has an alternate bit pattern".
    print("Yes, the given number{", gvn_numbr,
          "} has an alternate bit pattern")
else:
  # Else print "No, the given number doesn't have an alternate bit pattern".
    print("No, the given number{", gvn_numbr,
          "} doesn't have an alternate bit pattern")

Output:

Enter some random number = 14
No, the given number{ 14 } doesn't have an alternate bit pattern

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.