Wednesday, August 22, 2018

Operators N Expressions


Operators are symbols that trigger an action when applied to C variables and other objects. The data items on which operators act upon are called operands.

C is flexible and powerful, because it has rich set of operators. It has following operator set:

1.    Arithmetic Operators

2.    Modulus Operator

3.    Assignment Operators

4.    Unary Operators

5.    Relational Operators

6.    Logical Operators

7.    Bitwise Operators

The operators that work on two objects are called binary operators. Above all are not binary operators. The built-in mathematics related library functions are available to user of C language and are called by including <math.h> directive to code.

Expressions

An expression is a collection of data objects and operators that can be evaluated to a single value. An object is a constant, variable, or another expression. The most general form is

5 + 3

2.345 – 1.345

12 * 45

365 / 12

10 % 2

Parentheses and Precedence

If more than one expression is involved, using parentheses makes it readable and fixes order of evaluation to it. The fundamental operators follows a predictable precedence rule, call it a BOUDMASA rule.

B O U D M A S A

means

–Brackets-Order [In to Out, Left to Right]-Unary-Division-Multiplication-Addition-Subtraction-Assignment

 

Arithmetic Operators

There are four basic binary operators and are as follows: Addition (+), Subtraction (-), Multiplication (*) and Division (/).

Assignment Operator

This Assignment operator is evaluated last and appear anywhere in expression as any operator [like + or * operators in above example]. The below are valid statements.

i = j = 0;

tempData= adcData = adcValue * avgValue;

x = 2 * y + (z = x*5);

Note that in C language ‘=’ is an assignment operator and not an equal mark. To equate two objects ‘==’ [double equal sign] is to be used, such as if (a == b).

If (a == 0)  

//evaluates, if a equals zero prints message.

     puts(“a equals zero”);

If (a = 0)   

//assigns zero to a, a is not high/true hence message not prints.

     puts(“a equals zero”);

Modulus Operator

The operator helps to get the final remainder of the two objects division. The Modulus operator only works on char and int data types and not on float or double type.

x =257;     y = 5;      z = x %y ;       //z = 2

 

