WikiGalaxy

Personalize

Bitwise AND Operation

Understanding Bitwise AND:

The bitwise AND operation takes two binary numbers and performs the logical AND operation on each pair of corresponding bits. The result is 1 if both bits are 1, otherwise, it is 0.


int a = 5;  // 0101 in binary
int b = 3;  // 0011 in binary
int result = a & b; // 0001 in binary
System.out.println(result); // Outputs 1
    

Console Output:

1

Bitwise OR Operation

Understanding Bitwise OR:

The bitwise OR operation compares each pair of corresponding bits from two binary numbers. The result is 1 if at least one of the bits is 1; otherwise, it is 0.


int a = 5;  // 0101 in binary
int b = 3;  // 0011 in binary
int result = a | b; // 0111 in binary
System.out.println(result); // Outputs 7
    

Console Output:

7

Bitwise XOR Operation

Understanding Bitwise XOR:

The bitwise XOR operation compares each pair of corresponding bits from two binary numbers. The result is 1 if the bits are different; otherwise, it is 0.


int a = 5;  // 0101 in binary
int b = 3;  // 0011 in binary
int result = a ^ b; // 0110 in binary
System.out.println(result); // Outputs 6
    

Console Output:

6

Bitwise NOT Operation

Understanding Bitwise NOT:

The bitwise NOT operation inverts each bit of a binary number. This means that all 0s become 1s and all 1s become 0s.


int a = 5;  // 0101 in binary
int result = ~a; // 1010 in binary (inverted)
System.out.println(result); // Outputs -6
    

Console Output:

-6

Left Shift Operation

Understanding Left Shift:

The left shift operation shifts all bits in a binary number to the left by a specified number of positions, filling the vacant positions with 0s. This operation effectively multiplies the number by 2 for each shift position.


int a = 5;  // 0101 in binary
int result = a << 1; // 1010 in binary
System.out.println(result); // Outputs 10
    

Console Output:

10

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025