给定数字N。任务是取消设置N的最右边的位。
例子:
Input: N = 5
Output: 4
Explanation:
101(5) -> 100(4)
Input : N = 78
Output : 76
Explanation:
1001110(78) -> 1001100(76)
天真的方法:
- 翻转最右置位的一种简单方法是通过按位右移操作来查找数字中最右置位的位置,以检查第一次出现的1。
- 然后,在该位置翻转钻头。可以通过将给定数字和该数字与该位置位中的位进行XOR运算来完成翻转。
翻转位的重要属性:
0 ^ 1 = 1
1 ^ 1 = 0
Xor with 1 flips the bit.
- 通过将2设置为特定位的位置的幂可以完成仅设置给定位的操作。
下面是上述方法的实现。
C++
// C++ program to unset the rightmost
// set bit
#include
using namespace std;
// Unsets the rightmost set bit
// of n and returns the result
void FlipBits(int n)
{
for (int bit = 0; bit < 32; bit++)
{
// Checking whether bit position is
// set or not
if ((n >> bit) & 1)
{
// If bit position is found set,
// we flip this bit by xoring
// given number and number with
// bit position set
n = n ^ (1ll << bit);
break;
}
}
cout<<"The number after unsetting the";
cout<<" rightmost set bit "<< n;
}
// Driver code
int main()
{
int N = 12;
FlipBits(N);
return 0;
}
Java
// Java program to unset the rightmost
// set bit
import java.util.*;
class GFG{
// Unsets the rightmost set bit
// of n and returns the result
static void FlipBits(int n)
{
for(int bit = 0; bit < 32; bit++)
{
// Checking whether bit position
// is set or not
if ((n >> bit) % 2 > 0)
{
// If bit position is found set,
// we flip this bit by xoring
// given number and number with
// bit position set
n = n ^ (1 << bit);
break;
}
}
System.out.print("The number after unsetting the");
System.out.print(" rightmost set bit " + n);
}
// Driver code
public static void main(String[] args)
{
int N = 12;
FlipBits(N);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to unset the rightmost
# set bit
# Unsets the rightmost set bit
# of n and returns the result
def FlipBits(n):
for bit in range(32):
# Checking whether bit position is
# set or not
if ((n >> bit) & 1):
# If bit position is found set,
# we flip this bit by xoring
# given number and number with
# bit position set
n = n ^ (1 << bit)
break
print("The number after unsetting the", end = " ")
print("rightmost set bit", n)
# Driver code
if __name__ == '__main__':
N = 12;
FlipBits(N)
# This code is contributed by BhupendraSingh
C#
// C# program to unset the rightmost
// set bit
using System;
class GFG{
// Unsets the rightmost set bit
// of n and returns the result
static void FlipBits(int n)
{
for(int bit = 0; bit < 32; bit++)
{
// Checking whether bit position
// is set or not
if ((n >> bit) % 2 > 0)
{
// If bit position is found set,
// we flip this bit by xoring
// given number and number with
// bit position set
n = n ^ (1 << bit);
break;
}
}
Console.Write("The number after unsetting the ");
Console.Write("rightmost set bit " + n);
}
// Driver code
static void Main()
{
int N = 12;
FlipBits(N);
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ program to unset the rightmost
// set bit
#include
using namespace std;
// Unsets the rightmost set bit
// of n and returns the result
int FlipBits(unsigned int n)
{
return n -= (n & (-n));
}
// Driver Code
int main()
{
int N = 12;
cout<<"The number after unsetting the";
cout<<" rightmost set bit: "<
Java
// Java program to unset the rightmost set bit
import java.util.*;
class GFG{
// Unsets the rightmost set bit
// of n and returns the result
static int FlipBits(int n)
{
return n -= (n & (-n));
}
// Driver Code
public static void main(String[] args)
{
int N = 12;
System.out.print("The number after unsetting the ");
System.out.print("rightmost set bit: " + FlipBits(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to unset the rightmost set bit
# Unsets the rightmost set bit
# of n and returns the result
def FlipBits(n):
n -= (n & (-n));
return n;
# Driver Code
if __name__ == '__main__':
N = 12;
print("The number after unsetting the", end = "");
print(" rightmost set bit: ", FlipBits(N));
# This code is contributed by Rohit_ranjan
C#
// C# program to unset the rightmost set bit
using System;
class GFG{
// Unsets the rightmost set bit
// of n and returns the result
static int FlipBits(int n)
{
return n -= (n & (-n));
}
// Driver Code
public static void Main(String[] args)
{
int N = 12;
Console.Write("The number after" +
"unsetting the ");
Console.Write("rightmost set bit: " +
FlipBits(N));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
The number after unsetting the rightmost set bit 8
高效方法:
我们可以翻转O(1)中的LSB。尽管幼稚的方法也能很好地工作,但也存在针对该问题的单线解决方案。我们可以执行以下操作:
- 获得数字的2的补数(简单地-n给出2的补数)
- 在数字和2的补码之间执行按位AND运算。
- 从给定的数字中减去以上结果。
- 最终结果是LSB翻转了。
讲解
N = 1001110 (78)
2's compliment = 0110010
N & (-N) = 0000010
N - (N & (-N)) = 1001100 (76)
C++
// C++ program to unset the rightmost
// set bit
#include
using namespace std;
// Unsets the rightmost set bit
// of n and returns the result
int FlipBits(unsigned int n)
{
return n -= (n & (-n));
}
// Driver Code
int main()
{
int N = 12;
cout<<"The number after unsetting the";
cout<<" rightmost set bit: "<
Java
// Java program to unset the rightmost set bit
import java.util.*;
class GFG{
// Unsets the rightmost set bit
// of n and returns the result
static int FlipBits(int n)
{
return n -= (n & (-n));
}
// Driver Code
public static void main(String[] args)
{
int N = 12;
System.out.print("The number after unsetting the ");
System.out.print("rightmost set bit: " + FlipBits(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to unset the rightmost set bit
# Unsets the rightmost set bit
# of n and returns the result
def FlipBits(n):
n -= (n & (-n));
return n;
# Driver Code
if __name__ == '__main__':
N = 12;
print("The number after unsetting the", end = "");
print(" rightmost set bit: ", FlipBits(N));
# This code is contributed by Rohit_ranjan
C#
// C# program to unset the rightmost set bit
using System;
class GFG{
// Unsets the rightmost set bit
// of n and returns the result
static int FlipBits(int n)
{
return n -= (n & (-n));
}
// Driver Code
public static void Main(String[] args)
{
int N = 12;
Console.Write("The number after" +
"unsetting the ");
Console.Write("rightmost set bit: " +
FlipBits(N));
}
}
// This code is contributed by 29AjayKumar
Java脚本
输出:
The number after unsetting the rightmost set bit: 8