Contents
Operators in Objective C
Overview
The Objective-C language, built on top of C, retains many of the same operators as C. These operators are crucial for creating mathematical expressions and performing various operations. Operators act on variables (operands) to produce a result. For example, in the expression c = a + b
, the +
and =
are operators, and a
, b
, and c
are operands.
Objective-C is widely used for developing iOS and macOS applications. The operators can be categorized as follows:
Types of Operators
1. Arithmetic Operators: Arithmetic operators perform basic mathematical operations on operands, such as addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ | Adds two operands | c = a + b; // c = 12 |
- | Subtracts second operand from the first | c = a - b; // c = -2 |
* | Multiplies two operands | c = a * b; // c = 35 |
/ | Divides numerator by denominator | c = a / b; // c = 4 |
% | Returns remainder of division | c = a % b; // c = 0 |
++ | Increments operand by 1 | a++; // a = 6 |
-- | Decrements operand by 1 | a--; // a = 5 |
Example:
#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int n1 = 5, n2 = 3;
NSLog(@"ADDITION = %d", (n1 + n2));
NSLog(@"SUBTRACTION = %d", (n1 - n2));
NSLog(@"MULTIPLICATION = %d", (n1 * n2));
NSLog(@"DIVISION = %d", (n1 / n2));
NSLog(@"MODULUS = %d", (n1 % n2));
[pool drain];
return 0;
}
Output:
ADDITION = 8
SUBTRACTION = 2
MULTIPLICATION = 15
DIVISION = 1
MODULUS = 2
2. Relational (Comparison) Operators: Relational operators compare two values, returning true
or false
based on the comparison.
Operator | Description | Example |
== | Checks if two operands are equal | (a == b) // false |
!= | Checks if two operands are not equal | (a != b) // true |
> | Checks if left operand is greater | (a > b) // true |
< | Checks if left operand is smaller | (a < b) // false |
>= | Checks if left operand is greater or equal | (a >= b) // true |
<= | Checks if left operand is smaller or equal | (a <= b) // false |
Example:
#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int n1 = 5, n2 = 3;
NSLog(@"ADDITION = %d", (n1 + n2));
NSLog(@"SUBTRACTION = %d", (n1 - n2));
NSLog(@"MULTIPLICATION = %d", (n1 * n2));
NSLog(@"DIVISION = %d", (n1 / n2));
NSLog(@"MODULUS = %d", (n1 % n2));
[pool drain];
return 0;
}
Output:
ADDITION = 8
SUBTRACTION = 2
MULTIPLICATION = 15
DIVISION = 1
MODULUS = 2
3. Relational (Comparison) Operators: Relational operators compare two values, returning true
or false
based on the comparison.
Operator | Description | Example |
== | Checks if two operands are equal | (a == b) // false |
!= | Checks if two operands are not equal | (a != b) // true |
> | Checks if left operand is greater | (a > b) // true |
< | Checks if left operand is smaller | (a < b) // false |
>= | Checks if left operand is greater or equal | (a >= b) // true |
<= | Checks if left operand is smaller or equal | (a <= b) // false |
Examples:
#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int n1 = 5, n2 = 3;
NSLog(@"Equal = %d", (n1 == n2));
NSLog(@"NOT Equal = %d", (n1 != n2));
NSLog(@"LESS THAN = %d", (n1 < n2));
NSLog(@"GREATER THAN = %d", (n1 > n2));
[pool drain];
return 0;
}
Output:
Equal = 0
NOT Equal = 1
LESS THAN = 0
GREATER THAN = 1
4. Logical Operators: Logical operators perform logical operations and return either true
(1) or false
(0).
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. | !(A && B) is true. |
Example:
#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int n1 = 1, n2 = 0;
NSLog(@"LOGICAL AND = %d", (n1 && n2));
NSLog(@"LOGICAL OR = %d", (n1 || n2));
NSLog(@"LOGICAL NOT = %d", (!n1));
[pool drain];
return 0;
}
Output:
LOGICAL AND = 0
LOGICAL OR = 1
LOGICAL NOT = 0
5. Bitwise Operators: Bitwise operators operate on bits and are often used for low-level programming.
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Example:
#import
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// n1 = 12(00001100), n2 = 10(00001010)
int n1 = 12, n2 = 10;
// The result is 00001000
NSLog(@"Bitwise AND(n1&n2) = %d\n", (n1 & n2));
// The result is 00001110
NSLog(@"Bitwise OR(n1|n2) = %d\n", (n1 | n2));
// The result is 00101000
NSLog(@"LEFT SHIFT(n2<<2) = %d\n", (n2 << 2));
// The result is 00000010
NSLog(@"RIGHT SHIFT(n2>>2) = %d\n", (n2 >> 2));
// The result is 00000110
NSLog(@"XOR(n1^n2) = %d\n", (n1 ^ n2));
// The result is 11110011
NSLog(@"ONCE COMPLIMENT(~n1) = %d\n", (~n1));
[pool drain];
return 0;
}
Bitwise AND:
n1&n2=12&10n1 \& n2 = 12 \& 10n1&n2=12&10
Binary: 00001100&00001010=0000100000001100 \& 00001010 = 0000100000001100&00001010=00001000
Result: 8Bitwise OR:
n1∣n2=12∣10n1 | n2 = 12 | 10n1∣n2=12∣10
Binary: 00001100∣00001010=0000111000001100 | 00001010 = 0000111000001100∣00001010=00001110
Result: 14Left Shift (<<):
n2<<2=10<<2n2 << 2 = 10 << 2n2<<2=10<<2
Binary: 00001010<<2=0010100000001010 << 2 = 0010100000001010<<2=00101000
Result: 40Right Shift (>>):
n2>>2=10>>2n2 >> 2 = 10 >> 2n2>>2=10>>2
Binary: 00001010>>2=0000001000001010 >> 2 = 0000001000001010>>2=00000010
Result: 2XOR:
n1⊕n2=12⊕10n1 \oplus n2 = 12 \oplus 10n1⊕n2=12⊕10
Binary: 00001100⊕00001010=0000011000001100 \oplus 00001010 = 0000011000001100⊕00001010=00000110
Result: 6One’s Complement (~):
∼n1=∼12\sim n1 = \sim 12∼n1=∼12
Binary: ∼00001100=11110011\sim 00001100 = 11110011∼00001100=11110011 (in two’s complement, signed integer representation)
Result: -13
6. Assignment Operators: Assignment operators assign values or results of expressions to variables.
Operator | Description |
= | Assign value |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
<<= | Left shift and assign |
>>= | Right shift and assign |
&= | Bitwise AND and assign |
^= | Bitwise XOR and assign |
Example:
#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int n1 = 20;
n1 += 10;
NSLog(@"After +=: %d", n1);
n1 &= 2;
NSLog(@"After &=: %d", n1);
[pool drain];
return 0;
}
Output:
After +=: 30
After &=: 2
Miscellaneous or Special Operators
Special operators are unique to programming languages and are used to perform specific tasks.
Operator | Description | Example |
---|---|---|
sizeof() | Returns the size of a variable in bytes. | int a = 10; sizeof(a); // Returns 4 bytes |
& | Returns the address of a variable. | &a; // Returns the address of 'a' |
* | Used to create a pointer variable. | int a = 10; int *b = &a; // Pointer to 'a' |
?: | Conditional (ternary) operator, an alternative to if-else . | int number1 = 5, number2 = 10, max; max = (number1 > number2) ? number1 : number2; // max = 10 |
Example:
Size of num1 (int) = 4 bytes
Value of num1 = 8
Value pointed by ptr = 8
Result (a == 10) = 0
Result (a == 15) = 1
Operator Precedence
Operator precedence determines the order in which operators are evaluated in an expression based on their priority. If two or more operators have the same precedence, their associativity determines the evaluation order. Associativity can be either left-to-right or right-to-left.
For example:
In the expression m = 5 - 2 / 2
, the result is m = 4
, not m = 1
, because /
(division) has higher precedence than -
(subtraction). First, 2 / 2
is evaluated, then 5 - 1
.
Operator Precedence Table
Operator | Associativity |
---|---|
() [] -> . ++ -- | Left-to-right |
+ - ! ~ ++ -- (type) * & sizeof() | Right-to-left |
* / % | Left-to-right |
<< >> | Left-to-right |
== != | Left-to-right |
^ | | Left to right |
&& ` | Left to right |
?: | Right-to-left |
= += -= *= /= %= >>= <<= &= ^= ` | Right to left |
, (comma) | Left-to-right |