codescracker


objective c

Objective-C Operators



« Previous Tutorial Next Tutorial »


An operator is simply used to perform particular mathematical or logical manipulations. Here are the following types of operators in Objective-C:

Let's take a look at all these types of operators

Arithmetic Operators in Objective-C

Here, the following table lists all the arithmetic operators in Objective-C:

Operator Meaning
+ Adds two operands
- Subtracts second operand from the first
/ Divides numerator by denominator
* Multiplies both the operands
% Modulus operator and gives a remainder
++ Increment operator which increases the integer value by one
-- Decrement operator which decreases the integer value by one

Example

Here is an example program, uses arithmetic operators in Objective-C:

/* Objective-C Operators - Example Program */
		
#import <Foundation/Foundation.h>
main()
{
	int num1 = 23;
	int num2 = 10;
	int res;
	res=num1+num2;
	NSLog(@"res = %d\n", res);
	res=num1-num2;
	NSLog(@"res = %d\n", res);
	res=num1*num2;
	NSLog(@"res = %d\n", res);
	res=num1/num2;
	NSLog(@"res = %d\n", res);
	res=num1%num2;
	NSLog(@"res = %d\n", res);
	res=num1++;
	NSLog(@"res = %d\n", res);
	res=num1--;
	NSLog(@"res = %d\n", res);
}

When the above code is compiled and executed, it would produce the following result:

2013-08-03 13:18:37.932 demo[25774] res = 33
2013-08-03 13:18:37.932 demo[25774] res = 13
2013-08-03 13:18:37.932 demo[25774] res = 230
2013-08-03 13:18:37.932 demo[25774] res = 2
2013-08-03 13:18:37.932 demo[25774] res = 3
2013-08-03 13:18:37.932 demo[25774] res = 23
2013-08-03 13:18:37.932 demo[25774] res = 24

Relational Operators in Objective-C

Here is the table, lists all the relational operators in Objective-C:

Operator Meaning
!= This checks whether the values of both the operands are equal or not, if the values are not equal then the condition becomes true
== This checks whether the values of both the operands are equal or not, if equal then the condition becomes true
< This checks whether the value of the left operand is less than the value of the right operand, if yes then the condition becomes true
> This checks whether the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true
<= This checks whether the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true
>= This checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true

Example

Here is an example program, uses some of the relational operators in Objective-C:

/* Objective-C Operators - Example Program */
		
#import <Foundation/Foundation.h>
main()
{
	int num1 = 23;
	int num2 = 10;
	int res;
	if(num1==num2)
	{
		NSLog(@"num1 is equal to num2\n");
	}	
	else
	{
		NSLog(@"num1 is not equal to num2\n");
	}	
	if(num1<num2)
	{
		NSLog(@"num1 is less than num2\n");
	}	
	else
	{
		NSLog(@"num1 is not less than num2\n");
	}	
	if(num1>num2)
	{
		NSLog(@"num1 is greater than num2\n");
	}	
	else
	{
		NSLog(@"num1 is not greater than num2\n");
	}
}

When the above code is compile and executed, it would produce the following result:

2013-08-03 13:18:37.932 demo[9486] num1 is not equal to num2
2013-08-03 13:18:37.932 demo[9486] num1 is not less than num2
2013-08-03 13:18:37.932 demo[9486] num1 is greater than num2

Logical Operators in Objective-C

The table given below table lists all the logical operators in Objective-C:

Operator Meaning
|| this is a Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true
&& This is a Logical AND operator. If both the operands are non-zero, then the condition becomes true
! This is a Logical NOT Operator. This operator use to reverses the logical state of its operand. If a condition is true then the Logical NOT operator will make false

Example

Here is an example program, uses logical operators in Objective-C:

/* Objective-C Operators - Example Program */
		
