Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing bit packed data using C#

Tags:

I'm working on a TCP based application that processes bitpacked messages, meaning: The messages transmitted/received are not byte aligned. For instance 3 bits represent field 1, where 19 bits may represent field 2. My question is, does anyone know of a C# library that can take a set of bytes and set/get an arbitrary range of bits within those bytes? I've seen & created similar utilities in C/C++ but I need a 100% C# solution and I don't want to re-invent the wheel again.

I've looked at the BitArray class, but it doesn't allow for referencing arbitrary ranges of bits.

like image 599
nathan Avatar asked Jul 17 '09 19:07

nathan


People also ask

Can C manipulate bits?

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a byte. C language is very efficient in manipulating bits.

What is a bitfield in C?

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared.

What is bit masking in C?

Bit masking is simply the process of storing data truly as bits, as opposed to storing it as chars/ints/floats. It is incredibly useful for storing certain types of data compactly and efficiently. The idea for bit masking is based on boolean logic.

What is nibble in C?

In computers and digital technology, a nibble (pronounced NIHB-uhl; sometimes spelled nybble) is four binary digits or half of an eight-bit byte. A nibble can be conveniently represented by one hexadecimal digit.


1 Answers

Check out this CodeProject solution - it is a open source managed BitStream class callable from C#.

like image 144
LBushkin Avatar answered Oct 04 '22 16:10

LBushkin