1

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 .

0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

can I do some bit wise operation like shifting?? I am not much clear. thanks by the way @Jon Skeet
@AmitNatural: Well yes, you can do bit-shifting - but what would you expect that to accomplish? What prompted you to ask that question?
my basic requirement was to add a single bit, as it is not possible I need to go for work around solution. in my exact code, there is a value ranged (0-255 [I have made this range to fit it in a byte and I can rage it to 0-127, 7bits number and make right most bit in that byte unused for this bit shifting operation.]). but as I said I am not clear that bit shifting on MemoryStream is possible or not! and if yes how can I use it
@AmitNatural: You don't bit shift on a MemoryStream, you just bit shift what you're writing to the stream. So if you've already got a value in the range 0-127 that you're writing as a byte, just add 0 for "date absent" of 128 for "date present". When reading, detect that with if ((x & 0x80) != 0) and byte value = x & 0x7f; to get the original value.
I got your point. thanks. My case is probably not possible. as I was expecting that If it is case of "date absent", MemoryStream will not be containing remaining 7 bits.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.