📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. The Bitwise Logical Operators
The Bitwise NOT
The Bitwise AND
The Bitwise OR
The Bitwise XOR
2. Using the Bitwise Logical Operators
package net.javaguides.corejava.operators.bitwise;
public class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
3. The Left Shift
value << num
package net.javaguides.corejava.operators.bitwise;
public class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte)(a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
Original value of a: 64
i and b: 256 0
4. Java Right Shift Operator
value >> num
class OperatorExample {
public static void main(String args[]) {
System.out.println(10 >> 2); //10/2^2=10/4=2
System.out.println(20 >> 2); //20/2^2=20/4=5
System.out.println(20 >> 3); //20/2^3=20/8=2
}
}
2
5
2
5. Bitwise Operator Compound Assignments
a = a >> 4;
a >>= 4;
a = a | b;
a |= b;
package net.javaguides.corejava.operators.bitwise;
public class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
a = 3
b = 1
c = 6
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment