How to write bits (not bytes) to a file with c#, .net? I'm preety stuck with it.
Edit: i'm looking for a different way that just writing every 8 bits as a byte
views:
507answers:
3
A:
You will have to use bitshifts or binary arithmetic, as you can only write one byte at a time, not individual bits.
Aistina
2009-03-15 22:16:16
+6
A:
The smallest amount of data you can write at one time is a byte.
If you need to write individual bit-values. (Like for instance a binary format that requires a 1 bit flag, a 3 bit integer and a 4 bit integer); you would need to buffer the individual values in memory and write to the file when you have a whole byte to write. (For performance, it makes sense to buffer more and write larger chunks to the file).
driis
2009-03-15 22:17:15
unfortunately this is what i expected. i just wanted to avoid the problem with writing a number of bits that don't divide by 8 but i'll have to handle it
agnieszka
2009-03-15 22:22:44
+3
A:
- Accumulate the bits in a buffer (a single byte can qualify as a "buffer")
- When adding a bit, left-shift the buffer and put the new bit in the lowest position using OR
- Once the buffer is full, append it to the file
Jen
2009-03-15 22:18:53