Modify a bit at a given position
Last Updated :
18 Sep, 2023
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
2nd 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))
Below is the implementation of above steps :
C++
// 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));
}
// Driver code
int main()
{
cout << modifyBit(6, 2, 0) << endl;
cout << modifyBit(6, 5, 1) << endl;
return 0;
}
C
// C program to modify a bit at position
// p in n to b.
#include <stdio.h>
// Returns modified n.
int modifyBit(int n, int p, int b)
{
int mask = 1 << p;
return ((n & ~mask) | (b << p));
}
// Driver code
int main()
{
printf("%d\n",modifyBit(6, 2, 0));
printf("%d\n",modifyBit(6, 5, 1));
return 0;
}
// This code is contributed by kothvvsaakash.
Java
// 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
Python3
# 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
C#
// 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
PHP
<?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
?>
JavaScript
<script>
// Javascript program to modify a bit
// at position p in n to b.
// Returns modified n.
function modifyBit(n, p, b)
{
let mask = 1 << p;
return (n & ~mask) |
((b << p) & mask);
}
// Driver code
document.write(modifyBit(6, 2, 0) + "<br/>");
document.write(modifyBit(6, 5, 1));
// This code is contributed by susmitakundugoaldanga
</script>
Output :
2
38
Time Complexity : O(1)
Auxiliary Space : O(1)
Similar Reads
Extract 'k' bits from a given position in a number. How to extract 'k' bits from a given position 'p' in a number? Examples: Input : number = 171 k = 5 p = 2 Output : The extracted number is 21 171 is represented as 10101011 in binary, so, you should get only 10101 i.e. 21. Input : number = 72 k = 5 p = 1 Output : The extracted number is 8 72 is repr
4 min read
Binary representation of a given number Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000
6 min read
Find position of the only set bit Given a number n containing only 1 set bit in its binary representation, the task is to find the position of the only set bit. If there are 0 or more than 1 set bits, then return -1. Note: Position of set bit '1' should be counted starting with 1 from the LSB side in the binary representation of the
8 min read
Position of the K-th set bit in a number Given two numbers N and K, The task is to find the index of the K-th set bit in the number from the right. Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number "000011", the first set bit is at index 0 from the right, and the second set bit is at
6 min read
Position of rightmost different bit Given two numbers m and n. Find the position of the rightmost different bit in the binary representation of numbers. It is guaranteed that such a bit exists Examples: Input: m = 11, n = 9Output: 2Explanation: (11)10 = (1011)2(9)10 = (1001)2It can be seen that 2nd bit from the right is different Inpu
8 min read
Set the K-th bit of a given number Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1
4 min read