• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does bit wise operator '&' does here ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does bit wise operator '&' does here ? addition, substraction, division, modulus or what ?
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does a bit-wise AND.

So, if no is (in binary) 10111001 (for instance), then i & 1 would be 10111001 & 00000001, which is 00000001, or 1. The last digit is the only one that is 1 in both numbers.

So what that's doing is testing if the last binary digit is 1 or not. In this case, it's equivalent to testing whether the number is even or not, and so you could do the equivalent as (no % 2 == 1).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic