Operator - Operand - and Arithmetic
Operator - Operand - and Arithmetic
• Example :
C=A+B
(= and + sign are operators, A, B and C are operands)
• Type of the result will follow the left hand side operand
int x = 7/2; /*x value is 3 not 3.5*/
float y = 3; /*y value is 3.000 */
int main () {
int x=44; int y = 44; int z;
z = ++x; /* z, x is 45 */
z = y++; /* z is 44 and y is 45 */
return(0);
}
Answer: A
Symbol Functionality
== Equality
!= Not equal
< Less than
> Greater than
<= Less or equal than
>= Greater or equal than
?: Conditional assignment
• Syntax :
exp1 ? exp2 : exp3;
• Example (similar meaning with the above statement):
z = (a > b) ? a : b;
Example :
int x=5; int y=0;
x && y; // FALSE
(x > y) && (y>=0); // TRUE
| OR A | B;
^ XOR A ^ B;
~ Complement ~A;
Example:
int A=24; int B=35; int C;
C = A & B; // value C = 0
C = A | B; // value C = 59
Note:
A=24 binary: 011000
B=35 binary: 100011
Bit by bit AND operation resulting: 000000, in
decimal: 0
Bit by bit OR operation resulting : 111011, in
decimal: 59 COMP6047 - Algorithm and Programming 22
Bitwise Operators
XOR operation of two bits resulting 1 if both bit are
differ, and will result 0 if both are analogous.
Example:
int A,B=45;
A = B^75; // Result A=102
Note:
Decimal 45 binary: 0101101
Decimal 75 binary: 1001011
bit by bit XOR will result in : 1100110 or 102 in
decimal
COMP6047 - Algorithm and Programming 23
Bitwise Operators
To create a complement-1 value, then every bit with 0
value exchange to 1 and vice versa.
Example:
int A, B=0xC3;
A=~B; //value A=0x3C;
In C, writing a
Note: hexadecimal should start
0xC3 binary: 1100 0011 with 0x
complement-1 result:
0011 1100 in hexadecimal 3C