Showing posts with label codelib. Show all posts
Showing posts with label codelib. Show all posts

Saturday, January 22, 2011

Difference in time


/* 
   Returns difference in time between a given start hour, 
   start min and end hour and end min 
*/

int difftime(int sh, int sm, int eh, int em)
{
   int s, e ;
   s = 60*(sh-0) + sm ;
   e = 60*(eh-0) + em ;
   e -=s ;

   if (e < 0)
   e += 60 * 24 ;
   return e ;
}

Thursday, January 6, 2011

Useful Bit-manipulation Functions


/* Function to print binary representation of a number */
void printBinary(int n)
{
     int i ;
     for (i=31; i>=0; i--)
          printf("%d", ((1<<i & n))?1:0) ;
     printf("\n") ;
}

/* Function to get 2's complement of a number */
int get2scomplement(int n)
{
     int i, k=0 ;
     for (i=0; i<32; i++)
          k |= ~(1<<i | n) ;
     k+=1 ;

     return k ;
}

Followers