|
| 1 | +package com.baeldung.bitwiseoperator; |
| 2 | + |
| 3 | +public class BitwiseOperatorExample { |
| 4 | + |
| 5 | + public static void main(String[] args) { |
| 6 | + |
| 7 | + int value1 = 6; |
| 8 | + int value2 = 5; |
| 9 | + |
| 10 | + // Bitwise AND Operator |
| 11 | + int result = value1 & value2; |
| 12 | + System.out.println("result : " + result); |
| 13 | + |
| 14 | + // Bitwise OR Operator |
| 15 | + result = value1 | value2; |
| 16 | + System.out.println("result : " + result); |
| 17 | + |
| 18 | + // Bitwise Exclusive OR Operator |
| 19 | + result = value1 ^ value2; |
| 20 | + System.out.println("result : " + result); |
| 21 | + |
| 22 | + // Bitwise NOT operator |
| 23 | + result = ~value1; |
| 24 | + System.out.println("result : " + result); |
| 25 | + |
| 26 | + // Right Shift Operator with positive number |
| 27 | + int value = 12; |
| 28 | + int rightShift = value >> 2; |
| 29 | + System.out.println("rightShift result with positive number : " + rightShift); |
| 30 | + |
| 31 | + // Right Shift Operator with negative number |
| 32 | + value = -12; |
| 33 | + rightShift = value >> 2; |
| 34 | + System.out.println("rightShift result with negative number : " + rightShift); |
| 35 | + |
| 36 | + // Left Shift Operator with positive number |
| 37 | + value = 1; |
| 38 | + int leftShift = value << 1; |
| 39 | + System.out.println("leftShift result with positive number : " + leftShift); |
| 40 | + |
| 41 | + // Left Shift Operator with negative number |
| 42 | + value = -12; |
| 43 | + leftShift = value << 2; |
| 44 | + System.out.println("leftShift result with negative number : " + leftShift); |
| 45 | + |
| 46 | + // Unsigned Right Shift Operator with positive number |
| 47 | + value = 12; |
| 48 | + int unsignedRightShift = value >>> 2; |
| 49 | + System.out.println("unsignedRightShift result with positive number : " + unsignedRightShift); |
| 50 | + |
| 51 | + // Unsigned Right Shift Operator with negative number |
| 52 | + value = -12; |
| 53 | + unsignedRightShift = value >>> 2; |
| 54 | + System.out.println("unsignedRightShift result with negative number : " + unsignedRightShift); |
| 55 | + } |
| 56 | +} |
0 commit comments