#import <Foundation/Foundation.h>
main()
{
	int num1 = 10;
	int num2 = 15;
	int res;
	if(num1&&num2)
	{
		NSLog(@"Condition is true\n");
	}	
	if(num1||num2)
	{
		NSLog(@"Condition is true\n");
	}
	/* changing the values of num1 and num2 */
	num1 = 0;
	num2 = 15;
	if(num1&&num2)
	{
		NSLog(@"Condition is true\n");
	}
	else
	{
		NSLog(@"Condition is not true\n");
	}
	if(!(num1&&num2))
	{
		NSLog(@"Condition is true\n" );
	}
}

When the above code is compiled and executed, it would produce the following result:

2013-08-03 13:18:37.932 demo[19012] Condition is true
2013-08-03 13:18:37.932 demo[19012] Condition is true
2013-08-03 13:18:37.932 demo[19012] Condition is not true
2013-08-03 13:18:37.932 demo[19012] Condition is true

Bitwise Operators in Objective-C

Here is the table which lists all the bitwise operators in Objective-C:

Operator Meaning
| This is a Binary OR Operator. It copies a bit if it exists in either the operands
& This is a Binary AND Operator. It copies a bit to the result if it exists in both the operands
^ This is a Binary XOR Operator. It copies the bit if it is set in one operand but not in both the operands
~ This is a Binary Ones Complement Operator. It is unary and has the effect of 'flipping' bits
>> This is a Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by right operand
<< This is a Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by right operand

Bitwise operator works on bits and perform the operators, bit by bit. Here is the truth table for &, |, and ^

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Example

Let's assume if X = 60; and Y = 13; then in the binary format they are as follows:

AND Operator Example

  X = 0011 1100

& Y = 0000 1101
  -------------
      0000 1100

OR Operator Example

  X = 0011 1100

| Y = 0000 1101
  -------------
      0011 1101

XOR Operator Example

  X = 0011 1100

^ Y = 0000 1101
  -------------
      0011 0001

Ones Complement Operator Example

  X = 0011 1100

~ Y = 0000 1101
  -------------
      1100 0011

Here is an example program, uses bitwise operators in Objective-C:

/* Objective-C Operators - Example Program */
		
#import <Foundation/Foundation.h>
main()
{
   unsigned int x = 60;
   unsigned int y = 13;
   int res = 0;           
   res = x & y;
   NSLog(@"res = %d\n", res);
   res = x | y;
   NSLog(@"res = %d\n", res);
   res = x ^ y;
   NSLog(@"res = %d\n", res);
   res = ~x;
   NSLog(@"res = %d\n", res);
   res = y << 2;
   NSLog(@"res = %d\n", res);
   res = x >> 2;
   NSLog(@"res = %d\n", res);
}

When the above code is compiled and executed, it would produce the following result :

2013-08-03 13:18:37.932 demo[30836] res = 12
2013-08-03 13:18:37.932 demo[30836] res = 61
2013-08-03 13:18:37.932 demo[30836] res = 49
2013-08-03 13:18:37.932 demo[30836] res = -61
2013-08-03 13:18:37.932 demo[30836] res = 240
2013-08-03 13:18:37.932 demo[30836] res = 15

The following table shows the above decimal values in binary form:

Decimal Binary
60 0011 1100
13 0000 1101
12 0000 1100
61 0011 1101
49 0011 0001
-61 1100 0011
240 1111 0000
15 0000 1111

Assignment Operators in Objective-C

There are following assignment operators in Objective-C

Operator Meaning
= This is a Simple Assignment operator. This operator assigns the values from the right side operands to the left side operand
-= This is a Subtract AND assignment operator. This operator subtracts the right operand from the left operand and assign the result to the left operand
+= This is a Add AND assignment operator. This operator adds the right operand to the left operand and assign the result to the left operand
/= This is a Divide AND assignment operator. This operator divides the left operand with the right operand and assign the result to the left operand
*= This is a Multiply AND assignment operator. This operator multiplies the right operand with the left operand and assign the result to the left operand
%= This is a Modulus AND assignment operator. This operator takes modulus using the two operands and assign the result to the left operand
>>= This is a Right shift AND assignment operator
<<= This is a Left shift AND assignment operator
&= This is a Bitwise AND assignment operator
|= This is a Bitwise inclusive OR and assignment operator
^= This is a Bitwise exclusive OR and assignment operator

Example

Here is an example program in Objective-C, uses assignment operators:

/* Objective-C Operators - Example Program */
		
#import <Foundation/Foundation.h>
main()
{
	int x = 21;
	int res;
	
	res=x;
	NSLog(@"Objective-C Operator Example (=), res = %d\n", res);
	res += x;
	NSLog(@"Objective-C Operator Example (+=), res = %d\n", res);
	res -= x;
	NSLog(@"Objective-C Operator Example (-=), res = %d\n", res);
	res *= x;
	NSLog(@"Objective-C Operator Example (*=), res = %d\n", res);
	res /= x;
	NSLog(@"Objective-C Operator Example (/=), res = %d\n", res);
	res = 200;
	res %= x;
	NSLog(@"Objective-C Operator Example (%=), res = %d\n", res);
	res <<= 2;
	NSLog(@"Objective-C Operator Example (<<=), res = %d\n", res);
	res >>= 2;
	NSLog(@"Objective-C Operator Example (>>=), res = %d\n", res);
	res &= 2;
	NSLog(@"Objective-C Operator Example (&=), res = %d\n", res);
	res ^= 2;
	NSLog(@"Objective-C Operator Example (^=), res = %d\n", res);
	res |= 2;
	NSLog(@"Objective-C Operator Example (|=), res = %d\n", res);
}

When the above code is compiled and executed, it would produce the following result:

2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (=), res = 21
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (+=), res = 42
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (-=), res = 21
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (*=), res = 441
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (/=), res = 21
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (%=), res = 11
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (<<=), res = 44
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (>>=), res = 11
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (&=), res = 2
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (^=), res = 0
2013-08-03 13:18:37.932 demo[21858] Objective-C Operator Example (|=), res = 2

Misc (sizeof & ternary) Operators in Objective-C

Here the following table lists some other important operators in Objective-C:

Operator Meaning
& Returns the address of an variable
sizeof() Returns the size of a variable
? : Conditional Expression. If the Condition is true ? Then this value : Otherwise this value
* Pointer to a variable

Example

Here is an example, uses misc operators in Objective-C:

/* Objective-C Operators - Example Program */
		
#import <Foundation/Foundation.h>
main()
{
   int x = 4;
   short y;
   double z;
   int *ptr;
   NSLog(@"Size of x = %d\n", sizeof(x));
   NSLog(@"Size of y = %d\n", sizeof(y));
   NSLog(@"Size of z = %d\n", sizeof(z));
   ptr = &x;
   NSLog(@"Value of x = %d\n", x);
   NSLog(@"*ptr = %d\n", *ptr);
   x = 10;
   y = (x == 1) ? 20: 30;
   NSLog(@"Value of y = %d\n", y);
   y = (x == 10) ? 20: 30;
   NSLog(@"Value of y = %d\n", y);
}

When the above code is compiled and executed, it would produce the following result:

2013-08-03 13:18:37.932 demo[24593] Size of x = 4
2013-08-03 13:18:37.932 demo[24593] Size of y = 2
2013-08-03 13:18:37.932 demo[24593] Size of z = 8
2013-08-03 13:18:37.932 demo[24593] Value of x = 4
2013-08-03 13:18:37.932 demo[24593] *ptr = 4.
2013-08-03 13:18:37.932 demo[24593] Value of y = 30
2013-08-03 13:18:37.932 demo[24593] Value of y = 20

Objective-C Operator Precedence

Here, in the following table, operators with the highest precedence appears at the top of the table, and those with the lowest appears at the bottom. As you know that, within an expression, operators with higher precedence are evaluated first

Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test