Modify a bit at a given position

Given a number n, a position p and a binary value b, we need to change the bit at position p in n to value b.

Examples :

Input : n = 7, p = 2, b = 0
Output : 3
7 is 00000111 after clearing bit at 
2rd position, it becomes 0000011.

Input : n = 7, p = 3, b = 1
Output : 15
7 is 00000111 after setting bit at 
3rd position it becomes 00001111.
We first create a mask that has set bit only 
at given position using bit wise shift.
      mask = 1 << position

Then to change value of bit to b, we first
make it 0 using below operation
      value & ~mask

After changing it 0, we change it to b by
doing or of above expression with following
(b << p) & mask, i.e., we return
      (n & ~mask) | ((b << p) & mask)
 

Below is the implementation of above steps :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to modify a bit
// at position p in n to b.
import java.io.*;
  
class GFG 
{
      
// Returns modified n.
public static int modifyBit(int n, 
                            int p, 
                            int b)
{
    int mask = 1 << p;
    return (n & ~mask) | 
           ((b << p) & mask);
}
    // Driver Code
    public static void main (String[] args)
    {
        System.out.println(modifyBit(6, 2, 0));
        System.out.println (modifyBit(6, 5, 1));
    }
}
  
// This code is contributed by m_kit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to modify a bit at position
# p in n to b.
  
# Returns modified n.
def modifyBit( n,  p,  b):
    mask = 1 << p
    return (n & ~mask) | ((b << p) & mask)
   
# Driver code
def main():
    print(modifyBit(6, 2, 0))
    print(modifyBit(6, 5, 1))
      
if __name__ == '__main__':
    main()
# This code is contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to modify a bit
// at position p in n to b.
using System;
  
class GFG
{
// Returns modified n.
public static int modifyBit(int n, 
                            int p, 
                            int b)
{
    int mask = 1 << p;
    return (n & ~mask) | 
           ((b << p) & mask);
}
  
    // Driver Code
    static public void Main ()
    {
      
        Console.WriteLine(modifyBit(6, 2, 0));
        Console.WriteLine(modifyBit(6, 5, 1));
    }
}
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to modify a bit
// at position p in n to b.
  
// Returns modified n.
function modifyBit($n, $p, $b)
{
    $mask = 1 << $p;
    return ($n & ~$mask) | 
           (($b << $p) & $mask);
}
  
    // Driver code
    echo modifyBit(6, 2, 0),"\n";
    echo modifyBit(6, 5, 1) ,"\n";
      
// This code is contributed by ajit
?>

chevron_right



Output :

 2
 38

Time Complexity :O(1)

This article is contributed by Pawan Asipu. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.