Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

nevada

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

swap values without temporary variable

The XOR swap algorithm is an inefficient method of swapping two variables. http://en.wikipedia.org/wiki/Xor_swap_algorithm

   1  
   2  void XORSwap(void *x, void *y)
   3  {
   4     *x ^= *y;
   5     *y ^= *x;
   6     *x ^= *y;
   7  }

Bit Counting and clever loop condition

   1  
   2  unsigned bit_count(unsigned x) {
   3      unsigned n;
   4      for (n = 0; x; n++)
   5          x &= x-1;
   6      return n;
   7  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS