diff --git a/core-java-modules/core-java-lang-operators/README.md b/core-java-modules/core-java-lang-operators/README.md index 1fe524948c..3af2c8885b 100644 --- a/core-java-modules/core-java-lang-operators/README.md +++ b/core-java-modules/core-java-lang-operators/README.md @@ -11,4 +11,5 @@ This module contains articles about Java operators - [Java Compound Operators](https://www.baeldung.com/java-compound-operators) - [The XOR Operator in Java](https://www.baeldung.com/java-xor-operator) - [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators) +- [Bitwise & vs Logical && Operators](https://www.baeldung.com/bitwise-vs-logical-operators/) diff --git a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java new file mode 100644 index 0000000000..6158c2870d --- /dev/null +++ b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.andoperators; + +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.*; + +public class BitwiseAndLogicalANDOperatorsUnitTest { + + @Test + public void givenTwoTrueBooleans_whenBitwiseAndOperator_thenTrue() { + boolean trueBool = true; + boolean anotherTrueBool = true; + boolean trueANDTrue = trueBool & anotherTrueBool; + assertTrue(trueANDTrue); + } + + @Test + public void givenOneFalseAndOneTrueBooleans_whenBitwiseAndOperator_thenFalse() { + boolean trueBool = true; + boolean falseBool = false; + boolean trueANDFalse = trueBool & falseBool; + assertFalse(trueANDFalse); + } + + @Test + public void givenTwoFalseBooleans_whenBitwiseAndOperator_thenFalse() { + boolean falseBool = false; + boolean anotherFalseBool = false; + boolean falseANDFalse = falseBool & anotherFalseBool; + assertFalse(falseANDFalse); + } + + @Test + public void givenTwoIntegers_whenBitwiseAndOperator_thenNewDecimalNumber() { + int six = 6; + int five = 5; + int shouldBeFour = six & five; + assertEquals(4, shouldBeFour); + } + + @Test + public void givenTwoTrueBooleans_whenLogicalAndOperator_thenTrue() { + boolean trueBool = true; + boolean anotherTrueBool = true; + boolean trueANDTrue = trueBool && anotherTrueBool; + assertTrue(trueANDTrue); + } + + @Test + public void givenOneFalseAndOneTrueBooleans_whenLogicalAndOperator_thenFalse() { + boolean trueBool = true; + boolean falseBool = false; + boolean trueANDFalse = trueBool && falseBool; + assertFalse(trueANDFalse); + } + + @Test + public void givenTwoFalseBooleans_whenLogicalAndOperator_thenFalse() { + boolean falseBool = false; + boolean anotherFalseBool = false; + boolean falseANDFalse = falseBool && anotherFalseBool; + assertFalse(falseANDFalse); + } + + @Test + public void givenTwoFalseExpressions_whenLogicalAndOperator_thenShortCircuitFalse() { + boolean shortCircuitResult = (2<1) && (4<5); + assertFalse(shortCircuitResult); + } + +}