📜  在最高有效位之后切换所有位

📅  最后修改于: 2021-05-06 17:23:43             🧑  作者: Mango

给定一个数字,请在最高有效位(包括最高有效位)之后切换其所有位。

例子 :

Input : 10 
Output : 5
Binary representation of 10 is 1010
After toggling we get 0101

Input : 5
Output : 2

我们可以通过将其与1进行XOR来切换一位(请注意1 ^ 0 = 1和1 ^ 1 = 0)。这个想法是采用仅设置一位的数字温度。逐一将温度的唯一设定位向左移动,并与n进行XOR运算,直到它超过n的MSB(最高有效位)为止。

C++
// CPP program to toggle set  bits starting 
// from MSB
#include
using namespace std;
  
void toggle(int &n)
{
    // temporary variable to
    // use XOR with one of a n
    int temp = 1;
  
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n)
    {
        // Toggle bit of n
        // corresponding to
        // current set bit in
        // temp.
        n = n ^ temp;
  
        // Move set bit to next
        // higher position.
        temp = temp << 1;
    }
}
  
// Driver code
int main()
{
    int n = 10;
    toggle(n);
    cout << n;
    return 0;
}


Java
// Java program to toggle set 
// bits starting from MSB
  
class GFG {
      
static int toggle(int n) {
      
    // temporary variable to
    // use XOR with one of a n
    int temp = 1;
  
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n) {
          
    // Toggle bit of n
    // corresponding to
    // current set bit in
    // temp.
    n = n ^ temp;
  
    // Move set bit to next
    // higher position.
    temp = temp << 1;
    }
    return n;
}
  
// Driver code
public static void main(String arg[]) 
{
    int n = 10;
    n = toggle(n);
    System.out.print(n);
}
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python program to toggle
# set  bits starting 
# from MSB
  
def toggle(n):
  
    # temporary variable to
    # use XOR with one of a n
    temp = 1
   
    #Run loop until the only
    #set bit in temp crosses
    #MST of n.
    while (temp <= n):
      
        # Toggle bit of n
        # corresponding to
        # current set bit in
        # temp.
        n = n ^ temp
   
        # Move set bit to next
        # higher position.
        temp = temp << 1
  
    return n
   
# Driver code
  
n = 10
n=toggle(n)
print(n)
  
# This code is contributed
# by Anant Agarwal.


C#
// C# program to toggle set 
// bits starting from MSB
using System;
  
class GFG {
  
// Function to toggle bits
// starting from MSB    
static int toggle(int n) {
      
    // temporary variable to
    // use XOR with one of a n
    int temp = 1;
  
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n) {
          
    // Toggle bit of n
    // corresponding to
    // current set bit in
    // temp.
    n = n ^ temp;
  
    // Move set bit to next
    // higher position.
    temp = temp << 1;
    }
    return n;
}
  
// Driver code
public static void Main() 
{
    int n = 10;
    n = toggle(n);
    Console.Write(n);
}
}
  
// This code is contributed by Nitin Mittal.


PHP


C++
// CPP program to toggle set  bits starting 
// from MSB
#include
using namespace std;
  
// Returns a number which has all set bits
// starting from MSB of n
int setAllBitsAfterMSB(int n)
{    
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n>>1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n>>2;   
  
    n |= n>>4;  
    n |= n>>8;
    n |= n>>16;
    return n;
}
  
void toggle(int &n)
{
    n = n ^ setAllBitsAfterMSB(n);
}
  
// Driver code
int main()
{
    int n = 10;
    toggle(n);
    cout << n;
    return 0;
}


Java
// Java program to toggle set bits 
// starting from MSB
  
class GFG {
      
// Returns a number which has all 
// set bits starting from MSB of n
static int setAllBitsAfterMSB(int n) {
      
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
  
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    return n;
}
static int toggle(int n) 
{
    n = n ^ setAllBitsAfterMSB(n);
    return n;
}
  
// Driver code
public static void main(String arg[]) 
{
    int n = 10;
    n = toggle(n);
    System.out.print(n);
}
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python program to toggle set  bits starting 
# from MSB
  
# Returns a number which has all set bits
# starting from MSB of n
def setAllBitsAfterMSB(n):
   
    # This makes sure two bits
    # (From MSB and including MSB)
    # are set
    n |= n>>1
   
    # This makes sure 4 bits
    # (From MSB and including MSB)
    # are set
    n |= n>>2   
   
    n |= n>>4  
    n |= n>>8
    n |= n>>16
    return n
  
def toggle(n):
  
