You're viewing a single thread.
modulo
pseudocode:
if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is odd" (is_num_even = 0 or false)
plus you'd want an input validation beforehand
12 3 Replywho needs modulo when you can get less characters out of
while (number > 1) { number -= 2; } return number;
very efficient
edit: or theres the trusty iseven api
23 0 Replyhere is somewhat less:
return (number % 2) == 0;
9 0 Replyreturn !(number & 1);
10 0 ReplyThis is the way. Modulo takes too long to compute, bitwise compare should be a lot faster.
return !(number & 0x1);
8 0 Replyoh shit yo
this comment chain is pretty awesome, I learned a lot from this thanks!
5 0 Reply
just check the last bit jesus christ, what is it with these expensive modulo operations?!
return !(n&1);
4 0 Reply
are the negative numbers all even?
2 0 ReplyYes
2 0 Reply
🤦
8 1 Reply#You are an input. You have value! You matter! if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is odd" (is_num_even = 0 or false)
Am I doing it right? /S.
4 0 ReplyDon't put nbsps in code blocks, they show up literally.
5 0 Reply
Name doesn't check out.
3 0 ReplyThis code is terrible. If you input 10.66 it returns "number is odd
It should be:
if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is not even" (is_num_even = 0 or false)
2 0 ReplyJohn carmak posting
2 0 Replyare u a wizard?
2 0 Reply