在给定位置修改一个位

给定一个数字 N ,一个职位 P 和一个二进制值 B ,我们需要将n中位置p处的位更改为值b。

null

例如:

Input : n = 7, p = 2, b = 0Output : 37 is 00000111 after clearing bit at 2rd position, it becomes 0000011.Input : n = 7, p = 3, b = 1Output : 157 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 << positionThen to change value of bit to b, we firstmake it 0 using below operation      value & ~maskAfter changing it 0, we change it to b bydoing or of above expression with following(b << p) & mask, i.e., we return      ((n & ~mask) | (b << p))

以下是上述步骤的实施情况:

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;
}


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), "" ;
echo modifyBit(6, 5, 1) , "" ;
// 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>


输出:

 2 38

时间复杂性: O(1)

本文由 帕万·阿西普 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享