How to turn on a particular bit in a number ? | CPP program to turn on a particular bit ; Returns a number that has all bits same as n except the k 'th bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int turnOnK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n | ( 1 << ( k - 1 ) ) ) ; } int main ( ) { int n = 4 ; int k = 2 ; cout << turnOnK ( n , k ) ; return 0 ; }