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

📅  最后修改于: 2021-09-07 02:09:02             🧑  作者: Mango

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

例子:

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

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

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

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

  1. 我们从最右侧找到设置位位置,即 LSB 和总位数。
  2. XOR 给定的数字与所有设置位的数字与总设置位等于总位数的数字进行异或。
  3. 我们不想从设置位翻转最右边的位。因此,我们再次将 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


Javascript


输出:
8

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live