📜  最右置位的位置

📅  最后修改于: 2021-05-25 01:07:59             🧑  作者: Mango

编写一个单行函数,以整数的二进制表示形式从右到左返回第一个1的位置。

I/P    18,   Binary Representation 010010
O/P   2
I/P    19,   Binary Representation 010011
O/P   1

算法:(示例12(1100))
令I / P为12(1100)
1.取给定数字的二进制补码,因为所有位都被还原
从右到左的第一个’1’除外(0100)
2进行一点与原始的否,这将返回与否
只需要一个(0100)
3取no的log2,您将得到(位置– 1)(2)
4加1(3)

解释 –

(n&〜(n-1))始终返回包含最右边的设置位为1的二进制数。

如果N = 12(1100),则将返回4(100)

在这里log2将返回您,我们可以用2的幂表示该数字的次数。

对于所有包含最右边的置1的二进制数,如2,4,8,16,32…。

我们会发现最右边设置位的位置始终等于log2(Number)+1

程序:

C++
// C++ program for Position
// of rightmost set bit
#include 
#include 
using namespace std;
 
class gfg
{
 
public:
unsigned int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
};
 
// Driver code
int main()
{
    gfg g;
    int n = 12;
    cout << g.getFirstSetBitPos(n);
    return 0;
}
 
// This code is contributed by SoM15242


C
// C program for Position
// of rightmost set bit
#include 
#include 
 
unsigned int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// Driver code
int main()
{
    int n = 12;
    printf("%u", getFirstSetBitPos(n));
    getchar();
    return 0;
}


Java
// Java Code for Position of rightmost set bit
class GFG {
 
    public static int getFirstSetBitPos(int n)
    {
        return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1;
    }
 
    // Drive code
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(getFirstSetBitPos(n));
    }
}
// This code is contributed by Arnav Kr. Mandal


Python3
# Python Code for Position
# of rightmost set bit
 
import math
 
def getFirstSetBitPos(n):
 
     return math.log2(n&-n)+1
 
# driver code
 
n = 12
print(int(getFirstSetBitPos(n)))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# Code for Position of rightmost set bit
using System;
 
class GFG {
    public static int getFirstSetBitPos(int n)
    {
        return (int)((Math.Log10(n & -n))
                / Math.Log10(2)) + 1;
    }
 
