Problem Statement:- Program to Get or Find nth Bit of a Number.
Sample Input/Output:-
Data requirement:-
Input Data:- number, bit_pos
Output Data:-String Output
Additional Data:- mask
Program in C
Here is the source code of the C Program to get an 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 get(Between 0-31):"); scanf("%d", &bit_pos); /* Calculating the bit mask*/ int mask=(1<<bit_pos); /* Checking the Given Position bit is 0 or 1*/ if (number & mask) printf("Given Position bit is 1.\n"); else printf("Given Position bit is 0.\n"); return 0;}
Input/Output:
Enter the Number:13
Enter the Bit position you want to get(Between 0-31):3
Given Position bit is 1.
#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 get(Between 0-31):"); scanf("%d", &bit_pos); /* Calculating the bit mask*/ int mask=(1<<bit_pos); /* Checking the Given Position bit is 0 or 1*/ if (number & mask) printf("Given Position bit is 1.\n"); else printf("Given Position bit is 0.\n"); return 0;}
Program in C++
Here is the source code of the C++ Program to Find 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 get(Between 0-31):"; cin >> bit_pos; /* Calculating the bit mask*/ int mask = (1 << bit_pos); /* Checking the Given Position bit is 0 or 1*/ if (number & mask) cout << "Given Position bit is 1.\n"; else cout << "Given Position bit is 0.\n"; 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 get(Between 0-31):"; cin >> bit_pos; /* Calculating the bit mask*/ int mask = (1 << bit_pos); /* Checking the Given Position bit is 0 or 1*/ if (number & mask) cout << "Given Position bit is 1.\n"; else cout << "Given Position bit is 0.\n"; return 0;}
Input/Output:
Enter the Number:39
Enter the Bit position you want to get(Between 0-31):4
Given Position bit is 0.
Program in Java
Here is the source code of the Java Program to get an nth bit of a number.
Code:
import java.util.Scanner;
public class GetNthBit { 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 get(Between 0-31):"); int bit_pos = sc.nextInt(); /* Calculating the bit mask */ int mask = (1 << bit_pos); /* Checking the Given Position bit is 0 or 1 */ if ((number & mask) == 1) System.out.print("Given Position bit is 1."); else System.out.print("Given Position bit is 0."); sc.close(); }}
Input/Output:
Enter the Number:254
Enter the Bit position you want to get(Between 0-31):1
Given Position bit is 0.
import java.util.Scanner;
public class GetNthBit { 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 get(Between 0-31):"); int bit_pos = sc.nextInt(); /* Calculating the bit mask */ int mask = (1 << bit_pos); /* Checking the Given Position bit is 0 or 1 */ if ((number & mask) == 1) System.out.print("Given Position bit is 1."); else System.out.print("Given Position bit is 0."); sc.close(); }}
Program in Python
Here is the source code of the Python Program to Find 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 Get(Between 0-31):"))# Calculating the bit maskmask = (1 << bit_pos)# Checking the Given Position bit is 0 or 1if (number & mask): print("Given Position bit is 1.")else: print("Given Position bit is 0.")
Input/Output:
Enter the Number:777
Enter the Bit position you want to Get(Between 0-31):8
Given Position bit is 1.
# Get the number inputnumber = int(input("Enter the Number:"))# Get the bit position inputbit_pos = int(input("Enter the Bit position you want to Get(Between 0-31):"))# Calculating the bit maskmask = (1 << bit_pos)# Checking the Given Position bit is 0 or 1if (number & mask): print("Given Position bit is 1.")else: print("Given Position bit is 0.")
2 Comments
The solution is not correct. if (number & mask != 0) then 1, else 0. this should be the correct answer.
ReplyDeleteyes you are r8
DeletePlease do not Enter any spam link in the comment box