📜  从给定数字的LSB开始翻转连续的设置位

📅  最后修改于: 2021-05-06 09:10:20             🧑  作者: Mango

给定正整数N ,任务是找到可以通过以N的二进制表示形式从LSB开始翻转连续的设置位获得的数字。

例子:

天真的方法:最简单的方法是通过对N进行1的逻辑AND(&)运算直到N&1不为0并保持计数,从LSB开始查找连续的设置位数。 设置位的位数。然后,简单地将N移位设置位的数量即可。打印获得的号码作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number after
// converting 1s from end to 0s
int findNumber(int N)
{
    // Count of 1s
    int count = 0;
 
    // AND operation of N and 1
    while ((N & 1) == 1) {
        N = N >> 1;
        count++;
    }
 
    // Left shift N by count
    return N << count;
}
 
// Driver Code
int main()
{
    int N = 39;
 
    // Function Call
    cout << findNumber(N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
     
    // Count of 1s
    int count = 0;
 
    // AND operation of N and 1
    while ((N & 1) == 1)
    {
        N = N >> 1;
        count++;
    }
 
    // Left shift N by count
    return N << count;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 39;
     
    // Function Call
    System.out.println(findNumber(N));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the number after
# converting 1s from end to 0s
def findNumber(N):
     
    # Count of 1s
    count = 0
 
    # AND operation of N and 1
    while ((N & 1) == 1):
        N = N >> 1
        count += 1
 
    # Left shift N by count
    return N << count
 
# Driver Code
if __name__ == "__main__":
 
    N = 39
 
    # Function Call
    print(findNumber(N))
 
# This code is contributed by AnkThon


C#
// C# program to implement
// the above approach 
using System;
  
class GFG{
      
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
      
    // Count of 1s
    int count = 0;
  
    // AND operation of N and 1
    while ((N & 1) == 1)
    {
        N = N >> 1;
        count++;
    }
  
    // Left shift N by count
    return N << count;
}
 
// Driver Code
public static void Main()
{
    int N = 39;
      
    // Function Call
    Console.WriteLine(findNumber(N));
}
}
 
// This code is contributed by code_hunt


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number after
// converting 1s from end to 0s
int findNumber(int N)
{
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
int main()
{
    int N = 39;
 
    // Function Call
    cout << findNumber(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 39;
 
    // Function Call
    System.out.print(findNumber(N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the number after
# converting 1s from end to 0s
def findNumber(N):
     
    # Return the logical AND
    # of N and (N + 1)
    return N & (N + 1)
 
# Driver Code
if __name__ == '__main__':
    N = 39
 
    # Function Call
    print(findNumber(N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG
{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 39;
 
    // Function Call
    Console.Write(findNumber(N));
}
}
  
// This code is contributed by 29AjayKumar


输出:
32

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

高效方法:要优化上述方法,请找到N(N + 1)逻辑与(& ) 。关键的观察结果是,对数字加1会使LSB中的每个连续设置位变为0 。因此, N&(N + 1)给出所需的数字。

插图:

N = 39, therefore (N+1)=40
⇒   N = 39 = (100111)
⇒ N+1 = 40 = (101000)
Performing Logical AND(&) operation:
  1 0 0 1 1 1 
& 1 0 1 0 0 0
----------------
  1 0 0 0 0 0  ⇒ 32
----------------

Will this always work? Add 1 to N:
 1 0 0 1 1 1
 +         1
 ------------- 
 1 0 1 0 0 0    
 --------------     
It can be clearly seen that the continous set bits from the LSB becomnes unset.

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number after
// converting 1s from end to 0s
int findNumber(int N)
{
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
int main()
{
    int N = 39;
 
    // Function Call
    cout << findNumber(N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 39;
 
    // Function Call
    System.out.print(findNumber(N));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program for the above approach
 
# Function to find the number after
# converting 1s from end to 0s
def findNumber(N):
     
    # Return the logical AND
    # of N and (N + 1)
    return N & (N + 1)
 
# Driver Code
if __name__ == '__main__':
    N = 39
 
    # Function Call
    print(findNumber(N))
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
 
class GFG
{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 39;
 
    // Function Call
    Console.Write(findNumber(N));
}
}
  
// This code is contributed by 29AjayKumar
输出:
32

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