Java Bitwise Operations and Control Structures

Bitwise Operators

Binary Operatinos

int output = 12 & 3;
System.out.println(output); // Output: 0
// AND operation: both bits must be 1 to produce 1

output = 12 & 11;
System.out.println(output); // Output: 8

output = 12 | 11;
System.out.println(output); // Output: 15
// OR operation: at least one bit is 1 to produce 1

output = 12 ^ 11;
System.out.println(output); // Output: 7
// XOR operation: different bits produce 1, same bits produce 0

Variable swappping exercises:

int x = 11;
int y = 22;
int temp = x;
x = y;
y = temp;
System.out.println(x + "----" + y);

// Addition method
x = 11;
y = 22;
x = x + y; // x=33, y=22
y = x - y; // x=33, y=11
x = x - y; // x=22, y=11
System.out.println(x + "=======" + y);

// XOR method
x = 11;
y = 22;
x = x ^ y;
y = x ^ y;
x = x ^ y;

Complement Operations

output = ~11;
System.out.println(output); // Output: -12
// Complement: flips all bits (0 becomes 1, 1 becomes 0)

Shift Operations

output = 1 << 2;
System.out.println(output); // Output: 4

output = -1 >> 2;
System.out.println(output); // Output: -1

output = 3 >> 1;
System.out.println(output); // Output: 1

System.out.println(Integer.MAX_VALUE); // 2147483647
output = -1 >>> 1;
System.out.println(output); // 2147483647
// Unsigned right shift fills with zeros

Additional Operations

boolean flag = true & false;
int m = 12, n = 12;
flag = m++ < 12 & n++ > 12; // Bitwise AND
d = m++ < 12 && n++ > 12;  // Logical AND
// Difference: & doesn't short-circuit, && does

Assignment Operators

int value = 12;
value += 2;  // Equivalent to value = value + 2
value -= 2;
value *= 2;
value /= 2;
value %= 2;

Ternary Operator

int result = true ? 11 : 22;
System.out.println(result);

result = 12.3 != 48 ? 'b' : 322;
System.out.println(true ? 3 : 4.0); // Output: 3.0

Control Flow

If and Switch Statements

int grade = 87;
if (grade >= 60) {
    System.out.println("Passed");
} else {
    System.out.println("Failed");
}

int season = 2;
switch (season) {
    case 1:
        System.out.println("Spring");
        break;
    case 2:
        System.out.println("Summer");
        break;
    case 3:
        System.out.println("Autumn");
        break;
    case 4:
        System.out.println("Winter");
        break;
    default:
        System.out.println("Season");
}

Loops

for (int i = 0; i < 10; i++) {
    System.out.println(1 + i);
}

int catSpeed = 100;
int catches = 0;
int mouseSpeed = (int) (Math.random() * 120);
while (catSpeed > mouseSpeed) {
    catches++;
    mouseSpeed = (int) (Math.random() * 120);
}
System.out.println("Cat caught " + catches + " mice.");

int sum = 0;
for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0) {
        sum += i;
    }
}
System.out.println(sum);

for (int j = 1; j < 10; j++) {
    if (j % 7 == 0) {
        continue; // Skip current iteration
    }
    System.out.println(j);
}

Arrays

int[] numbers = {1, 2, 3, 4};
int[] moreNumbers = new int[]{1, 2, 3};
int[] dynamicArray = new int[4];

System.out.println(numbers[3]); // Output: 4
numbers[0] = 99;
System.out.println(java.util.Arrays.toString(numbers));
System.out.println(numbers.length);

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

// Two-dimensional arrays
int[][] matrix = new int[4][3];
matrix[0][1] = 12;
System.out.println(java.util.Arrays.deepToString(matrix));

matrix[2] = new int[]{1, 2, 3, 4};
System.out.println(java.util.Arrays.deepToString(matrix));

// Initialize 2D array with random values
int[][] randomMatrix = new int[5][4];
for (int i = 0; i < randomMatrix.length; i++) {
    for (int j = 0; j < randomMatrix[i].length; j++) {
        randomMatrix[i][j] = (int)(Math.random() * 100);
    }
}
System.out.println(java.util.Arrays.deepToString(randomMatrix));

Tags: java Bitwise Operations Control Structures Arrays

Posted on Thu, 07 May 2026 06:36:53 +0000 by domerdel