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

void XORSwap(void *x, void *y)
{
   *x ^= *y;
   *y ^= *x;
   *x ^= *y;
}

Bit Counting and clever loop condition

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