Skip to content

Instantly share code, notes, and snippets.

@burakozhan
Created July 25, 2018 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burakozhan/c57644ad34bd0960376e158f3132f492 to your computer and use it in GitHub Desktop.
Save burakozhan/c57644ad34bd0960376e158f3132f492 to your computer and use it in GitHub Desktop.
Side effects of bitwiseAND vs logicalAND
import org.junit.Test;
public class BooleanTest {
private static boolean D = true;
@Test
public void bitwiseAND() {
int finds = 0;
int ctr = 0;
for (int i = 0; i < 100; i++){
if (i % 7 == 0 & ctr++ % 7 == 0){
if(D) System.out.printf("i = %d , ctr = %d", i, ctr);
if(D) System.out.print("\nfound one");
finds++;
if(D) System.out.println();
}
}
System.out.printf("\nFound total items %d", finds);
System.out.printf("\nFinal counter position %d", ctr);
System.out.print("\n\n");
}
@Test
public void logicalAND() {
int finds = 0;
int ctr = 0;
for (int i = 0; i < 100; i++){
if (i % 2 == 0 && ctr++ % 7 == 0){
if(D) System.out.printf("i = %d , ctr = %d", i, ctr);
if(D) System.out.print("\nfound one !");
finds++;
if(D) System.out.println();
}
}
System.out.printf("\nFound total items %d", finds);
System.out.printf("\nFinal counter position %d", ctr);
System.out.print("\n\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment