Skip to content

Commit 655c545

Browse files
sumit-bhawsarsjmillington
authored andcommitted
Bael 3512-difference between logical and bitwise & (eugenp#8203)
* BAEL-3512 unit tests for Bitwise and Logical AND operators * BAEL-3512 readme.md is updated and added new tests for bitwise & with booleans * BAEL-3512 updated variables names to more meaningful names * BAEL-3512 added example for short circuit
1 parent 8ea5a6b commit 655c545

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

core-java-modules/core-java-lang-operators/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ This module contains articles about Java operators
1111
- [Java Compound Operators](https://www.baeldung.com/java-compound-operators)
1212
- [The XOR Operator in Java](https://www.baeldung.com/java-xor-operator)
1313
- [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators)
14+
- [Bitwise & vs Logical && Operators](https://www.baeldung.com/bitwise-vs-logical-operators/)
1415

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.andoperators;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class BitwiseAndLogicalANDOperatorsUnitTest {
8+
9+
@Test
10+
public void givenTwoTrueBooleans_whenBitwiseAndOperator_thenTrue() {
11+
boolean trueBool = true;
12+
boolean anotherTrueBool = true;
13+
boolean trueANDTrue = trueBool & anotherTrueBool;
14+
assertTrue(trueANDTrue);
15+
}
16+
17+
@Test
18+
public void givenOneFalseAndOneTrueBooleans_whenBitwiseAndOperator_thenFalse() {
19+
boolean trueBool = true;
20+
boolean falseBool = false;
21+
boolean trueANDFalse = trueBool & falseBool;
22+
assertFalse(trueANDFalse);
23+
}
24+
25+
@Test
26+
public void givenTwoFalseBooleans_whenBitwiseAndOperator_thenFalse() {
27+
boolean falseBool = false;
28+
boolean anotherFalseBool = false;
29+
boolean falseANDFalse = falseBool & anotherFalseBool;
30+
assertFalse(falseANDFalse);
31+
}
32+
33+
@Test
34+
public void givenTwoIntegers_whenBitwiseAndOperator_thenNewDecimalNumber() {
35+
int six = 6;
36+
int five = 5;
37+
int shouldBeFour = six & five;
38+
assertEquals(4, shouldBeFour);
39+
}
40+
41+
@Test
42+
public void givenTwoTrueBooleans_whenLogicalAndOperator_thenTrue() {
43+
boolean trueBool = true;
44+
boolean anotherTrueBool = true;
45+
boolean trueANDTrue = trueBool && anotherTrueBool;
46+
assertTrue(trueANDTrue);
47+
}
48+
49+
@Test
50+
public void givenOneFalseAndOneTrueBooleans_whenLogicalAndOperator_thenFalse() {
51+
boolean trueBool = true;
52+
boolean falseBool = false;
53+
boolean trueANDFalse = trueBool && falseBool;
54+
assertFalse(trueANDFalse);
55+
}
56+
57+
@Test
58+
public void givenTwoFalseBooleans_whenLogicalAndOperator_thenFalse() {
59+
boolean falseBool = false;
60+
boolean anotherFalseBool = false;
61+
boolean falseANDFalse = falseBool && anotherFalseBool;
62+
assertFalse(falseANDFalse);
63+
}
64+
65+
@Test
66+
public void givenTwoFalseExpressions_whenLogicalAndOperator_thenShortCircuitFalse() {
67+
boolean shortCircuitResult = (2<1) && (4<5);
68+
assertFalse(shortCircuitResult);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)