Problem Statement:- Program to Clear the nth bit of a Number.
Sample Input/Output:-
Data requirement:-
Input Data:- number, bit_pos
Output Data:-number
Additional Data:- mask
Program in C
Here is the source code of the C Program to clear the nth bit of a number.
Code:
#include <stdio.h>int main(){ int number, bit_pos; /* Get the number input */ printf("Enter the Number:"); scanf("%d", &number); /* Get the bit position input */ printf("Enter the Bit position you want to clear(Between 0-31):"); scanf("%d", &bit_pos); /* Calculating the bit mask*/ int mask = (1 << bit_pos); /*~(Invert sign, it's convert o to 1 or 1 to 0.) */ number = number & (~mask); printf("The number after clear the bit in the given position is: %d", number); return 0;}
Input/Output:
Enter the Number:6
Enter the Bit position you want to clear(Between 0-31):1
The number after clear the bit in the given position is: 4
#include <stdio.h>int main(){ int number, bit_pos; /* Get the number input */ printf("Enter the Number:"); scanf("%d", &number); /* Get the bit position input */ printf("Enter the Bit position you want to clear(Between 0-31):"); scanf("%d", &bit_pos); /* Calculating the bit mask*/ int mask = (1 << bit_pos); /*~(Invert sign, it's convert o to 1 or 1 to 0.) */ number = number & (~mask); printf("The number after clear the bit in the given position is: %d", number); return 0;}
Program in C++
Here is the source code of the C++ Program to clear the nth bit of a number.
Code:
#include <iostream>using namespace std;int main(){ int number, bit_pos; /* Get the number input */ cout << "Enter the Number:"; cin >> number; /* Get the bit position input */ cout << "Enter the Bit position you want to clear(Between 0-31):"; cin >> bit_pos; /* Calculating the bit mask*/ int mask = (1 << bit_pos); /*~(Invert sign, it's convert o to 1 or 1 to 0.) */ number = number & (~mask); cout << "The number after clear the bit in the given position is: " << number; return 0;}
#include <iostream>using namespace std;int main(){ int number, bit_pos; /* Get the number input */ cout << "Enter the Number:"; cin >> number; /* Get the bit position input */ cout << "Enter the Bit position you want to clear(Between 0-31):"; cin >> bit_pos; /* Calculating the bit mask*/ int mask = (1 << bit_pos); /*~(Invert sign, it's convert o to 1 or 1 to 0.) */ number = number & (~mask); cout << "The number after clear the bit in the given position is: " << number; return 0;}
Input/Output:
Enter the Number:17
Enter the Bit position you want to clear(Between 0-31):0
The number after clear the bit in the given position is: 16
Program in Java
Here is the source code of the Java Program to clear the nth bit of a number.
Code:
import java.util.Scanner;
public class ClearNthBit { public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* Get the number input */ System.out.print("Enter the Number:"); int number = sc.nextInt(); /* Get the bit position input */ System.out.print("Enter the Bit position you want to clear(Between 0-31):"); int bit_pos = sc.nextInt(); /* Calculating the bit mask */ int mask = (1 << bit_pos); /* ~(Invert sign, it's convert o to 1 or 1 to 0.) */ number = number & (~mask); System.out.print("The number after clear the bit in the given position is: " + number); sc.close(); }}
Input/Output:
Enter the Number:63
Enter the Bit position you want to clear(Between 0-31):5
The number after clear the bit in the given position is: 31
import java.util.Scanner;
public class ClearNthBit { public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* Get the number input */ System.out.print("Enter the Number:"); int number = sc.nextInt(); /* Get the bit position input */ System.out.print("Enter the Bit position you want to clear(Between 0-31):"); int bit_pos = sc.nextInt(); /* Calculating the bit mask */ int mask = (1 << bit_pos); /* ~(Invert sign, it's convert o to 1 or 1 to 0.) */ number = number & (~mask); System.out.print("The number after clear the bit in the given position is: " + number); sc.close(); }}
Program in Python
Here is the source code of the Python Program to clear the nth bit of a number.
Code:
# Get the number inputnumber = int(input("Enter the Number:"))# Get the bit position inputbit_pos = int(input("Enter the Bit position you want to Clear(Between 0-31):"))# Calculating the bit maskmask = (1 << bit_pos)# ~(Invert sign, it's convert o to 1 or 1 to 0.)number = number & (~mask)print("The number after Clear the bit in the given position is:", number)
Input/Output:
Enter the Number:125
Enter the Bit position you want to Clear(Between 0-31):4
The number after Clear the bit in the given position is: 109
# Get the number inputnumber = int(input("Enter the Number:"))# Get the bit position inputbit_pos = int(input("Enter the Bit position you want to Clear(Between 0-31):"))# Calculating the bit maskmask = (1 << bit_pos)# ~(Invert sign, it's convert o to 1 or 1 to 0.)number = number & (~mask)print("The number after Clear the bit in the given position is:", number)
0 Comments
Please do not Enter any spam link in the comment box