    n = n ^ setAllBitsAfterMSB(n)
    return n
      
#Driver code
  
n = 10
n=toggle(n)
print(n)
# This code is contributed by Anant Agarwal.


C#
// C# program to toggle set bits 
// starting from MSB
using System;
  
class GFG {
          
    // Returns a number which has all 
    // set bits starting from MSB of n
    static int setAllBitsAfterMSB(int n)
    {
          
        // This makes sure two bits
        // (From MSB and including MSB)
        // are set
        n |= n >> 1;
      
        // This makes sure 4 bits
        // (From MSB and including MSB)
        // are set
        n |= n >> 2;
      
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
        return n;
    }
      
    static int toggle(int n) 
    {
        n = n ^ setAllBitsAfterMSB(n);
        return n;
    }
      
    // Driver code
    public static void Main() 
    {
        int n = 10;
        n = toggle(n);
        Console.WriteLine(n);
    }
}
  
// This code is contributed by Sam007.


PHP
> 1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    $n |= $n >> 2; 
  
    $n |= $n >> 4; 
    $n |= $n >> 8;
    $n |= $n >> 16;
    return $n;
}
function toggle(&$n)
{
    $n = $n ^ setAllBitsAfterMSB($n);
}
  
// Driver Code
$n = 10;
toggle($n);
echo $n;
  
// This code is contributed by ajit
?>


输出 :

5

在数字以32位存储的假设下,可以优化上述解决方案以使其在O(1)时间内工作。

C++

// CPP program to toggle set  bits starting 
// from MSB
#include
using namespace std;
  
// Returns a number which has all set bits
// starting from MSB of n
int setAllBitsAfterMSB(int n)
{    
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n>>1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n>>2;   
  
    n |= n>>4;  
    n |= n>>8;
    n |= n>>16;
    return n;
}
  
void toggle(int &n)
{
    n = n ^ setAllBitsAfterMSB(n);
}
  
// Driver code
int main()
{
    int n = 10;
    toggle(n);
    cout << n;
    return 0;
}

Java

// Java program to toggle set bits 
// starting from MSB
  
class GFG {
      
// Returns a number which has all 
// set bits starting from MSB of n
static int setAllBitsAfterMSB(int n) {
      
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
  
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    return n;
}
static int toggle(int n) 
{
    n = n ^ setAllBitsAfterMSB(n);
    return n;
}
  
// Driver code
public static void main(String arg[]) 
{
    int n = 10;
    n = toggle(n);
    System.out.print(n);
}
}
  
// This code is contributed by Anant Agarwal.

Python3

# Python program to toggle set  bits starting 
# from MSB
  
# Returns a number which has all set bits
# starting from MSB of n
def setAllBitsAfterMSB(n):
   
    # This makes sure two bits
    # (From MSB and including MSB)
    # are set
    n |= n>>1
   
    # This makes sure 4 bits
    # (From MSB and including MSB)
    # are set
    n |= n>>2   
   
    n |= n>>4  
    n |= n>>8
    n |= n>>16
    return n
  
def toggle(n):
  
    n = n ^ setAllBitsAfterMSB(n)
    return n
      
#Driver code
  
n = 10
n=toggle(n)
print(n)
# This code is contributed by Anant Agarwal.

C#

// C# program to toggle set bits 
// starting from MSB
using System;
  
class GFG {
          
    // Returns a number which has all 
    // set bits starting from MSB of n
    static int setAllBitsAfterMSB(int n)
    {
          
        // This makes sure two bits
        // (From MSB and including MSB)
        // are set
        n |= n >> 1;
      
        // This makes sure 4 bits
        // (From MSB and including MSB)
        // are set
        n |= n >> 2;
      
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
        return n;
    }
      
    static int toggle(int n) 
    {
        n = n ^ setAllBitsAfterMSB(n);
        return n;
    }
      
    // Driver code
    public static void Main() 
    {
        int n = 10;
        n = toggle(n);
        Console.WriteLine(n);
    }
}
  
// This code is contributed by Sam007.

的PHP

> 1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    $n |= $n >> 2; 
  
    $n |= $n >> 4; 
    $n |= $n >> 8;
    $n |= $n >> 16;
    return $n;
}
function toggle(&$n)
{
    $n = $n ^ setAllBitsAfterMSB($n);
}
  
// Driver Code
$n = 10;
toggle($n);
echo $n;
  
// This code is contributed by ajit
?>

输出 :

5

感谢Devanshu Agarwal提出了这种方法。