Operators
Operators
• They are used to manipulate data at both bit and byte levels, allowing for
efficient data processing.
• Operators like the assignment and unary operators simplify code by reducing
complexity and increasing readability.
• Operators, especially bitwise and shift operators, can perform operations faster
than their functional equivalents, enhancing performance.
ASCII
ASCII (American Standard Code for Information Interchange) is a
character encoding standard used in computers and electronic devices
to represent text.
It assigns a unique numerical value to each character, including letters,
digits, punctuation marks, and control characters.
ASCII Decimal
Character Range Characters
Range
Uppercase Letters 'A' → 'Z' 65 → 90
In Java, char ch = 'A'; stores the Unicode value 65 internally, which matches the
ASCII value for basic characters .
• Arithmetic Operators
• Logical Operators
• Assignment Operators
• Unary Operators
• Ternary Operator
• Shift Operators
• Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical
calculations. Java supports addition (+), subtraction (-), multiplication
(*), division (/), and modulus (%).
It is a binary operator (Which accepts two data to perform a task.)
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
Example :
byte + byte = int
byte + short = int
short + short = int
short + long = long
double + float = double
int + double = double
char + char = int
char + int = int
char + double = double
long + float = float
int + long = long
Left Right
Result
Operator Name Operand Operand Notes
Type
(Part 1) (Part 2)
int, double,
* Multiplication Multiplicand Multiplier Result is numeric
char
int, double, Division by 0 causes
/ Division Dividend Divisor
char error
Modulus int, double,
% Dividend Divisor Gives remainder
(Remainder) char
EX:
int a = 10; int b = 5;
// Addition
int sum = a + b;
System.out.println("Sum: " + sum);
// Subtraction
int difference = a - b;
System.out.println("Difference: " + difference);
// Multiplication
int product = a * b;
System.out.println("Product: " + product);
// Division
int quotient = a / b;
System.out.println("Quotient: " + quotient);
// Modulus (Remainder)
int remainder = a % b;
System.out.println("Remainder: " + remainder);
Relational (Comparison) Operators
• Greater than (>) it will give resilt as true if the op1 is greater than op2
otherwise result is false.
• Less than (<) it will give resilt as true if the op1 is less than op2
otherwise result is false.
• Greater than or equal to (>=) it will give resilt as true if the op1 is
greater than equals to op2 otherwise result is false. { Ex: 5>= 3 = true}
• Less than or equal to (<=) it will give resilt as true if the op1 is less
than equals to op2 otherwise result is false. { ex: 5<= 6 = true }
Example for comparison operator
int a = 5; int b = 10;
// Equal to (==)
System.out.println("Is a equal to b? " + (a == b));
• Syntax: boolean varName =op1 && op2 //return only Boolean value
It will give result as true if any one of the operand is true otherwise
result will be false.
It works on the principle of a logical operator ‘OR’.
Syntax: ! (operand)
It is a unary operator takes only one data
The ! (NOT) operator in Java takes a boolean input and returns a
boolean output by inverting its value.
Operand Result
true false
false true
• Compound Assignment (e.g., +=, -=, *=, /=, %=): Performs the operation and assigns
the result to the variable.
• These are shortcuts for common assignments.
Operator Meaning Example Same as
+= Add and assign a += 5 a=a+5
Ex: int x = 5;
int a = --x; // x = 4, a = 4
int b = x--; // b = 4, x = 3
Ternary Operator
• Complement (~) (tilde symbol) Inverts all bits (1s become 0s, 0s
become 1s).
Real world use cases.
1. Access Control & Permissions Management
Many systems use bit masking to manage user permissions.
Example: File permissions in Unix/Linux (rwx - read, write, execute) can be
represented using bitwise operations.
2. Optimizing Performance in Computation
Bitwise operations are faster than arithmetic operations (x / 2 is slower than x
>> 1 for division).
Used in graphics rendering, signal processing, and cryptography for quick
calculations.
3. Data Compression & Encryption
Helps encode and compress data efficiently.
Used in encryption algorithms (like XOR in cryptography).
EX:
int a = 5; // binary: 0101
int b = 3; // binary: 0011
S.o.p(andOp);
S.o.p(orOp);
S.o.p(xor);
Shift operator:
Shift operators in Java are used to manipulate individual bits of a
number.
These operators shift the bits left or right, effectively multiplying or
dividing the number by powers of two
Left Shift (<<):
Shifts bits to the left, filling vacant positions with 0.
Equivalent to multiplying the number by 2^n (where n is the number of
positions shifted).
Right Shift (>>):
Shifts bits to the right, preserving the sign bit for negative numbers.
Equivalent to dividing the number by 2^n.
Unsigned Right Shift (>>>):
Similar to right shift (>>) but fills empty positions with 0, ignoring the sign
bit.
Used for unsigned data manipulation.
Ex:
LeftShift:
int a = 5; // Binary: 0000 0101
int result = a << 2; // Left shift by 2 positions
System.out.println(result); // Output: 20 (Binary: 0001 0100)
Right Shift:
int b = 20; // Binary: 0001 0100
int result = b >> 2; // Right shift by 2 positions
System.out.println(result); // Output: 5 (Binary: 0000 0101)
Unsigned Right Shift:
int c = -10; // Binary (32-bit representation): 1111...1010
int result = c >>> 2; // Unsigned right shift by 2 positions
System.out.println(result); // Output: A large positive number due to sign bit
removal