Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :Compute parity for 

//     any integral type

//**************************************

//     

/* +++Date last modified: 05-Jul-1997 */

/*

** RND_DIV.C - Rounded integer division

**

** public domain - suggested by Dave Hansen in comp.lang.c

*/

#include 

int round_div(int n, int d)

    {

    div_t res = div(n,d);

    div_t rnd = div(res.rem, (abs(d)+1)/2 );

    return res.quot + rnd.quot;

}

long round_ldiv(long n, long d)

    {

    ldiv_t res = ldiv(n,d);

    ldiv_t rnd = ldiv(res.rem, (abs(d)+1)/2 );

    return res.quot + rnd.quot;

}

#ifdef TEST

#include 

#include 

main(int argc, char *argv[])

    {

    long n, d, q;

    if (argc != 3)

        {

        puts("Usage: RND_DIV n d\n");

        puts("Returns n/d rounded to nearest integer");

        return -1;

    }

    n = atol(argv[1]);

    d = atol(argv[2]);

    if (n > INT_MAX || d > INT_MAX)

        {

        q = round_ldiv(n, d);

        printf("round_ldiv(%ld, %ld) = %ld\n", n, d, q);

    }

    else

        {

        q = (long)round_div((int)n, (int)d);

        printf("round_div(%ld, %ld) = %ld\n", n, d, q);

    }

    return 0;

}

#endif /* TEST */

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/* +++Date last modified: 05-Jul-1997 */

/*

** PARITY.C - Computes even or odd parity for various integral types

**

** public domain demo by Bob Stout

*/

#include "parity.h"

unsigned parity32(unsigned long x, Parity_T even)

    {

    x = x ^ (x >> 16);

    x = x ^ (x >> 8);

    x = x ^ (x >> 4);

    x = x ^ (x >> 2);

    x = x ^ (x >> 1);

    return ((unsigned)(x & 1)) ^ even;

}

unsigned parity16(unsigned short x, Parity_T even)

    {

    x = x ^ (x >> 8);

    x = x ^ (x >> 4);

    x = x ^ (x >> 2);

    x = x ^ (x >> 1);

    return ((unsigned)(x & 1)) ^ even;

}

unsigned parity8(unsigned char x, Parity_T even)

    {

    x = x ^ (x >> 4);

    x = x ^ (x >> 2);

    x = x ^ (x >> 1);

    return ((unsigned)(x & 1)) ^ even;

}

unsigned parity64(void *x, Parity_T even)

    {

    union longlong *val64 = (union longlong *)x;

    return (parity32(val64->lo, even) ^ parity32(val64->hi, even));

}

#ifdef TEST

#include 

#include 

main(int argc, char *argv[])

    {

    while (--argc)

        {

        unsigned long n = strtoul(*(++argv), NULL, 10);

        printf("Even parity of %ld = %d\n", n, parity32(n, Even_));

        printf("Odd parity of %ld = %d\n\n", n, parity32(n, Odd_));

    }

}

#endif