• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java question  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I just want to make sure that the following codes below are both correct. Please correct me if I am wrong and explain. Thanks!


 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi junaid.
yes they both are correct.are you familiar with bitwise and(&),if yes you can use it here to do the same task and it is even faster in regard of performance as compared to the prior 2 implementations you have done.
try to implement it by your own.
if you are not familiar with it,leave it and give it a try after you learn about bitwise operators.

kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In terms of style, the second form is much better.
There is a third form which doesn't use the % operator, and I challenge you to find it. PK has already given you a big hint.
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us how would you check if number is odd?
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When checking for even or odd use the & symbol, not he % sign
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!