int
, long
, short
, byte
and char
.
int
, long
, short
, byte
and char
) but not with floating-point or boolean values.
^
) is often used in simple encryption schemes.
<<
, >>
, >>>
) for tasks like scaling numbers or accessing specific bits.
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
System.out.println(a & b); // Output: 1 (Binary: 0001)
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
System.out.println(a | b); // Output: 7 (Binary: 0111)
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
System.out.println(a ^ b); // Output: 6 (Binary: 0110)
int a = 5; // Binary: 0101
System.out.println(~a); // Output: -6 (Binary: 1010 in two's complement form)
public class BitwiseOperators
{
public static void main(String[] args)
{
// Initialize variables
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
// Bitwise AND (&)
System.out.println("Bitwise AND (a & b): " + (a & b)); // Output: 1 (Binary: 0001)
// Bitwise OR (|)
System.out.println("Bitwise OR (a | b): " + (a | b)); // Output: 7 (Binary: 0111)
// Bitwise XOR (^)
System.out.println("Bitwise XOR (a ^ b): " + (a ^ b)); // Output: 6 (Binary: 0110)
// Bitwise Complement (~)
System.out.println("Bitwise Complement (~a): " + (~a)); // Output: -6 (Binary: 1010 in two's complement)
// Demonstrating with larger numbers
int x = 12; // Binary: 1100
int y = 10; // Binary: 1010
System.out.println("\nWith larger numbers:");
System.out.println("Bitwise AND (x & y): " + (x & y)); // Output: 8 (Binary: 1000)
System.out.println("Bitwise OR (x | y): " + (x | y)); // Output: 14 (Binary: 1110)
System.out.println("Bitwise XOR (x ^ y): " + (x ^ y)); // Output: 6 (Binary: 0110)
System.out.println("Bitwise Complement (~x): " + (~x)); // Output: -13 (Binary: 0011 in two's complement)
}
}
Bitwise AND (a & b): 1 Bitwise OR (a | b): 7 Bitwise XOR (a ^ b): 6 Bitwise Complement (~a): -6 With larger numbers: Bitwise AND (x & y): 8 Bitwise OR (x | y): 14 Bitwise XOR (x ^ y): 6 Bitwise Complement (~x): -13
System.out.println(5 & 3); // Output: 1
System.out.println(8 | 2); // Output: 10
byte
, short
, int
, long
and char
.
float
, double
) will result in a compilation error.
System.out.println(~5); // Output: -6
boolean
or String
will result in a compilation error.
int result = 5 + 3 & 2; // Evaluates as (5 + 3) & 2 = 8 & 2 = 0
int result = (5 | 3) & 2; // Binary: (0101 | 0011) & 0010 = 0111 & 0010 = 0010 = 2
byte
uses 8 bits, so bitwise operations are limited to this range, while int
uses 32 bits.
~
:
~
operator flips all bits, including the sign bit in two's complement representation.
int a = 5; // Binary: 00000000 00000000 00000000 00000101
System.out.println(~a); // Binary: 11111111 11111111 11111111 11111010 = -6
int
(or long
if one operand is long
) before the operation is performed.
byte a = 5, b = 3;
System.out.println(a & b); // Implicitly promoted to int during operation
&
, |
, ^
) are not interchangeable with logical operators (&&
, ||
).
System.out.println(true & false); // Output: false
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.