# How do I compare these ranges of numbers efficiently?

I'm looking for an efficient way of testing eights. What happens is I need to check if a value is eights and discard it.

The numbers I need to check for are:

8.8888, 88.888, 888.88, 8888.8, 88888.00                // 5 digits
88.8888, 888.888, 8888.88, 88888.8, 888888.00           // 6 digits
888.8888, 8888.888, 88888.88, 888888.8, 8888888.00      // 7 digits
8888.8888, 88888.888, 888888.88, 8888888.8, 88888888.00 // 8 digits


However, these are actually represented in integer form. Which is the number multiplied by 10000.

So 8.8888 is represented as an int64 with the value 88888, and 888888.00 is represented as 888880000

There are quite a few values here to check. My simple approach was to just compare each one directly from a table. But then I thought perhaps I should maybe do something more efficient like masking and comparing each digit. But my crude approach did not work. It seems cumbersome and potentially a bit slow to convert to a string and compare eights that way. This code will run on an embedded system which checks these values many times over so I would like it to be reasonably performant and easy to understand.

Note that I won't have less than 5 digits represented or more than 8 really.

This is what I am currently doing. Not sure if it's the best approach but it works.

bool TestEights(__int64 rt)
{
char rts[20];
bool eights = true;

sprintf_s(rts, sizeof(rts), "%llu", rt);

int len = strlen(rts) - 1;
int dec = len - 5;
for (int i = len; i > 0; i--) {
if (i >= dec) {
eights &= rts[i] == '0' || rts[i] == '8';
}
else {
eights &= rts[i] == '8';
}

}

return eights;
}

• Can you expand on what you mean by "embedded system"? A digital thermometer and an MRI scanner are both embedded systems, but they have very different performance characteristics. – Pseudonym Apr 23 '20 at 1:24
• @Pseudonym - I can't tell you what it is unfortunately. Performance would be similar to an arduino – Matt Apr 23 '20 at 1:52
• Performance isn't really the main thing. I just like nice and easy to follow code that minimises unnecessary steps. It does seem a bit clunky to convert it to a string but it does work. – Matt Apr 23 '20 at 2:07
• You're looking for one 64-bit number out of a set of 20. The AVR has a shallow pipeline with in-order scheduling. This means there's no branch misprediction penalty. So plain old binary search (even implemented as a tree of if statements) is a realistic option. But you should test it against linear search. – Pseudonym Apr 23 '20 at 2:12
• 888888.00 is represented as 888880000 computers excel in counting. – greybeard Apr 23 '20 at 4:43