Program to Check whether kth Bit is Set or not

Python Program to Check whether kth Bit is Set or not

When it comes to programming challenges, bits are really significant. In this post, we looked at how to use Python to determine whether the kth bit of a number is set or not.

Examples:

Example1:

Input:

Given Number =29
Given K=3

Output:

Enter the given number and the value of k separated by spaces = 29 3
The 3 nd bit in the number 29 (binary representation = 11101 ) is set bit

Example2:

Input:

Given Number =576
Given K=3

Output:

The 3 nd bit in the number number 576 (binary representation = 1001000000 )is not a set bit

Program to Check whether kth Bit is Set or not in Python

Below are the ways to check whether the kth bit is set bit or not in python.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method #1: Using Left Shift Operator (Static Input)

Approach:

  • Give the number and the value of k as static input and store it in a variable.
  • First, compute 1<<(k-1) and save it in a variable called temp, resulting in temp=1<<(k-1). temp holds an integer with only the kth bit set.
  • You must execute a bitwise AND of number and temp in this step. If this gives a non-zero integer, the kth bit of the number is set otherwise, it is not.
  • To Check the above statement using the If conditional Statement.
  • If it is true then print the kth bit is set bit.
  • Else print it is not set bit.
  • The Exit of the Program.

Below is the implementation:

# Give the number and the value of k as static input
# and store it in a variable.
numb = 19
k = 2
# First, compute 1<<(k-1) and save it in a variable called temp,
# resulting in temp=1<<(k-1). temp holds an integer with only the kth bit set.
temp = 1 << (k-1)
# You must execute a bitwise AND of number and temp in this step.
# If this gives a non-zero integer, the kth bit of the number is set otherwise, it is not.
# To Check the above statement using the If conditional Statement.
if (numb & temp):
        # If it is true then print the kth bit is set bit.
    print('The', k, 'nd bit in the number ', numb,
          '(binary representation =', bin(numb)[2:], ') is set bit')
# Else print it is not set bit.
else:
    print('The', k, 'nd bit in the number ', numb,
          '(binary representation =', bin(numb)[2:], ')is not a set bit')

Output:

The 2 nd bit in the number 19 (binary representation = 10011 ) is set bit

Method #2: Using Left Shift Operator (User Input)

Approach:

  • Give the number and the value of k as user input using map(), int(), split() functions and store them in two separate variables.
  • First, compute 1<<(k-1) and save it in a variable called temp, resulting in temp=1<<(k-1). temp holds an integer with only the kth bit set.
  • You must execute a bitwise AND of number and temp in this step. If this gives a non-zero integer, the kth bit of the number is set otherwise, it is not.
  • To Check the above statement using the If conditional Statement.
  • If it is true then print the kth bit is set bit.
  • Else print it is not set bit.
  • The Exit of the Program.

Below is the implementation:

# Give the number and the value of k as user input using map(), int(), split() functions
# and store them in two separate variables.
numb, k = map(int, input(
    'Enter the given number and the value of k separated by spaces = ').split())
# First, compute 1<<(k-1) and save it in a variable called temp,
# resulting in temp=1<<(k-1). temp holds an integer with only the kth bit set.
temp = 1 << (k-1)
# You must execute a bitwise AND of number and temp in this step.
# If this gives a non-zero integer, the kth bit of the number is set otherwise, it is not.
# To Check the above statement using the If conditional Statement.
if (numb & temp):
        # If it is true then print the kth bit is set bit.
    print('The', k, 'nd bit in the number', numb,
          '(binary representation =', bin(numb)[2:], ') is set bit')
# Else print it is not set bit.
else:
    print('The', k, 'nd bit in the number', numb,
          '(binary representation =', bin(numb)[2:], ')is not a set bit')

Output:

Enter the given number and the value of k separated by spaces = 29 3
The 3 nd bit in the number 29 (binary representation = 11101 ) is set bit

Method #3: Using Right Shift Operator (Static Input)

Approach:

  • Give the number and the value of k as static input and store it in a variable.
  • First, compute number>>(k-1) and store it in a variable called temp, resulting in temp=number>>(k-1). If the kth bit is set, the last bit of temp will be 1, otherwise, it will be 0.
  • You must execute a bitwise AND of 1 and temp in this step. If this gives a non-zero integer, the kth bit of the number is set, otherwise, it is not.
  • To Check the above statement using the If conditional Statement.
  • If it is true then print the kth bit is set bit.
  • Else print it is not set bit.
  • The Exit of the Program.

Below is the implementation:

# Give the number and the value of k as static input # and store it in a variable.
numb = 19
k = 2
# First, compute number>>(k-1) and store it in a variable called temp, resulting in temp=number>>(k-1).
# If the kth bit is set, the last bit of temp will be 1, otherwise, it will be 0.
temp = numb >> (k-1)
# You must execute a bitwise AND of 1 and temp in this step.
# If this gives a non-zero integer,
# the kth bit of the number is set, otherwise, it is not.
if (1 & temp):
        # If it is true then print the kth bit is set bit.
    print('The', k, 'nd bit in the number ', numb,
          '(binary representation =', bin(numb)[2:], ') is set bit')
# Else print it is not set bit.
else:
    print('The', k, 'nd bit in the number', numb,
          '(binary representation =', bin(numb)[2:], ')is not a set bit')

Output:

The 2 nd bit in the number 19 (binary representation = 10011 ) is set bit

Method #4: Using Right Shift Operator (User Input)

Approach:

  • Give the number and the value of k as user input using map(), int(), split() functions and store them in two separate variables.
  • First, compute number>>(k-1) and store it in a variable called temp, resulting in temp=number>>(k-1). If the kth bit is set, the last bit of temp will be 1, otherwise, it will be 0.
  • You must execute a bitwise AND of 1 and temp in this step. If this gives a non-zero integer, the kth bit of the number is set, otherwise, it is not.
  • To Check the above statement using the If conditional Statement.
  • If it is true then print the kth bit is set bit.
  • Else print it is not set bit.
  • The Exit of the Program.

Below is the implementation:

# Give the number and the value of k as user input using map(), int(), split() functions
# and store them in two separate variables.
numb, k = map(int, input(
    'Enter the given number and the value of k separated by spaces = ').split())
# First, compute number>>(k-1) and store it in a variable called temp, resulting in temp=number>>(k-1).
# If the kth bit is set, the last bit of temp will be 1, otherwise, it will be 0.
temp = numb >> (k-1)
# You must execute a bitwise AND of 1 and temp in this step.
# If this gives a non-zero integer,
# the kth bit of the number is set, otherwise, it is not.
if (1 & temp):
        # If it is true then print the kth bit is set bit.
    print('The', k, 'nd bit in the number', numb,
          '(binary representation =', bin(numb)[2:], ') is set bit')
# Else print it is not set bit.
else:
    print('The', k, 'nd bit in the number', numb,
          '(binary representation =', bin(numb)[2:], ')is not a set bit')

Output:

Enter the given number and the value of k separated by spaces = 576 3
The 3 nd bit in the number 576 (binary representation = 1001000000 )is not a set bit

Related Programs: