Adding test file for bitwise operators
This commit is contained in:
parent
3aff027b7a
commit
2fb3e867d7
|
@ -1,56 +0,0 @@
|
|||
package com.baeldung.bitwiseoperator;
|
||||
|
||||
public class BitwiseOperatorExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
|
||||
// Bitwise AND Operator
|
||||
int result = value1 & value2;
|
||||
System.out.println("result : " + result);
|
||||
|
||||
// Bitwise OR Operator
|
||||
result = value1 | value2;
|
||||
System.out.println("result : " + result);
|
||||
|
||||
// Bitwise Exclusive OR Operator
|
||||
result = value1 ^ value2;
|
||||
System.out.println("result : " + result);
|
||||
|
||||
// Bitwise NOT operator
|
||||
result = ~value1;
|
||||
System.out.println("result : " + result);
|
||||
|
||||
// Right Shift Operator with positive number
|
||||
int value = 12;
|
||||
int rightShift = value >> 2;
|
||||
System.out.println("rightShift result with positive number : " + rightShift);
|
||||
|
||||
// Right Shift Operator with negative number
|
||||
value = -12;
|
||||
rightShift = value >> 2;
|
||||
System.out.println("rightShift result with negative number : " + rightShift);
|
||||
|
||||
// Left Shift Operator with positive number
|
||||
value = 1;
|
||||
int leftShift = value << 1;
|
||||
System.out.println("leftShift result with positive number : " + leftShift);
|
||||
|
||||
// Left Shift Operator with negative number
|
||||
value = -12;
|
||||
leftShift = value << 2;
|
||||
System.out.println("leftShift result with negative number : " + leftShift);
|
||||
|
||||
// Unsigned Right Shift Operator with positive number
|
||||
value = 12;
|
||||
int unsignedRightShift = value >>> 2;
|
||||
System.out.println("unsignedRightShift result with positive number : " + unsignedRightShift);
|
||||
|
||||
// Unsigned Right Shift Operator with negative number
|
||||
value = -12;
|
||||
unsignedRightShift = value >>> 2;
|
||||
System.out.println("unsignedRightShift result with negative number : " + unsignedRightShift);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.bitwiseoperator.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BitwiseOperatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoIntegers_whenAndOperator_thenNewDecimalNumber() {
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
int result = value1 & value2;
|
||||
Assert.assertEquals(result, 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoIntegers_whenOrOperator_thenNewDecimalNumber() {
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
int result = value1 | value2;
|
||||
Assert.assertEquals(result, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoIntegers_whenXorOperator_thenNewDecimalNumber() {
|
||||
int value1 = 6;
|
||||
int value2 = 5;
|
||||
int result = value1 ^ value2;
|
||||
Assert.assertEquals(result, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneInteger_whenNotOperator_thenNewDecimalNumber() {
|
||||
int value1 = 6;
|
||||
int result = ~value1;
|
||||
Assert.assertEquals(result, -7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOnePositiveInteger_whenSignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = 12;
|
||||
int rightShift = value >> 2;
|
||||
Assert.assertEquals(rightShift, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneNegativeInteger_whenSignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = -12;
|
||||
int rightShift = value >> 2;
|
||||
Assert.assertEquals(rightShift, -3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOnePositiveInteger_whenLeftShiftOperator_thenNewDecimalNumber() {
|
||||
int value = 12;
|
||||
int leftShift = value << 2;
|
||||
Assert.assertEquals(leftShift, 48);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneNegativeInteger_whenLeftShiftOperator_thenNewDecimalNumber() {
|
||||
int value = -12;
|
||||
int leftShift = value << 2;
|
||||
Assert.assertEquals(leftShift, -48);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOnePositiveInteger_whenUnsignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = 12;
|
||||
int unsignedRightShift = value >>> 2;
|
||||
Assert.assertEquals(unsignedRightShift, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneNegativeInteger_whenUnsignedRightShiftOperator_thenNewDecimalNumber() {
|
||||
int value = -12;
|
||||
int unsignedRightShift = value >>> 2;
|
||||
Assert.assertEquals(unsignedRightShift, 1073741821);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue