📌  相关文章
📜  通过将所有位翻转到最右边的设置位的左侧而形成的数字

📅  最后修改于: 2021-05-06 07:33:34             🧑  作者: Mango

给定整数N ,任务是将所有位翻转到最右边的设置位的左侧,并打印生成的数字。
例子:

天真的方法:
为了解决上述问题,我们将按照以下步骤操作:

  • 将给定的整数转换为二进制形式,并将每个位存储到数组中。
  • 遍历数组并在第一次出现1之后中断。
  • 将所有位翻转到该索引的左侧。将该二进制序列转换为十进制数字,然后将其返回。

时间复杂度: O(N)
辅助空间: O(N)

高效方法:
为了优化上述方法,我们将尝试使用按位运算符

  1. 我们从最右边找到设置的位位置,即LSB和总位数。
  2. 将给定数字与所有置位位数等于总位数的数字进行异或。
  3. 我们不想从设置的位翻转最右边的位。因此,我们再次对所有设置位的总设置位等于firstbit的数字进行XOR运算。

下面是上述方法的实现:

C++
// C++ program to find the 
// integer formed after flipping 
// all bits to the left of the 
// rightmost set bit
#include 
using namespace std;
  
int totCount; 
int firstCount; 
  
// Function to get the total count 
void getTotCount(int num) 
{ 
    totCount = 1; 
    firstCount = 1; 
    int temp = 1; 
      
    // Moving until we get 
    // the rightmost set bit 
    while ((num & temp) == 0) 
    { 
        temp = temp << 1; 
        totCount += 1; 
    } 
      
    firstCount = totCount; 
    temp = num >> totCount; 
      
    // To get total number 
    // of bits in a number 
    while (temp != 0) 
    { 
        totCount += 1; 
        temp = temp >> 1; 
    } 
} 
      
// Function to find the integer formed 
// after flipping all bits to the left 
// of the rightmost set bit 
int flipBitsFromRightMostSetBit(int num) 
{ 
      
    // Find the total count of bits and 
    // the rightmost set bit 
    getTotCount(num); 
      
    // XOR given number with the 
    // number which has is made up 
    // of only totbits set 
    int num1 = num ^ ((1 << totCount) - 1); 
      
    // To avoid flipping the bits 
    // to the right of the set bit, 
    // take XOR with the number 
    // made up of only set firstbits 
    num1 = num1 ^ ((1 << firstCount) - 1); 
      
    return num1; 
} 
  
// Driver Code
int main()
{
    int n = 120; 
      
    cout << flipBitsFromRightMostSetBit(n)
         << endl;
  
    return 0;
}
  
// This code is contributed by divyeshrabadiya07


Java
// Java program to find the 
// integer formed after flipping 
// all bits to the left of the 
// rightmost set bit 
import java.util.*; 
  
class GFG{ 
      
static int totCount; 
static int firstCount; 
  
// Function to get the total count 
static void getTotCount(int num) 
{ 
    totCount = 1; 
    firstCount = 1; 
    int temp = 1; 
      
    // Moving until we get 
    // the rightmost set bit 
    while ((num & temp) == 0) 
    { 
        temp = temp << 1; 
        totCount += 1; 
    } 
      
    firstCount = totCount; 
    temp = num >> totCount; 
      
    // To get total number 
    // of bits in a number 
    while (temp != 0) 
    { 
        totCount += 1; 
        temp = temp >> 1; 
    } 
} 
      
// Function to find the integer formed 
// after flipping all bits to the left 
// of the rightmost set bit 
static int flipBitsFromRightMostSetBit(int num) 
{ 
      
    // Find the total count of bits and 
    // the rightmost set bit 
    getTotCount(num); 
      
    // XOR given number with the 
    // number which has is made up 
    // of only totbits set 
    int num1 = num ^ ((1 << totCount) - 1); 
      
    // To avoid flipping the bits 
    // to the right of the set bit, 
    // take XOR with the number 
    // made up of only set firstbits 
    num1 = num1 ^ ((1 << firstCount) - 1); 
      
    return num1; 
} 
          
// Driver Code 
public static void main (String[] args) 
{ 
    int n = 120; 
      
    System.out.println( 
        flipBitsFromRightMostSetBit(n)); 
} 
} 
  
// This code is contributed by offbeat


Python3
# Python3 program to find the 
# integer formed after flipping 
# all bits to the left of the 
# rightmost set bit 
  
# Function to get the total count 
def getTotCount(num): 
    totCount = 1
    firstCount = 1
      
    temp = 1
      
    # Moving until we get 
    # the rightmost set bit 
    while (not(num & temp)): 
        temp = temp << 1
        totCount += 1
    firstCount = totCount 
      
    temp = num >> totCount 
      
    # To get total number 
    # of bits in a number 
    while (temp): 
        totCount += 1
        temp = temp >> 1
      
    return totCount, firstCount 
      
      
# Function to find the integer formed 
# after flipping all bits to the left 
# of the rightmost set bit 
def flipBitsFromRightMostSetBit(num): 
      
    # Find the total count of bits and 
    # the rightmost set bit 
    totbit, firstbit = getTotCount(num) 
      
      
    # XOR given number with the 
    # number which has is made up 
    # of only totbits set 
      
    num1 = num ^ ((1 << totbit) - 1) 
      
    # To avoid flipping the bits 
    # to the right of the set bit, 
    # take XOR with the number 
    # made up of only set firstbits 
      
    num1 = num1 ^ ((1 << firstbit) - 1) 
      
    return num1 
      
  
if __name__=='__main__': 
    n = 120
    print(flipBitsFromRightMostSetBit(n))


C#
// C# program to find the 
// integer formed after flipping 
// all bits to the left of the 
// rightmost set bit 
using System;
  
class GFG{
      
static int totCount;
static int firstCount;
  
// Function to get the total count 
static void getTotCount(int num)
{
    totCount = 1;
    firstCount = 1;
    int temp = 1;
      
    // Moving until we get 
    // the rightmost set bit 
    while ((num & temp) == 0) 
    { 
        temp = temp << 1; 
        totCount += 1; 
    }
      
    firstCount = totCount; 
    temp = num >> totCount; 
      
    // To get total number 
    // of bits in a number 
    while (temp != 0)
    { 
        totCount += 1;
        temp = temp >> 1;
    }
}
      
// Function to find the integer formed 
// after flipping all bits to the left 
// of the rightmost set bit 
static int flipBitsFromRightMostSetBit(int num)
{ 
      
    // Find the total count of bits and 
    // the rightmost set bit 
    getTotCount(num);
      
    // XOR given number with the 
    // number which has is made up 
    // of only totbits set 
    int num1 = num ^ ((1 << totCount) - 1);
      
    // To avoid flipping the bits 
    // to the right of the set bit, 
    // take XOR with the number 
    // made up of only set firstbits 
    num1 = num1 ^ ((1 << firstCount) - 1);
      
    return num1;
}
          
// Driver Code
public static void Main (string[] args)
{
    int n = 120;
      
    Console.Write(
        flipBitsFromRightMostSetBit(n));
}
}
  
// This code is contributed by rutvik_56


输出:
8

时间复杂度: O(N)
辅助空间: O(1)