How to get the Nth digit of an integer with bit-wise operations?

后端 未结 12 1016
深忆病人
深忆病人 2021-01-30 21:23

Example. 123456, and we want the third from the right (\'4\') out.

The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1).

C/C++/C# preferred.

相关标签:
12条回答
  • 2021-01-30 21:54

    A more efficient implementation might be something like this:

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

    This saves the effort of converting all digits to string format if you only want one of them. And, you don't have to allocate space for the converted string.

    If speed is a concern, you could precalculate an array of powers of 10 and use n to index into this array:

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

    As mentioned by others, this is as close as you are going to get to bitwise operations for base 10.

    0 讨论(0)
  • 2021-01-30 21:55

    value = (number % (10^position)) / 10^(position - 1)

    Example:

    number = 23846

    position = 1 -> value = 6

    position = 2 -> value = 4

    position = 3 -> value = 8

    Here is a simple Objective-C utility method to do this:

    + (int)digitAtPosition:(int)pos of:(int)number {
    
        return (number % ((int)pow(10, pos))) / (int)pow(10, pos - 1);
    }
    
    0 讨论(0)
  • 2021-01-30 21:56
    int returndigit(int n,int d)
    {
        d=d-1;
        while(d--)
        {
            n/=10;
        }
        return (n%10);
    }
    
    0 讨论(0)
  • 2021-01-30 21:57

    This works for unsigned ints up to 451069, as explained 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.
    

    Testing it out:

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

    I'd be surprised if it's faster, though. Maybe you should reconsider your problem.

    0 讨论(0)
  • 2021-01-30 21:57

    Following code will give nth digit from right in a number:

    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 讨论(0)
  • 2021-01-30 22:03

    Use base-10 math:

    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;
        }
    }
    

    Produces:

    6
    5
    4
    3
    2
    1
    
    0 讨论(0)
提交回复
热议问题