0

In order to reduce the data size over network, i would like to write only enough bits to network, that can hold the value. For example, if 40 bits can hold the value, i want to write 40 bits to the stream and not 64 bits. Or if the value can be stored in 3 bits, i would simply like to write 3 bits to the binary stream and not 8 bits, with 5 bits as 0.

My question is how do i write non aligned data to a binary stream in C++ ?

6
  • with great care and difficulty... how does the receiver know how to decode this information? Commented Jan 17, 2012 at 0:24
  • the receiver can know how many bits to parse by size, so by publishing some spec like size:bits. Commented Jan 17, 2012 at 0:28
  • in that case, then bit shifting is the way (as was the answer to your previous question!) you simply need to maintain the order of bytes in the stream, and shift your bits appropriately, bit fields are not portable (unless you can guarantee a homogeneous environment) Commented Jan 17, 2012 at 0:29
  • Nim, can you please point me to some resource which explains bit stuffing to reduce network traffic? Commented Jan 17, 2012 at 0:30
  • not aware of any such resource, you simply need to understand how bitwise operations work (and, or, left/right shift etc.) are you working to a protocol, if so, they will spec out how the bits are laid out in the byte stream, else you need to define this. It's quite trivial - for example, look at how the tcp (or udp) header is laid out in a frame, you'll see how certain groupings of bits are treated. Commented Jan 17, 2012 at 0:33

2 Answers 2

2

The stream works with bytes, not bits, so you'll have to work with multiples of 8 bits. You can write 40 bits to the stream because that's exactly 5 bytes.

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

4 Comments

not necessarily a concern, there are protocols which rely on bit stuffing to reduce network traffic, however they tend to require lots of logic to decode/encode...
Is the fact that stream works with bytes and not bits, documented somewhere as a standard?
For C++ the stream is still part of the virtual machine, and thus follows its memory model. In the spec under 1.7 is where it says each byte has an address. Nothing below a byte has an address. IP protocols are also defined strictly with series of octets (bytes), thus no independent bits.
There's a reason why IP uses "octets". Languages such as C and C++ do not define a byte as 8 bits. It would be legal (but extremely uncommon) for a byte to have 40 bits. (9 is more common, though).
0

You are inventing your own compression scheme and will almost certainly do worse than the experts have done.

Your network may also already doing compression so you might be doing work that is already being done.

Your question is sorely lacking in detail that makes a better answer impossible.

Comments

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.