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

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

提交回复
热议问题