Program to set nth bit of a number

 

Problem Statement:- Program to set the nth bit of a Number.

Sample Input/Output:-


Sample Input First:

8/Number

1//Position

/*13 Binary=>1000*/

Sample Output First: 

10

/*10 Binary=>01010*/

Sample Input Second: 

15//Number

4//Position

/*15 Binary=>01111*/

Sample Output Second: 

31

/*31 Binary=>11111*/


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 set 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 set(Between 0-31):");
    scanf("%d", &bit_pos);
    /* Calculating the bit mask*/
    int mask=(1<<bit_pos);
        number=number|mask;
    printf("The number after set the bit in the given position is: %d",number);
    return 0;
}


Input/Output:

Enter the Number:8

Enter the Bit position you want set(Between 0-31):1

The number after set the bit in the given position is: 10


Program in C++

Here is the source code of the C++ Program to set the nth bit of a number.


Code:

#include <iostream>
using namespace std;
int main()
{
    int numberbit_pos;
    /* Get the number input */
    cout << "Enter the Number:";
    cin >> number;
    /* Get the bit position input */
    cout << "Enter the Bit position you want to set(Between 0-31):";
    cin >> bit_pos;
    /*Calculating the bit mask*/
    int mask = (1 << bit_pos);
    number = number | mask;
    cout << "The number after set the bit in the given position is:" << number;
    return 0;
}


Input/Output:

Enter the Number:15

Enter the Bit position you want to set(Between 0-31):4

The number after set the bit in the given position is:31


Program in Java

Here is the source code of the Java Program to set the nth bit of a number.


Code:

import java.util.Scanner;

public class SetNthBits {
    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 set(Between 0-31):");
        int bit_pos = sc.nextInt();
        /* Calculating the bit mask */
        int mask = (1 << bit_pos);
        number = number | mask;
        System.out.print("The number after set the bit in the given position is: " + number);
        sc.close();
    }
}


Input/Output:

Enter the Number:787

Enter the Bit position you want to set(Between 0-31):3

The number after set the bit in the given position is: 795


Program in Python

Here is the source code of the Python Program to set the nth bit of a number.


Code:

# Get the number input
number = int(input("Enter the Number:"))
# Get the bit position input
bit_pos = int(input("Enter the Bit position you want to set(Between 0-31):"))
# Calculating the bit mask
mask = (1 << bit_pos)
number = number | mask
print("The number after set the bit in the given position is:"number)



Input/Output:

Enter the Number:450

Enter the Bit position you want to set(Between 0-31):0

The number after set the bit in the given position is: 451

Most Recommend Questions:-







More Questions:-


Post a Comment

0 Comments