编写一个程序,将整数最右边的设定位置乱。 例如:
Input: 12 (00...01100)Output: 8 (00...01000)Input: 7 (00...00111)Output: 6 (00...00110)
让输入数字为n。n-1将使所有位在最右边的设定位(包括设定位)之后翻转。所以,做n&(n-1)将得到所需的结果。
那么,现在让我们看看如何 n–1 是 翻来覆去 到 正当 ( 包括…在内 这个 最右边的设定位 另外)的 N . 取n=12,所以(n-1)=11,
N 可以这样写 (n=(n-1)+1) 所以现在我们可以把这个问题看作 任何数字加1 (请参阅本文以获得更好的理解) 二进制表示法 of(n-1)=11=1011,现在让我们从(n-1)中得出n,这可以通过将1添加到(n-1)中来实现 将1与任意数字X相加时,所有位都位于 最右边的0 ( 包括…在内 这个 最右边的零 )得到 轻弹 (n-1)=1011
(n-1)+1=1100 (最右边的零(包括最右边的零)右边的所有位都被翻转了) 因为我们已经翻转了 最右边的零 所以现在, 最右边的0现在被翻转到最右边的1(也就是最右边的n位) 和 最右边0之前的所有位与之前相同 X=010。0(最右边的0)111
X+1=010。1(最右边的一个)0 0
例子:
X=71=将其视为n-1
X=1000111的二进制表示
X+1=72=将其视为n
(X+1)=1001000的二进制表示 观察:
1.X中最右边的0(不包括最右边的0)左边的所有位(认为它是n–1)与X+1中最右边的1(不包括最右边的1)左边的位(认为它是n–1)相同 属于 它是n)
2.X中最右边的0(包括最右边的0)右边的所有位(将其视为n–1)与X+1(将其视为)中最右边的1(包括最右边的1)右边的位不同 属于 它是n)
所以 X的左半部分的按位AND (直到最右边的0,不包括最右边的0)和 X+1的左半部分 (直到最右边的1,不包括最右边的1) 将给出所需的答案 , X的按位右部分 (从最右边的0)和 X+1的右边部分 (从最右边的1(最右边的设定位)) 将导致 0
C++
#include <bits/stdc++.h> using namespace std; // unsets the rightmost set bit // of n and returns the result int fun(unsigned int n) { return n & (n - 1); } // Driver Code int main() { int n = 7; cout<< "The number after unsetting the" ; cout<< " rightmost set bit " <<fun(n); return 0; } //This code is contributed by rathbhupendra |
C
#include <stdio.h> // unsets the rightmost set bit // of n and returns the result int fun(unsigned int n) { return n & (n - 1); } // Driver Code int main() { int n = 7; printf ( "The number after unsetting the" ); printf ( " rightmost set bit %d" , fun(n)); return 0; } |
JAVA
// Java program to unset the // rightmost set bit of an integer. class GFG { /* unsets the rightmost set bit of n and returns the result */ static int fun( int n) { return n & (n - 1 ); } // Driver code public static void main(String arg[]) { int n = 7 ; System.out.print( "The number after unsetting " + "the rightmost set bit " + fun(n)); } } // This code is contributed by Anant Agarwal. |
蟒蛇3
# unsets the rightmost set bit # of n and returns the result def fun(n): return n & (n - 1 ) # Driver code n = 7 print ( "The number after unsetting the rightmost set bit" , fun(n)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to unset the // rightmost set bit of an integer. using System; class GFG { /* unsets the rightmost set bit of n and returns the result */ static int fun( int n) { return n & (n - 1); } // Driver code public static void Main() { int n = 7; Console.Write( "The number after unsetting " + "the rightmost set bit " + fun(n)); } } // This code is contributed by Sam007 |
Javascript
<script> // JavaScript program for the above approach /* unsets the rightmost set bit of n and returns the result */ function fun(n) { return n & (n - 1); } // Driver Code let n = 7; document.write( "The number after unsetting " + "the rightmost set bit " + fun(n)); // This code is contributed by susmitakundugoaldanga. </script> |
PHP
<?php // unsets the rightmost set bit // of n and returns the result function fun( $n ) { return $n & ( $n - 1); } // Driver Code $n = 7; echo "The number after unsetting the" . " rightmost set bit " , fun( $n ); // This code is contributed by vt_m. ?> |
输出:
The number after unsetting the rightmost set bit 6
时间复杂性: O(1)
辅助空间: O(1)
如果您发现上述代码/算法不正确,请写评论,或者找到更好的方法来解决相同的问题