Can't Copy and Paste this?

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

 

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

//     

//INCLUDE files for :Read/write bit file

//     s

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

//     

/* bittops.h +++Date last modified: 05-Jul-1997 */

/*

** Macros and prototypes for bit operations

**

** public domain for SNIPPETS by:

**Scott Dudley

**Auke Reitsma

**Ratko Tomic

**Aare Tali

**J. Blauth

**Bruce Wedding

**Bob Stout

*/

#ifndef BITOPS__H

#define BITOPS__H

#include 

#include  /* for size_t*/

#include  /* for CHAR_BIT */

#include "sniptype.h"/* for TOBOOL() */

#include "extkword.h"/* for CDECL*/

/*

** Macros to manipulate bits in any integral data type.

*/

#define BitSet(arg,posn) ((arg) | (1L << (posn)))

#define BitClr(arg,posn) ((arg) & ~(1L << (posn)))

#define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))

#define BitTst(arg,posn) TOBOOL((arg) & (1L << (posn)))

/*

** Macros to manipulate bits in an array of char.

** These macros assume CHAR_BIT is one of either 8, 16, or 32.

*/

#define MASK CHAR_BIT-1

#define SHIFT ((CHAR_BIT==8)?3:(CHAR_BIT==16)?4:8)

#define BitOff(a,x) ((void)((a)[(x)>>SHIFT] &= ~(1 << ((x)&MASK))))

#define BitOn(a,x)((void)((a)[(x)>>SHIFT] |= (1 << ((x)&MASK))))

#define BitFlip(a,x) ((void)((a)[(x)>>SHIFT] ^= (1 << ((x)&MASK))))

#define IsBit(a,x)((a)[(x)>>SHIFT]&(1 << ((x)&MASK)))

/*

** BITARRAY.C

*/

char *alloc_bit_array(size_t bits);

intgetbit(char *set, int number);

void setbit(char *set, int number, int value);

void flipbit(char *set, int number);

/*

** BITFILES.C

*/

    typedef struct {

    FILE * file;/* for stream I/O*/

    charrbuf;/* read bit buffer */

    charrcnt;/* read bit count*/

    charwbuf;/* write bit buffer */

    charwcnt;/* write bit count */

} bfile;

bfile * bfopen(char *name, char *mode);

int bfread(bfile *bf);

voidbfwrite(int bit, bfile *bf);

voidbfclose(bfile *bf);

/*

** BITSTRNG.C

*/

void bitstring(char *str, long byze, int biz, int strwid);

/*

** BSTR_I.C

*/

unsigned int bstr_i(char *cptr);

/*

** BITCNT_1.C

*/

int CDECL bit_count(long x);

/*

** BITCNT_2.C

*/

int CDECL bitcount(long i);

/*

** BITCNT_3.C

*/

int CDECL ntbl_bitcount(long int x);

int CDECL BW_btbl_bitcount(long int x);

int CDECL AR_btbl_bitcount(long int x);

/*

** BITCNT_4.C

*/

int CDECL ntbl_bitcnt(long x);

int CDECL btbl_bitcnt(long x);

#endif /* BITOPS__H */

 

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 */

/*

** BITFILES.C - reading/writing bit files

**

** public domain by Aare Tali

*/

#include 

#include "bitops.h"

bfile *bfopen(char *name, char *mode)

    {

    bfile * bf;

    bf = malloc(sizeof(bfile));

    if (NULL == bf)

    return NULL;

    bf->file = fopen(name, mode);

    if (NULL == bf->file)

        {

        free(bf);

        return NULL;

    }

    bf->rcnt = 0;

    bf->wcnt = 0;

    return bf;

}

int bfread(bfile *bf)

    {

    if (0 == bf->rcnt) /* read new byte */

        {

        bf->rbuf = (char)fgetc(bf->file);

        bf->rcnt = 8;

    }

    bf->rcnt--;

    return (bf->rbuf & (1 << bf->rcnt)) != 0;

}

void bfwrite(int bit, bfile *bf)

    {

    if (8 == bf->wcnt) /* write full byte */

        {

        fputc(bf->wbuf, bf->file);

        bf->wcnt = 0;

    }

    bf->wcnt++;

    bf->wbuf <<= 1;

    bf->wbuf |= bit & 1;

}

void bfclose(bfile *bf)

    {

    fclose(bf->file);

    free(bf);

}

#ifdef TEST

void test1(void)

    {

    bfile *out;

    bfile *in;

    FILE *in1;

    FILE *in2;

    in = bfopen("bitfiles.c", "rb");

    out = bfopen("bitfiles.cc", "wb");

    if ((NULL == in) || (NULL == out))

        {

        printf("Can't open/create test files\n");

        exit(1);

    }

    while (!feof(in->file))

    bfwrite(bfread(in), out);

    bfclose(in);

    bfclose(out);

    in1 = fopen("bitfiles.c", "rb");

    in2 = fopen("bitfiles.cc", "rb");

    if ((NULL == in1) || (NULL == in2))

        {

        printf("Can't open test files for verifying\n");

        exit(1);

    }

    while (!feof(in1) && !feof(in2))

        {

        if (fgetc(in1) != fgetc(in2))

            {

            printf("Files not identical, copy failed!\n");

            exit(1);

        }

    }

    if (!feof(in1) || !feof(in2))

        {

        printf("Not same size, copy failed!\n");

        exit(1);

    }

    fclose(in1);

    fclose(in2);

}

void test2(void)

    {

    FILE *in1;

    bfile *in2;

    intch;

    in1 = fopen("bitfiles.c", "rb");

    in2 = bfopen("bitfiles.cc", "rb");

    if ((NULL == in1) || (NULL == in2))

        {

        printf("Can't open test files\n");

        exit(1);

    }

    while (!feof(in1) && !feof(in2->file))

        {

        ch = fgetc(in1);

        if (ch < ' ')

        ch = '.';

        printf(" '%c' ", ch);

        for (ch = 0; ch < 8; ch++)

        printf("%c", "01"[bfread(in2)]);

        printf("");

    }

    fclose(in1);

    bfclose(in2);

}

main()

    {

    test1();

    test2();

    return 0;

}

#endif /* TEST */