5 ) 257 ( 51

      25

    -------

     007

         5

     -------

          2

Unary Operators

The Increment or Decrement a variable value by one is common task in any programming language. So special operators are provided by C to deal with this task and it needs only one object to carry out it [it is not a binary but unary operator].

/*++i = prefix increment: incremented before assign */

I = 3 ; j = ++I ; /* now j = 4 and I = 4 */

/*--i = prefix decrement: decremented before assign */

I = 4 ; j = ++I ; /* now j = 3 and I = 3 */

/*i++ = postfix increment: incremented after assign */

I = 3 ; j = i++ ; /* now j = 3 and I = 4 */

/*i-- = postfix decrement: decremented after assign */

I = 4 ; j = ++I ; /* now j = 4 and I = 3 */

Shorthand Notations

While writing expression shorthand notations are used to reduce the space, but they are sometimes hard to read and understand. See the below examples.

 

shorthand statement

equivalent to

 

i + = 5;

i = i + 5 ;

 

Sal * = rate;

Sal = Sal * rate ;

In=5; Out=7;

In+=Out++;

In = In +( Out++)

[after evaluation, In=12,Out=8]

Relational Operators

Every programming language provides some kind of operators to check conditions of certain object in its program during its operation. Two such mechanisms are relational operators and logical operators in C language.

Note that Relational Operators/Logical Operators are evaluated after all arithmetic operators. The equal & not equal operators are evaluated lastly. Both operators return an integer value, 0 for false and 1 for true to indicate the evaluation result.

Operator

Meaning

Example

==

equal

if (switch == pressed)

!=

not equal

if(ans != ‘y’)

<

less than

if (a < b )

>

greater than

if ( b > c )

<=

less than or equal to

if ( i <= 5)

>=

greater than or equal to

if ( j >= index)

Logical Operators

There are three logical operators in C language, AND, OR and NOT. These are used to combine relational tests in single expression. These treat non-zero operands as true and zero operands as false, for example x = 5 then it is treated as true and if x = 0 then as false operand. The below table shows evaluation results of all input combinations of AND and OR operators. The NOT operator just returns inverse of its input operand value.

The Logical AND operator represented by two ampersand symbols: &&. The Logical OR operator is represented by two pipe symbols [not slashes but shift + back slash key]: ||. The Logical Not operator is represented by exclamation mark: !.

OPERANDS

RESULTS

Operand 1

Operand 2

&& [AND]

| | [OR]

0

0

0

0

0

1

0

1

1

0

0

1

1

1

1

1

HERE 0 = FALSE [ZERO VALUE]  1 = TRUE [NON-ZERO VALUE]

!(Operand)

!(TRUE)

FALSE

! OR NOT OPERATOR

!(Operand)

!(FALSE)

TRUE

 

The logical operator connects expressions in logical way and helps to take decision. For example if two or more options are to be checked for status to take one decision.

Bitwise Operators

The digital computer data is stored in binary form, so it is usually required to test/set/reset each bit for certain status or condition. Such operation is done on binary data in C language by using bitwise operators, where five are binary operators and one is unary operator. The below table list them.

Operator

Meaning

&

bitwise AND

|

bitwise OR

^

bitwise Exclusive-OR

<<

shift left

>>

shift right

~

one’s complement

Note that bitwise operators are applied only to integers, not any other data types like float or double.

The below table shows all input combinations of bitwise AND, bitwise OR and bitwise Exclusive-OR operators with their corresponding bit set/reset status. These three operator output is same as corresponding logical operators. The One’s Complement operator just returns inverse of its input operand value.

The bitwise AND operator is represented by single [a bit] ampersand symbol: &. The bitwise OR operator is represented by single pipe symbol [not slashes but shift + back slash key]: |.The bitwise Exclusive-OR is represented by single caret symbol [not quotation mark but shift+6 key]: ^. The One’s Complement operator is represented by tilde mark [shift + `, first key of number keys]: ~.

OPERANDS

RESULTS

Operand 1

Operand 2

&

|

^

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

HERE 0 = RESET   1 = SET

~(Operand)

!(1111)

0000

~ OR  One’s Complement OPERATOR

~(Operand)

!(0000)

1111

 

These Bitwise operators are usually use in embedded systems to check which key is pressed in 4 x 4 matrix key board connected to microcontroller input/output port pins. These are useful for controlling and interacting with hardware, other devices, and in performing some mathematical tasks.

bitwise AND operator (&)

This operator is used to check whether particular bit of an operand is set or reset.

bitwiseOR operator (|)

This operator is used to turn ON the bit whether that bit of an operand is set or reset.

Bitwise Ex-OR operator (^)

The above one is inclusive OR operator, where any or both inputs are high output goes high. But in this Exclusive OR operator, the output goes high only when any one input is high. The operation and usage is as same as OR operator.

One’s Complement operator (~)

This operator just inverts all bits of the operands and is a unary operator [works with single operand]. This operator is used to do some mathematical operations on data with certain rule. See below examples for action.

~(1010) = 0101

10 changes to 5

~(1111) = 0000

15 changes to 0

~(10000) =01111

16 changes to 15

 

 

Left Shift operator (<<)

Like One’s Complement operator, Left Shift operator too operates on a single variable [but is not unary operator as it needs two operands]. As it shifts bits to left side, blanks are created on right side and are filled with zeros.

/* num = 11010111, apply left shift << to it */

num << 3 /* now num = 10111000 three zeroes filled to right side of number */

Right Shift operator (>>)

Like One’s Complement operator, Right Shift operator too operates on a single variable [but is not unary operator as it needs two operands]. As it shifts bits to right side, blanks are created on left side and are filled with zeros.

/* num = 110011110, apply right shift >> to it */

num >> 3 /* now num = 000110011 three zeroes filled to left side of number */


Previous Page Main Contents Next Page

No comments:

Post a Comment