Check integer for 0 on last digit
Sep 29, 2013 at 9:02pm UTC
Hi
Is there a way to check if an integer say 123456 has a 0 on the last digit using bitwise operators only and not string conversions?
1 2 3 4
123450 //This has a 0 for the last digit
123456 //This does not have a 0 for the last digit
I'm attempting a problem but the conversion from int to string is a huge bottleneck in a loop.
Thanks
Last edited on Sep 29, 2013 at 9:03pm UTC
Sep 29, 2013 at 9:14pm UTC
You could use bitwise operations to check for a binary zero, or even a hexadecimal or octal zero. But if it's everyday decimal digits you are interested in, then the modulus operator % is probably the simplest method.
Sep 29, 2013 at 9:17pm UTC
Wow I thought it required something complex , although I did think about the modulus operator i didn't see how it could do that. Thanks!
Topic archived. No new replies allowed.