    // Driver method
    public static void Main()
    {
        int n = 12;
        Console.WriteLine(getFirstSetBitPos(n));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


C++
// C++ program to find the
// position of first rightmost
// set bit in a given number.
#include 
using namespace std;
 
// Function to find rightmost
// set bit in given number.
int getFirstSetBitPos(int n)
{
    return ffs(n);
}
 
// Driver function
int main()
{
    int n = 12;
    cout << getFirstSetBitPos(n) << endl;
    return 0;
}


C++
// C++ program to find the first
// rightmost set bit using XOR operator
#include 
using namespace std;
 
// function to find the rightmost set bit
int PositionRightmostSetbit(int n)
{
    // Position variable initialize with 1
    // m variable is used to check the set bit
    int position = 1;
    int m = 1;
 
    while (!(n & m)) {
 
        // left shift
        m = m << 1;
        position++;
    }
    return position;
}
// Driver Code
int main()
{
    int n = 16;
    // function call
    cout << PositionRightmostSetbit(n);
    return 0;
}


Java
// Java program to find the
// first rightmost set bit
// using XOR operator
 
class GFG {
 
    // function to find
    // the rightmost set bit
    static int PositionRightmostSetbit(int n)
    {
        // Position variable initialize
        // with 1 m variable is used to
        // check the set bit
        int position = 1;
        int m = 1;
 
        while ((n & m) == 0) {
 
            // left shift
            m = m << 1;
            position++;
        }
        return position;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 16;
 
        // function call
        System.out.println(PositionRightmostSetbit(n));
    }
}
 
// This code is contributed
// by Smitha


Python3
# Python3 program to find
# the first rightmost set
# bit using XOR operator
 
# function to find the
# rightmost set bit
def PositionRightmostSetbit(n):
 
    # Position variable initialize
    # with 1 m variable is used
    # to check the set bit
    position = 1
    m = 1
 
    while (not(n & m)) :
 
        # left shift
        m = m << 1
        position += 1
     
    return position
 
# Driver Code
n = 16
 
# function call
print(PositionRightmostSetbit(n))
 
# This code is contributed
# by Smitha


C#
// C# program to find the
// first rightmost set bit
// using XOR operator
using System;
 
class GFG {
 
    // function to find
    // the rightmost set bit
    static int PositionRightmostSetbit(int n)
    {
        // Position variable initialize
        // with 1 m variable is used to
        // check the set bit
        int position = 1;
        int m = 1;
 
        while ((n & m) == 0) {
 
            // left shift
            m = m << 1;
            position++;
        }
        return position;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 16;
 
        // function call
        Console.WriteLine(
            PositionRightmostSetbit(n));
    }
}
 
// This code is contributed
// by @ajit


PHP


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
#define INT_SIZE 32
 
int Right_most_setbit(int num)
{
    int pos = 1;
    // counting the position of first set bit
    for (int i = 0; i < INT_SIZE; i++) {
        if (!(num & (1 << i)))
            pos++;
        else
            break;
    }
    return pos;
}
int main()
{
    int num = 18;
    int pos = Right_most_setbit(num);
    cout << pos << endl;
    return 0;
}
// This approach has been contributed by @vivek kumar9


Java
//Java implementation of above approach
public class GFG {
     
    static int INT_SIZE = 32;
 
    static int Right_most_setbit(int num)
    {
        int pos = 1;
        // counting the position of first set bit
        for (int i = 0; i < INT_SIZE; i++) {
            if ((num & (1 << i))== 0)
                pos++;
             
            else
                break;
        }
        return pos;
    }
     
    //Driver code
    public static void main(String[] args) {
     
         int num = 18;
            int pos = Right_most_setbit(num);
            System.out.println(pos);
    }
}


Python3
# Python 3 implementation of above approach
 
INT_SIZE = 32
 
def Right_most_setbit(num) :
 
    pos = 1
 
    # counting the position of first set bit
    for i in range(INT_SIZE) :
        if not(num & (1 << i)) :
            pos += 1
        else :
            break
         
    return pos
     
 
 
if __name__ == "__main__" :
 
    num = 18
    pos = Right_most_setbit(num)
    print(pos)
     
# This code is contributed by ANKITRAI1


C#
// C# implementation of above approach
using System;
 
class GFG {
     
    static int INT_SIZE = 32;
 
    static int Right_most_setbit(int num)
    {
        int pos = 1;
         
        // counting the position
        // of first set bit
        for (int i = 0; i < INT_SIZE; i++)
        {
            if ((num & (1 << i))== 0)
                pos++;
             
            else
                break;
        }
        return pos;
    }
     
    // Driver code
    static public void Main ()
    {
        int num = 18;
        int pos = Right_most_setbit(num);
        Console.WriteLine(pos);
    }
}


PHP


Javascript


C++
// C++ program for above approach
#include
using namespace std;
 
// Program to find position of
// rightmost set bit
int PositionRightmostSetbit(int n)
{
  int p=1;
   
  // Iterate till number>0
  while(n > 0)
  {
     
    // Checking if last bit is set
    if(n&1){
      return p;
    }
     
    // Increment position and right shift number
    p++;
    n=n>>1;
  }
   
  // set bit not found.
  return -1;
}
 
// Driver Code
int main()
{
  int n=18;
   
  // Function call
  int pos=Last_set_bit(n);
 
  if(pos!=-1)
    cout<


Java
// Java program for above approach
import java.io.*;
 
class GFG{
     
// Function to find position of
// rightmost set bit
public static int Last_set_bit(int n)
{
    int p = 1;
 
    // Iterate till number>0
    while (n > 0)
    {
         
        // Checking if last bit is set
        if ((n & 1) > 0)
        {
            return p;
        }
 
        // Increment position and
        // right shift number
        p++;
        n = n >> 1;
    }
 
    // set bit not found.
    return -1;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 18;
 
    // Function call
    int pos = Last_set_bit(n);
 
    if (pos != -1)
        System.out.println(pos);
    else
        System.out.println("0");
}
}
 
// This code is contributed by RohitOberoi


Python3
# Python program for above approach
 
# Program to find position of
# rightmost set bit
def PositionRightmostSetbit( n):
  p = 1
   
  # Iterate till number>0
  while(n > 0):
     
    # Checking if last bit is set
    if(n&1):
      return p
     
    # Increment position and right shift number
    p += 1
    n = n>>1
   
  # set bit not found.
  return -1;
 
# Driver Code
n = 18
# Function call
pos = PositionRightmostSetbit(n)
if(pos != -1):
  print(pos)
else:
  print(0)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for above approach
using System;
 
class GFG{
     
// Function to find position of
// rightmost set bit
public static int Last_set_bit(int n)
{
    int p = 1;
 
    // Iterate till number>0
    while (n > 0)
    {
         
        // Checking if last bit is set
        if ((n & 1) > 0)
        {
            return p;
        }
 
        // Increment position and
        // right shift number
        p++;
        n = n >> 1;
    }
 
    // Set bit not found.
    return -1;
}
 
// Driver Code
public static void Main(string[] args)
{
    int n = 18;
 
    // Function call
    int pos = Last_set_bit(n);
 
    if (pos != -1)
        Console.WriteLine(pos);
    else
        Console.WriteLine("0");
}
}
 
// This code is contributed by AnkThon


Javascript


输出
3

使用ffs()函数: ffs()函数返回第一个最低有效位的索引。索引从ffs()函数从1开始。
例如:
n = 12 = 1100

在上面的示例中,ffs(n)返回最右边的设置位索引3。

C++

// C++ program to find the
// position of first rightmost
// set bit in a given number.
#include 
using namespace std;
 
// Function to find rightmost
// set bit in given number.
int getFirstSetBitPos(int n)
{
    return ffs(n);
}
 
// Driver function
int main()
{
    int n = 12;
    cout << getFirstSetBitPos(n) << endl;
    return 0;
}
输出

3

使用XOR和&运算符:
将m初始化为1,以从最右边的位开始的位检查其XOR。将m左移一个,直到找到第一个设置位,因为当我们对m执行&运算时,第一个设置位给出一个数字。

C++

// C++ program to find the first
// rightmost set bit using XOR operator
#include 
using namespace std;
 
// function to find the rightmost set bit
int PositionRightmostSetbit(int n)
{
    // Position variable initialize with 1
    // m variable is used to check the set bit
    int position = 1;
    int m = 1;
 
    while (!(n & m)) {
 
        // left shift
        m = m << 1;
        position++;
    }
    return position;
}
// Driver Code
int main()
{
    int n = 16;
    // function call
    cout << PositionRightmostSetbit(n);
    return 0;
}

Java

// Java program to find the
// first rightmost set bit
// using XOR operator
 
class GFG {
 
    // function to find
    // the rightmost set bit
    static int PositionRightmostSetbit(int n)
    {
        // Position variable initialize
        // with 1 m variable is used to
        // check the set bit
        int position = 1;
        int m = 1;
 
        while ((n & m) == 0) {
 
            // left shift
            m = m << 1;
            position++;
        }
        return position;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 16;
 
        // function call
        System.out.println(PositionRightmostSetbit(n));
    }
}
 
// This code is contributed
// by Smitha

Python3

# Python3 program to find
# the first rightmost set
# bit using XOR operator
 
# function to find the
# rightmost set bit
def PositionRightmostSetbit(n):
 
    # Position variable initialize
    # with 1 m variable is used
    # to check the set bit
    position = 1
    m = 1
 
    while (not(n & m)) :
 
        # left shift
        m = m << 1
        position += 1
     
    return position
 
# Driver Code
n = 16
 
# function call
print(PositionRightmostSetbit(n))
 
# This code is contributed
# by Smitha

C#

// C# program to find the
// first rightmost set bit
// using XOR operator
using System;
 
class GFG {
 
    // function to find
    // the rightmost set bit
    static int PositionRightmostSetbit(int n)
    {
        // Position variable initialize
        // with 1 m variable is used to
        // check the set bit
        int position = 1;
        int m = 1;
 
        while ((n & m) == 0) {
 
            // left shift
            m = m << 1;
            position++;
        }
        return position;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 16;
 
        // function call
        Console.WriteLine(
            PositionRightmostSetbit(n));
    }
}
 
// This code is contributed
// by @ajit

的PHP


Java脚本


输出
5

这种方法是由mubashshir ahmad贡献的

使用左移(<<):用1初始化pos,最多迭代INT_SIZE(Here 32)并检查是否设置了位,如果设置了位则中断循环,否则增加pos。

C++

// C++ implementation of above approach
#include 
using namespace std;
#define INT_SIZE 32
 
int Right_most_setbit(int num)
{
    int pos = 1;
    // counting the position of first set bit
    for (int i = 0; i < INT_SIZE; i++) {
        if (!(num & (1 << i)))
            pos++;
        else
            break;
    }
    return pos;
}
int main()
{
    int num = 18;
    int pos = Right_most_setbit(num);
    cout << pos << endl;
    return 0;
}
// This approach has been contributed by @vivek kumar9

Java

//Java implementation of above approach
public class GFG {
     
    static int INT_SIZE = 32;
 
    static int Right_most_setbit(int num)
    {
        int pos = 1;
        // counting the position of first set bit
        for (int i = 0; i < INT_SIZE; i++) {
            if ((num & (1 << i))== 0)
                pos++;
             
            else
                break;
        }
        return pos;
    }
     
    //Driver code
    public static void main(String[] args) {
     
         int num = 18;
            int pos = Right_most_setbit(num);
            System.out.println(pos);
    }
}  

Python3

# Python 3 implementation of above approach
 
INT_SIZE = 32
 
def Right_most_setbit(num) :
 
    pos = 1
 
    # counting the position of first set bit
    for i in range(INT_SIZE) :
        if not(num & (1 << i)) :
            pos += 1
        else :
            break
         
    return pos
     
 
 
if __name__ == "__main__" :
 
    num = 18
    pos = Right_most_setbit(num)
    print(pos)
     
# This code is contributed by ANKITRAI1

C#

// C# implementation of above approach
using System;
 
class GFG {
     
    static int INT_SIZE = 32;
 
    static int Right_most_setbit(int num)
    {
        int pos = 1;
         
        // counting the position
        // of first set bit
        for (int i = 0; i < INT_SIZE; i++)
        {
            if ((num & (1 << i))== 0)
                pos++;
             
            else
                break;
        }
        return pos;
    }
     
    // Driver code
    static public void Main ()
    {
        int num = 18;
        int pos = Right_most_setbit(num);
        Console.WriteLine(pos);
    }
}

的PHP


Java脚本


输出 :

2

另一种使用右Shift(>>)的方法

初始化pos = 1。重复执行直到数字> 0,然后在每个步骤中检查是否设置了最后一位。如果设置了最后一位,则返回当前位置,否则将pos加1,向右移n 1。

C++

// C++ program for above approach
#include
using namespace std;
 
// Program to find position of
// rightmost set bit
int PositionRightmostSetbit(int n)
{
  int p=1;
   
  // Iterate till number>0
  while(n > 0)
  {
     
    // Checking if last bit is set
    if(n&1){
      return p;
    }
     
    // Increment position and right shift number
    p++;
    n=n>>1;
  }
   
  // set bit not found.
  return -1;
}
 
// Driver Code
int main()
{
  int n=18;
   
  // Function call
  int pos=Last_set_bit(n);
 
  if(pos!=-1)
    cout<

Java

// Java program for above approach
import java.io.*;
 
class GFG{
     
// Function to find position of
// rightmost set bit
public static int Last_set_bit(int n)
{
    int p = 1;
 
    // Iterate till number>0
    while (n > 0)
    {
         
        // Checking if last bit is set
        if ((n & 1) > 0)
        {
            return p;
        }
 
        // Increment position and
        // right shift number
        p++;
        n = n >> 1;
    }
 
    // set bit not found.
    return -1;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 18;
 
    // Function call
    int pos = Last_set_bit(n);
 
    if (pos != -1)
        System.out.println(pos);
    else
        System.out.println("0");
}
}
 
// This code is contributed by RohitOberoi

Python3

# Python program for above approach
 
# Program to find position of
# rightmost set bit
def PositionRightmostSetbit( n):
  p = 1
   
  # Iterate till number>0
  while(n > 0):
     
    # Checking if last bit is set
    if(n&1):
      return p
     
    # Increment position and right shift number
    p += 1
    n = n>>1
   
  # set bit not found.
  return -1;
 
# Driver Code
n = 18
# Function call
pos = PositionRightmostSetbit(n)
if(pos != -1):
  print(pos)
else:
  print(0)
 
# This code is contributed by rohitsingh07052.

C#

// C# program for above approach
using System;
 
class GFG{
     
// Function to find position of
// rightmost set bit
public static int Last_set_bit(int n)
{
    int p = 1;
 
    // Iterate till number>0
    while (n > 0)
    {
         
        // Checking if last bit is set
        if ((n & 1) > 0)
        {
            return p;
        }
 
        // Increment position and
        // right shift number
        p++;
        n = n >> 1;
    }
 
    // Set bit not found.
    return -1;
}
 
// Driver Code
public static void Main(string[] args)
{
    int n = 18;
 
    // Function call
    int pos = Last_set_bit(n);
 
    if (pos != -1)
        Console.WriteLine(pos);
    else
        Console.WriteLine("0");
}
}
 
// This code is contributed by AnkThon

Java脚本


输出
2