Conversation
Notices
-
Embed this notice
@deprecated_ii Apologies for the long post.
=================================
What if it was a " i % 2 != 0 " inside the if statement? Would that have been ok? That way you'd only need one avaluation structure to fetch the odd numbers from the array.
Unless the library already has a built in function to do this like an "array.nOdd(i)" that could be assigned to a new array.
What would've been your answer to clean up this code? I don't know how to code, but I would like learn about this stuff from the opinion of people that do know.
-
Embed this notice
@greenshoots the fast way to check if an integer is odd is to look at the least significant bit and see if it's a 1 or a 0. an odd number is always going to have a 1 as the least significant bit. so "if(Array[i] & 1) { do whatever };"
for the sake of readability and clarity that check might be made in an "is_odd" function
there's nothing "wrong" with the mod 2 method but depending on the language and how often the check is happening it might be a significant performance problem. but that's the kind of thing you shouldn't worry about until you see the problem in a performance profiler
but yeah the core point is to reduce the check to a single operation that you can put any integer into
-
Embed this notice
@deprecated_ii @greenshoots any real compiler with optimizations will turn %2 into &1
-
Embed this notice
@Inginsub @greenshoots yeah
but then there's python