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

后端 未结 12 1013
深忆病人
深忆病人 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 22:03

    Just for fun, here is the C# extension class for it:

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

    Usage:

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

    The reason that it won't work (easily) with bit-wise operations is that the base of the decimal system (10) is not a power of the base of the binary system (2).

    If you were coding in base 8, you'd have pow(2, 3) == 8, and could extract each octal digit as a block of three bits.

    So you really have to convert to base 10, which is usually done by converting to a string (with toString (Java) or sprintf (C), as the others have shown in their replies).

    0 讨论(0)
  • 2021-01-30 22:10

    two digit d1 and d2 will be passed .the program must print the nth Number is the number system that consist only digit with d1 and d2 input format first line contain d1 second line contain d2 third kine contain n d1 is not equal to d2

    0 讨论(0)
  • 2021-01-30 22:12

    Just spent time writing this based on answers here, so thought I would share.

    This is based on Brannon's answer, but lets you get more than one digit at a time. In my case I use it to extract parts from a date and time saved in an int where the digits are in yyyymmddhhnnssm_s format.

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

    I made it an extension, you might not want to, but here is sample usage:

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

    results:

    year = 2001

    month = 6

    day = 7

    0 讨论(0)
  • 2021-01-30 22:12

    In C you could do something like the following, where n=0 would indicate the rightmost digit

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

    Change [19-x] to [20-x] if you want n=1 for rightmost digit.

    0 讨论(0)
  • 2021-01-30 22:19

    You could try a bitwise shift-left (for N-1) and then read the digit at [0], as this could be an assembler approach.

    123456 -> 456 -> read first digit

    0 讨论(0)
提交回复
热议问题