News for hackers. Stuff that matters.
Re:Faster Integer Parsing by tzs on Thursday June 25, 2020 @04:05PM (#23644982)
> It’s 16 characters long, and this could also apply to credit card numbers.

Credit card numbers can be up to 19 characters and as short as 12 characters. (I think they can theoretically be shorter but I'm not aware of any issuer that actually does that).

Also, they are strings over the alphabet of decimal digits. They are not intended to be interpreted as representing integers, and doing so is a bad idea. Leading 0s matter in credit card numbers. The first few digits identify the issuer. You could in theory have two card numbers where one is N digits without any leading zeros and the other is N+k digits with k leading zeros and the remaining N digits the same as the first card's. These would be different cards from different issuers but if you just parsed them into numbers would come out the same.

Re:Faster Integer Parsing by theandrewbailey on Thursday June 25, 2020 @04:42PM (#23645460)
Credit card numbers are a good example of the test 'do you need to do math or compare them?' If not, keep the numbers as strings.
Re:Faster Integer Parsing by bch on Thursday June 25, 2020 @06:10PM (#23646342)
Are the strings checksummed, though? In this case, you’d need to do math, but still keep them as strings...
Re:Faster Integer Parsing by PeterisP on Thursday June 25, 2020 @06:20PM (#23646427)
The checksum calculation does not involve interpreting the whole multidigit number as an integer, so you don't need to parse the number; you're doing math on specific characters/digits only.
Re:Faster Integer Parsing by boring_twenties on Thursday June 25, 2020 @08:20PM (#23647372)
Wouldn't it in fact be a lot slower using an integer? Lots of divisions by 10, instead of just adding every byte.
Re:Faster Integer Parsing by userbinator on Thursday June 25, 2020 @08:27PM (#23647424)
https://en.wikipedia.org/wiki/Luhn_algorithm

There's exactly one division required.

(Implementing it using vector instructions is left as an exercise for the reader. A very elementary exercise, so I encourage you to try it if you're curious about SSE and such.)

Re:Faster Integer Parsing by Arnavion on Thursday June 25, 2020 @11:49PM (#23648504)
Luhn's algorithm works on a digit string, not a single integer.
Re:Faster Integer Parsing by im3w1l-alt on Saturday June 27, 2020 @01:03PM (#23662998)
Checking divisibility by a known constant can be done in a better way than doing a division.

https://lemire.me/blog/2019/02/08/faster-remainders-when-the...