简体   繁体   中英

C# Check specific digit in integer if it is only as 1 or 0

I have int number of the type yyyy000000 y can be 1 or 0 if the first y is 0 th len is 9 instead of 10

for example:

1111000000 111000000 0 1010000000

it is type of int..

what is the best way to check if the secound digit is 1 or the first?

every one of the first four mean something

It is posible to do something like 1111000000.ToString()[1] == '1'

but is will take long time to cast of to sting if I run on alot of data.. there is any faster way to do it?

but is will take long time to cast of to sting if I run on alot of data.. there is any faster way to do it?

These kinds of questions need to be answered with benchmarks. Otherwise you work on optimizing things that are not the problem, since you don't actually know what the problem is.

In any case, maybe you can save a few ns here, which means the amount of time you can save is probably throttled by disk i/o instead of CPU time, because you say "it will take too long", which only makes sense if you are trying to parse some terabytes of integers (at ns per parse)...

Quick benchmark.net test results

|         Method |       Mean |     Error |    StdDev |
|--------------- |-----------:|----------:|----------:|
| ToStringMethod | 18.5890 ns | 0.3854 ns | 0.4587 ns |
|  ConvertMethod | 12.8943 ns | 0.0984 ns | 0.0821 ns |
| SubtractMethod |  0.2576 ns | 0.0288 ns | 0.0283 ns |

Explanation : A simple subtract/compare is the solution you want, as outlined in the other answer.


Example benchmark code, this is just "close enough" for testing purposes, but the code doesn't solve your problem.

public class WorkTest
{
    private readonly int _data;

    public WorkTest()
    {
        _data = new Random().Next(1000000, 1111000000);
    }

    [Benchmark]
    public bool ToStringMethod() => _data.ToString()[1] == '1';

    [Benchmark]
    public bool ConvertMethod() => (ConvertBase10As2(_data) & 0x10000000) > 0;

    [Benchmark]
    public bool SubtractMethod() => _data > 111000000 ? (_data - 1000000000 > 0) : (_data - 100000000 > 0);

    private int ConvertBase10As2(int b10)
    {
        int result = 0;
        int shift = 0;

        while (b10 > 0)
        {
            result |= (b10 & 1) << shift;
            b10 /= 10;
            shift++;
        }

        return result;
    }
}

If I understand you question correctly, you can either have a 10 digit int or a 9 digit int, depending on whether the first y is a 1 or a 0.

If that is the case you basically have these two options:

  • 1yyy000000
  • 1yy000000 (the first y is 0, but int can't have a preceeding 0, so we end up with 9 digits instead of 10.

To figure out if the first digit is 1 or 0, you could check whether the value is less than 1000000000:

if (x < 1000000000) {
   // First digit is 0
} else {
   // First digit is 1
}

The easiest way to get the digits of a number is to use ToString() . But the optimal solution is to divide the number...

//get first digit
int i=123456789;
while (i >= 10)
     i /= 10;
Console.WriteLine(i);

//get second digit
int i1=123456789;
while (i1 >= 100)
     i1 /= 10;
Console.WriteLine(i1%10);

//third digit
int i2=123456789;
while (i2 >= 1000)
     i2 /= 10;
Console.WriteLine(i2% 10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM