Operators - Objective C
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Objective C language is rich in built0 in operators and provides following types of operators:
Arithmetic Operators
Following tables shows all the arithmetic operators supported by Objective C language. Assume variable A holds 10 and variable B holds 20 then,
Operator Description Example
+ Adds two operands A+B will give 30
- Subtracts second operand from the first A-B will give -10
* Multiplies both operand A*B will give 200
/ Divides numerator by denominator B/A will give 2
% Modulus operator and remainder B%A will give 0
of after an integer division
++ Increment operator increases integer value by one A++ will give 11
-- Decrement operator decreases integer value by one B-- will give 19
Relational Operators
Following table shows all the relational operators supported by Objective C language. Assume variable A hold 10 and variable B holds 20 then :
Operator Description Example
== Checks if the value of two operand are equal or not (A==B) is not true.
:if yes, then condition becomes true.
!= Checks if the value of two operand are equal or not (A!=B) is not true.
:if value not equal, then condition becomes true.
< Checks if the value of left operand is less than (A<B) is true.
the value of right operand:if yes, then condition
becomes true.
> Checks if the value of left operand is greater than (A>B) is not true.
the value of right operand:if yes, then condition
becomes true.
<= Checks if the value of left operand is less than (A<=B) is true.
or equal to the value of right operand:if yes,
then condition becomes true.
>= Checks if the value of left operand is greater than (A=>B) is not true.
the value of right operand:if yes, then condition
becomes true.
Logical Operators
Following table shows all the logical operators supported by Objective C language. Assume variable A hold 1 and variable B holds 0 then :
Operator Description Example
&& Called Logical AND operator. If both the operand (A&&B) is false
are non zero then condition becomes true.
|| Called logical OR operator. If any one of two (A||B) is true
operand is non zero then condition becomes true. ! Called logical NOT operator. Use to reverses the !(A&&B) is true Logical state of its operand. If a condition is true,
then logical NOT operator will make false.
Bitwise operator
Bitwise operator works on bits and perform bit by bit operation. The truth table for &, ! and ^ are as follows:
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
Assume if A=60 and B=13; now in binary format they will be as follows;
A = 0011 1100
B = 0000 1101
-------------------
A&B = 0000 1100
A||B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operator supported by Objective C language are listed in following table. Assume variable A holds 60 and variable B holds 13 then:
Operator Description Example
& Binary AND operator copies a bit to the result
if it exist in both operands
| Binary OR operator copies a bit if it exist in either (A|B) will give 61 which is
operand 0011 1101
^ Binary XOR operator copies the bit if it is set in (A^B) will give 49 which is
operand but not both. 0011 0001
~ Binary ones complement operator is unary and (~A) will give -61, which is
has the effect of 'flippingbits'. 1100 0011 in 2's complement
<< Binary left shift operator.The left operands values (A<<2) will give 240 which is
is moved left by the number of bits specified by 1111 0000.
right operand.
>> Binary right shift operator.The left operands values (A>>2) will give 15 which is
is moved right by the number of bits specified by 0000 1111
right operand.
Assignment Operators
There are following assignment operators supported by objective C language.
Operator Description Example
= Simple assignment operator, Assignment values C =A+B will assign value of
from right side operands to left side operand. A+B into C.
+= Add AND assignment operator, it adds right C +=A is equivalent to C=C+A
operand to the left operand and assign the result
to left operand.
-= Subtract AND assignment operator, it subtract C-=A is equivalent to C=C-A
right operand from the left operand and assign
the result to left operand.
*= Multiply AND assignment operator, it multiplies C*=A is equivalent to C=C*A
right operand from the left operand and assign
the result to left operand.
/= Divides AND assignment operator, it divides C/=A is equivalent to C=C/A
right operand from the left operand and assign
the result to left operand.
%/ Modulus AND assignment operator, it modulus C%=A is equivalent to C=C%A
right operand from the left operand and assign
the result to left operand.
<<= Left shift AND assignment operator C<<=2 is same as C=C<<2
>>= Right shift AND assignment operator C>>=2 is same as C=C>>2
&= Bitwise shift AND assignment operator C&=2 is same as C=C&2
^= Bitwise exclusive OR assignment operator C^=2 is same as C=C^2
|= Bitwise inclusive OR assignment operator C |=2 is same as C=C | 2
MISC Operators --> sizeof & ternary
There are few other important operators including sizeof and ?: supported by Objective C language.
Operator Description Example
sizeof() Return the size of an variable sizeof(a) where a is integer,will return 4
& Return the address of an variable &a; will give actual address of the variable
* Pointer to a variable *a; will pointer to a variable
?: Conditional Expression if condition is true ? then value X: otherwise value Y
Operator Precedence in Objective C
Operator precedence determines the grouping of term in expression. This affects how an expression is evaluated. Certain operators have higher precedence than other: for ex, the multiplication operator has higher precedence than the addition operator.
For ex, x=7+3*2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then add int?
Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.Within an expression, higher precedence operators will be evaluated first.
Operator Description Example
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
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
Arithmetic Operators
Following tables shows all the arithmetic operators supported by Objective C language. Assume variable A holds 10 and variable B holds 20 then,
Operator Description Example
+ Adds two operands A+B will give 30
- Subtracts second operand from the first A-B will give -10
* Multiplies both operand A*B will give 200
/ Divides numerator by denominator B/A will give 2
% Modulus operator and remainder B%A will give 0
of after an integer division
++ Increment operator increases integer value by one A++ will give 11
-- Decrement operator decreases integer value by one B-- will give 19
Relational Operators
Following table shows all the relational operators supported by Objective C language. Assume variable A hold 10 and variable B holds 20 then :
Operator Description Example
== Checks if the value of two operand are equal or not (A==B) is not true.
:if yes, then condition becomes true.
!= Checks if the value of two operand are equal or not (A!=B) is not true.
:if value not equal, then condition becomes true.
< Checks if the value of left operand is less than (A<B) is true.
the value of right operand:if yes, then condition
becomes true.
> Checks if the value of left operand is greater than (A>B) is not true.
the value of right operand:if yes, then condition
becomes true.
<= Checks if the value of left operand is less than (A<=B) is true.
or equal to the value of right operand:if yes,
then condition becomes true.
>= Checks if the value of left operand is greater than (A=>B) is not true.
the value of right operand:if yes, then condition
becomes true.
Logical Operators
Following table shows all the logical operators supported by Objective C language. Assume variable A hold 1 and variable B holds 0 then :
Operator Description Example
&& Called Logical AND operator. If both the operand (A&&B) is false
are non zero then condition becomes true.
|| Called logical OR operator. If any one of two (A||B) is true
operand is non zero then condition becomes true. ! Called logical NOT operator. Use to reverses the !(A&&B) is true Logical state of its operand. If a condition is true,
then logical NOT operator will make false.
Bitwise operator
Bitwise operator works on bits and perform bit by bit operation. The truth table for &, ! and ^ are as follows:
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
Assume if A=60 and B=13; now in binary format they will be as follows;
A = 0011 1100
B = 0000 1101
-------------------
A&B = 0000 1100
A||B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operator supported by Objective C language are listed in following table. Assume variable A holds 60 and variable B holds 13 then:
Operator Description Example
& Binary AND operator copies a bit to the result
if it exist in both operands
| Binary OR operator copies a bit if it exist in either (A|B) will give 61 which is
operand 0011 1101
^ Binary XOR operator copies the bit if it is set in (A^B) will give 49 which is
operand but not both. 0011 0001
~ Binary ones complement operator is unary and (~A) will give -61, which is
has the effect of 'flippingbits'. 1100 0011 in 2's complement
<< Binary left shift operator.The left operands values (A<<2) will give 240 which is
is moved left by the number of bits specified by 1111 0000.
right operand.
>> Binary right shift operator.The left operands values (A>>2) will give 15 which is
is moved right by the number of bits specified by 0000 1111
right operand.
Assignment Operators
There are following assignment operators supported by objective C language.
Operator Description Example
= Simple assignment operator, Assignment values C =A+B will assign value of
from right side operands to left side operand. A+B into C.
+= Add AND assignment operator, it adds right C +=A is equivalent to C=C+A
operand to the left operand and assign the result
to left operand.
-= Subtract AND assignment operator, it subtract C-=A is equivalent to C=C-A
right operand from the left operand and assign
the result to left operand.
*= Multiply AND assignment operator, it multiplies C*=A is equivalent to C=C*A
right operand from the left operand and assign
the result to left operand.
/= Divides AND assignment operator, it divides C/=A is equivalent to C=C/A
right operand from the left operand and assign
the result to left operand.
%/ Modulus AND assignment operator, it modulus C%=A is equivalent to C=C%A
right operand from the left operand and assign
the result to left operand.
<<= Left shift AND assignment operator C<<=2 is same as C=C<<2
>>= Right shift AND assignment operator C>>=2 is same as C=C>>2
&= Bitwise shift AND assignment operator C&=2 is same as C=C&2
^= Bitwise exclusive OR assignment operator C^=2 is same as C=C^2
|= Bitwise inclusive OR assignment operator C |=2 is same as C=C | 2
MISC Operators --> sizeof & ternary
There are few other important operators including sizeof and ?: supported by Objective C language.
Operator Description Example
sizeof() Return the size of an variable sizeof(a) where a is integer,will return 4
& Return the address of an variable &a; will give actual address of the variable
* Pointer to a variable *a; will pointer to a variable
?: Conditional Expression if condition is true ? then value X: otherwise value Y
Operator Precedence in Objective C
Operator precedence determines the grouping of term in expression. This affects how an expression is evaluated. Certain operators have higher precedence than other: for ex, the multiplication operator has higher precedence than the addition operator.
For ex, x=7+3*2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then add int?
Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.Within an expression, higher precedence operators will be evaluated first.
Operator Description Example
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
No comments: