CSE Rockz Home Blog Links Forum VSF

Friday, December 12, 2008

BASICS:1

Topic 1:

Operators in c: 

operators are used to compose an expression out of two or more expressions to give a desired output.

operators in c are:

Arithmatic operators

the following operators are arithmatic operators

binariy arithmatic operators:

  • + - to add 2 numbers 
  • -  - to substract 2 numbers
  • *  - to multiply 2 numbers 
  • /  - to divide 2 numbers
  • % - to find reminder of 2 numbers
two numbers are required to perform these operations, hence we call these operators as binary operators

unary arthimatic operators:

  • ++  - increment operators
  • _ _ - decrement operator
  • -     - unary minus
  • size of() 
  • type casting

  • increment operator is used to increase the value in a certain variable by one.similarly decrement operators is used to decrease that value bye one
  • incrment operator or decrament operator is used before or after a variable,the value stored in operand changes but the order in which each operator in a sequence of operations is used differs from each operator.  
          
          eg.  when a=5;  
                  a++  +  a = 10;
                  
         eg. when a=5
                        ++a + a = 12;


  • so,in an post increment,incerment is done after all other operattions,where as in                 pre incerment, increment is done first and all other operations later

  • unary minus is an operator similar to binary minus but unary minus requires only a single operand

  • sizeof() operator is used to know the size of the operand stored in the particular variable.

eg.
if a is an short integer
size of(a); = 16

  • type casting is used to change the data type of a variable when required in an operation

  eg:  if a,b are integers
1.) c = a/b(float);

2.)c=a/b;

  • here in the above example...... operation 2 returns the type integer and operation 2 gives float...(float) is the type casting used here



Relational operators

  • the Relational operators in c are:

  • > - greater than 
  • < - lesser than
  • >= - greater than or equal to
  • <= - lesser than or equal to
  • == - equality operator
  • != - not equal to operator


  • the equality operator is used to check equivalance of two operands in a condition

  • the not equal to operator used to check inequality of two operands in a condition

  • all relational operators are binary

logical operators

  • && -is used when two conditions are to be checked and statements involving these conditions are to be executed if and only if both the conditions are true.
                               eg: a>b && b>c
  • || - is used when two conditions are to be checked and statements involving these conditions are to be executed if any one of  the conditions are true.
                            eg: b>a || b>c
  • ! - a negation operator is used when statements involving these conditions are to be executed if the negtion of the condition is true
                           eg:  a!=b;
  • all logical operators are binary


bitwise operators

  • bit wise operators are used to perform operations bit by bit within a variable.

  •  when this is used between two operands, the operands are written in binary format,and the operation is performed,every bit of resultant value is obtained by operating each and every bit of operands taken.
  

  • | - when this operator is used between two bits ,resultant is 1 if any one of the bits is 1           
                    eg: 0 1 0 1
                             0 1 0 1
                         
                 res   0 1 0 1


  • if & is used the resultant bit is 1 if and only if both the bits are 1

eg: 1 1 0 0
      0 1 1 0

             res    0 1 0 0

  • if ^ (xor-exclusive or) is used the result is 1 if and only if only one of the bits is 1

eg : 0 1 0 1
       1 0 0 1
      1 1 0 0

  • ~(negation) is used to convert any bit to its opposite bit (0~ = 1 ; 1~ =0)

  • left shift(>>) is used to shift a bit to its left and to fill current bit to zero
  • right shift (<<) is used shift a bit to its right and to fill current bit to zero


eg:

6<<4(shift 6 4 times)

0 0 0 0 0 1 1 0 0 - stge 1 -6
0 0 0 0 1 1 0 0 0 -stage 2- 12
0 0 0 1 1 0 0 0 0 - stage 3- 24
0 0 1 1 0 0 0 0 0- stage 4- 48 
  • left shift doubles the value and rigt shift reduces it by 2
  • all the bitwise operators are binary exept unary negation

conditinal operators

  • conditional operators are used to check a condition and assign a value to the operand based on the condition status
  • if condition is true,avalue is assigned , where as another value is assigned if it is false.

eg: a= (b>c)?b:c;

  • the above operation means that if b>c, b value is assigned to a,else c value is assigned

1 comment:

  1. Dude excellent job!!!!
    I can surely say .... u differnt from othrs
    Wish u all succes
    All da best !!

    Krishna Teja
    Administrator
    CSE Rockz

    ReplyDelete