Modify a bit at a given position | CPP program to modify a bit at position p in n to b . ; Returns modified n . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int modifyBit ( int n , int p , int b ) { int mask = 1 << p ; return ( ( n & ~ mask ) | ( b << p ) ) ; } int main ( ) { cout << modifyBit ( 6 , 2 , 0 ) << endl ; cout << modifyBit ( 6 , 5 , 1 ) << endl ; return 0 ; }