如何使用按位运算获取整数的第N位?

问题描述 投票:31回答:12

例。 123456,我们希望第三个从右边('4')出来。

实践中的想法是单独访问每个数字(即6 5 4 3 2 1)。

C / C ++ / C#首选。

numbers bit-manipulation
12个回答
38
投票

更有效的实现可能是这样的:

char nthdigit(int x, int n)
{
    while (n--) {
        x /= 10;
    }
    return (x % 10) + '0';
}

如果您只需要其中一个数字,这可以节省将所有数字转换为字符串格式的工作量。而且,您不必为转换后的字符串分配空间。

如果速度是一个问题,你可以预先计算10的幂的数组并使用n索引到这个数组:

char nthdigit(int x, int n)
{
    static int powersof10[] = {1, 10, 100, 1000, ...};
    return ((x / powersof10[n]) % 10) + '0';
}

正如其他人所提到的,这就像你要对基数10进行按位运算一样接近。


-1
投票
int returndigit(int n,int d)
{
    d=d-1;
    while(d--)
    {
        n/=10;
    }
    return (n%10);
}

-1
投票

两个数字d1和d2将通过。程序必须打印第n个数字是仅包含数字的数字系统d1和d2输入格式第一行包含d1第二行包含d2第三个kine包含n d1不等于d2


-2
投票

在C中你可以做类似下面的事情,其中​​n = 0表示最右边的数字

char nthDigitFromRight(int x,int n)
{
    char str[20];
    sprintf(str,"%020d",x);
    return(str[19 - x]);
}

如果您想要最右边的数字n = 1,请将[19-x]更改为[20-x]。


5
投票

刚刚花时间在这里根据答案写这个,所以我想分享。

这是基于Brannon的答案,但一次可以让你获得多个数字。在我的例子中,我使用它从保存在int中的日期和时间中提取部分,其中数字是yyyymmddhhnnssm_s格式。

public static int GetDigits(this int number, int highestDigit, int numDigits)
{
    return (number / (int)Math.Pow(10, highestDigit - numDigits)) % (int)Math.Pow(10, numDigits);
}

我把它作为一个扩展,你可能不想,但这里是样本用法:

int i = 20010607;
string year = i.GetDigits(8,4).ToString();
string month = i.GetDigits(4,2).ToString();
string day = i.GetDigits(2,2).ToString();

结果:

年= 2001年

月= 6

天= 7


4
投票

使用base-10数学:

class Program
{
    static void Main(string[] args)
    {
        int x = 123456;

        for (int i = 1; i <= 6; i++)
        {
            Console.WriteLine(GetDigit(x, i));
        }
    }

    static int GetDigit(int number, int digit)
    {
        return (number / (int)Math.Pow(10, digit - 1)) % 10;
    }
}

生产:

6
5
4
3
2
1

3
投票

使用逐位操作不能(轻松)工作的原因是十进制系统(10)的基数不是二进制系统(2)的基数的幂。

如果你在基数8编码,你就有pow(2, 3) == 8,并且可以将每个八进制数字提取为三位数的块。

所以你真的必须转换为基数10,这通常是通过转换为字符串(使用toString(Java)或sprintf(C)来完成的,正如其他人在回复中所示)。


2
投票

这适用于高达451069的无符号整数,正如here所解释的:

def hundreds_digit(u): return mod10(div100(u))

def div100(u): return div10(div10(u))
def mod10(u):  return u - mul10(div10(u))
def mul10(u):  return ((u << 2) + u) << 1

def div10(u):
    Q = ((u >> 1) + u) >> 1  # Q = u*0.11
    Q = ((Q >> 4) + Q)       # Q = u*0.110011
    Q = ((Q >> 8) + Q) >> 3  # Q = u*0.00011001100110011
    return Q

# Alternatively:
#   def div100(u): return (u * 0xa3d7) >> 22
# though that'd only work for 16-bit u values.
# Or you could construct shifts and adds along the lines of div10(),
# but I didn't go to the trouble.

测试出来:

>>> hundreds_digit(123456)
4
>>> hundreds_digit(123956)
9

不过,如果速度更快,我会感到惊讶。也许你应该重新考虑你的问题。


1
投票

value =(数字%(10 ^位置))/ 10 ^(位置-1)

例:

数字= 23846

position = 1 - > value = 6

position = 2 - > value = 4

position = 3 - > value = 8

这是一个简单的Objective-C实用方法来执行此操作:

+ (int)digitAtPosition:(int)pos of:(int)number {

    return (number % ((int)pow(10, pos))) / (int)pow(10, pos - 1);
}

0
投票

您可以尝试按位左移(对于N-1)然后读取[0]处的数字,因为这可能是汇编程序方法。

123456 - > 456 - >读取第一个数字


0
投票

以下代码将从数字右侧给出第n个数字:

public void getDigit(long n,int k){
    int i=0;
    long r =0;
    while(i<n){
        r=n%10;
        n=n/10;
        i++;
    }
    System.out.println( k + "th digit from right " + r);
 }

0
投票

只是为了好玩,这里是C#扩展类:

public static class IntExtensions
{
    /// <summary>
    /// Returns the nth digit from an int, 
    /// where 0 is the least significant digit 
    /// and n is the most significant digit.
    /// </summary>
    public static int GetDigit(this int number, int digit)
    {
        for (int i = 0; i < digit; i++)
        {
            number /= 10;
        }
        return number % 10;
    }
}

用法:

int myNumber = 12345;
int five = myNumber.GetDigit(0);
int four = myNumber.GetDigit(1);
int three = myNumber.GetDigit(2);
int two = myNumber.GetDigit(3);
int one = myNumber.GetDigit(4);
int zero = myNumber.GetDigit(5);
© www.soinside.com 2019 - 2024. All rights reserved.