Writing vectors of bits to a file 
Author Message
 Writing vectors of bits to a file

Hi,

I would like to write a series of 1/0 vectors to a file. I would like
to write the vectors as bits to save space. (The file can contain up
to 500 million vectors, and the more compact format I use, the
better.) Currently my impementation writes *actual* integer 1's and
zeros.

Any pointers/suggestions on how to write bit vectors of 1/0's to disk
would be greatly appreciated.

Thanks!



Fri, 09 Dec 2005 10:43:24 GMT  
 Writing vectors of bits to a file
Hi Shaun,

 S> Any pointers/suggestions on how to write bit vectors of 1/0's to
 S> disk would be greatly appreciated.

Something like this may help?

/* variables */
int i,j;
int data[];
char c = 0;

/* go through the data */
for (i=0,j=0; i<data_size; i++) {

 if (data[i] == 1)   /* data is 1         */
   c |= 0x1;
 else                /* data is 0         */
   c |= 0x0;

 if (++j > 7) {      /* when char is full */
   fputc(fd,c);      /* write it to file  */
   j = 0;            /* reset j and c     */
   c = 0;
 }
 else {              /* otherwise shift c */
   c << 1;           /* to left 1-bit     */
 }

Quote:
}

if (j>0 && j<7) {    /* check for non-    */
  c << (7-j);        /* written data if   */
  fputc(fd,c);       /* any               */

Quote:
}

Best regards, Ali

 \    / _ |_ |. _  _  Ali Cinar

 340 N. 12th Street - Philadelphia, PA 19107 - 215/925-1800



Fri, 09 Dec 2005 07:44:47 GMT  
 Writing vectors of bits to a file
On Mon, 23 Jun 2003 00:44:47 +0100 (BST),

Quote:

>Hi Shaun,

> S> Any pointers/suggestions on how to write bit vectors of 1/0's to
> S> disk would be greatly appreciated.

>Something like this may help?

>/* variables */
>int i,j;
>int data[];

data needs to point at an array of 0's and 1's.
int *data = ....;

Quote:
>char c = 0;

You really want to be using
unsigned char c=0;

Quote:
>/* go through the data */
>for (i=0,j=0; i<data_size; i++) {

> if (data[i] == 1)   /* data is 1         */
>   c |= 0x1;
> else                /* data is 0         */
>   c |= 0x0;

these two lines are redundant.

Quote:
> if (++j > 7) {      /* when char is full */
>   fputc(fd,c);      /* write it to file  */

assume fd is a file open for writing.

fputc((int)c, fd);

Quote:
>   j = 0;            /* reset j and c     */
>   c = 0;
> }
> else {              /* otherwise shift c */
>   c << 1;           /* to left 1-bit     */

c <<= 1;

Quote:
> }
>}
>if (j>0 && j<7) {    /* check for non-    */
>  c << (7-j);        /* written data if   */

c <<= 7-j;

Quote:
>  fputc(fd,c);       /* any               */

fputc((int)c, fd);

Quote:
>}

You'll also need to store a count of the bits at the beginning of the
file so you can recover the last few.

Jim



Fri, 09 Dec 2005 14:31:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Writing bits to file in C

2. Writing bits to a file == possible?

3. Dividing 42 bits / 42 bits in segments of 8 bits

4. reading writing bits in C

5. Reading, Writing in bits

6. reading and writing bits?!?

7. Questions about writing bits.

8. Read and write bits

9. How to write a Vector Based C# Drawing Application

10. What precision I got with 53 bits of significand bits

11. IPAddress.Address 64-bits instead of 32-bits

12. Algorithm to convert from 24 bits to 8 bits

 

 
Powered by phpBB® Forum Software