42) Write a program to set nth bit of a number in C++. (1)
In this, we are going to see a program to Set nth bit of a Number in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>

void main()
{
    clrscr();
	int num, n, setter;
	cout<<"Enter a number: ";;
	cin>>num;
	cout<<"Enter the place(n) of the bit you want to set: ";
	cin>>n;
	setter = 1 << n;
	num = num | setter;
	cout<<"The number after setting "<<n<<" th place is: "<<num;
	getch();
}


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;
int main()
{
	int num, n, setter;
	cout<<"Enter a number: ";;
	cin>>num;
	cout<<"Enter the place(n) of the bit you want to set: ";
	cin>>n;
	setter = 1 << n;
	num = num | setter;
	cout<<"The number after setting "<<n<<" th place is: "<<num;
	return 0;
}


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post