adding single bit in MemoryStream using BinaryWriter

Currently I am working to develop a MemoryStream using BinaryWriter.

I have several types (i.e. datatype) of data which I am putting in MemoryStream one by one.

and for sake of dynamicity, I need to decide whether some records should be inserted in MemoryStream or not and while reading this MemoryStream it should read accordingly.

for example suppose I'm inserting records of income so format of records should be:

byte sr_No; double grandTotal;

but as in most off the cases sr_No will be in a row, unless holydays. so I should not put sr_No portion of every record. Instead I am planning for a single bit 0 for sr_No absent and 1 for date present.

so basically what I'm thinking to have a 1 bit which will show if upcoming byte(as sr_No is byte) is containing sr_No or directly grandTotal (as sr_No been skipped) like this

sr_No present situation :

1[1 byte of sr_No][4 bytes of grandTotal]

sr_No absent situation :

0[4 bytes of grandTotal]

but it is not possible, so can I make bit wise shifting on MemoryStream (may be also not possible) to have one bit header.

or there may any other way to achieve it .

Jon Skeet
people
quotationmark

so my question how can I write and read (using BinaryReader) single bit to/from MemoryStream ?

You can't. The smallest "unit" of data in a stream is a byte. If you've got some other byte of which you're only using 7 bits, you could include the extra bit within that byte, but otherwise you just need to write a whole extra byte to indicate what else is present.

people

See more on this question at Stackoverflow