Note: this post is published also at OzCode's blog.

There are 10 types of developers; those who work in a higher level and depend on abstractions, and those who work with the bits and bytes alongside the bare metal. As a C# developer, I am one of the former, and I usually try to abstract everything I do. A few weeks ago, I figured out that I can use some of these abstraction techniques to solve "low-level" challenges too. To make a long story short, I was able to utilize LINQ in order to eliminate a rather very complicated bitwise operation.

# The mission

A framework for communicating with FPGA cards (Field-Programmable Gate Array), where it sends and receives messages to and from those cards. One of the framework's abilities is to serialize and deserialize messages that were sent or received from a FPGA card to and from byte arrays, according to an application-specific protocol. The structure and the parts each message is composed of vary, and are only determined at runtime.

# This looks easy enough

Each message is composed of�different parts with different lengths. By serializing a message, we concatenate each part's value according to its length in bits. The following is an example of a message that is composed of�3 parts.
 Message Part 0 Part 1 Part 2 001 1010 00111110000 3 Bits 4 Bits 11 Bits
In this example, "Part 0" has the value of 0x1, "Part 1" has the value of "0xA" and "Part 2" has the value of "0x1F0". The framework doesn't really care about the content of the messages, but rather cares for their structure. When deserializing a message, we only need the structure or the schema of the message. For each message-part we need to specify its name, index (its order in the message) and length in bits. I have therefore�introduced the following class:
```public class LayoutPart
{
public string Name { get; set; }
public int Index { get; set; }
public int BitCount { get; set; }

public LayoutPart(string name, int index, int bitCount)
{
Name = name;
Index = index;
BitCount = bitCount;
}
}
```
When serializing a message, each message-part has to specify four properties. The three properties of LayoutPart, in addition to the Value of that part. Here are the classes I have added: In reality, the content's type wasn't known beforehand, and it surly wasn't always an int, but I decided to use an int in all of my examples to keep them simple. The relevant API is really simple, and it has two methods; Serialize and Deserialize:

# Bitwise operations: This is really ugly

So far so good :D

After defining the API, I went on and started implementing it. The following is an example of serializing the message from the example above
Name BitCount Value Value in Bits Value in Bytes
Part 0 3 1 001 00000001
Part 1 4 0xA 1010 00001010
Part 2 11 0x1F0 00111110000 00000001 11110000
The byte array should look like this
Byte 2 Byte 1 Byte 0
00000000 11111000 01010001
The first byte is composed of�Part 0 (3 bits), Part 1 (4 bits) and the first bit of Part 2 (11 bits). The second byte is composed of bits 1-8 of Part 2, and the third byte is composed of bits 9-10 of Part 2 with 6 more trailing zeros to complete a byte of 8 bits. I approached it using the standard-naive way: bitwise operations. I needed the ability to extract bits from different bytes and bit indexes, then concatenate them together for composing a byte array. Assuming we are working with System.Int32, how would I extract the n first bits? Easy enough! And to extract the n last bits? What about extracting n bits that are in the "middle"? This is starting to be more complicated. I did a little refactoring and defined them in methods. This looks a little bit (pun not intended) better. I had to use System.Int64 and system.UInt32 for overflow/underflow safety when performing bitwise operations. Most importantly, this is an over simplified version of what I needed to do. I also had to consider:
• Concatenating the results
• Messaging parts of length exceeding 8 bytes (sizeof(long)). Current implementation won't work
• Transfer the results into a byte array
• Implementing deserialization, which is even harder
I went on and finished the rest of the implementation. It took me a full day of work (implementing + testing), and it wasn't easy, at least not as I thought it would be. I kept telling myself that there has to be a better way. The code wasn't maintainable, even with the 90% code coverage I had of tests. Not by the version of me from next month or even from next week, and of course not by another dev from my team. This code will become an unmaintainable, 'don't touch', legacy code as soon as I'll push the changes to the repo and mark the task as 'Done'. This code is ugly.

# Oh, much better

I took a step back and tackled the problem from a different angle. I figured that my goal is pretty simple; I have a collection of values and all I have to do for the Serialize method is to:
1. Project each message part into bits
2. Concatenate the bits of all values
3. Project the result into a byte array
And all I have to do for the deserialize method is to:
1. Project the byte array into bits.
2. Slice them according to the length of each message part
3. Project each slice into the corresponding message part object
I quickly thought about LINQ! Isn't this one of the scenarios that are easily solved by LINQ? I went on and explored this idea further. I decided to use the Type-Safe Enum Pattern to represent a Bit (Bit.cs). I also used some LINQ operations from Jon Skeet's MoreLinq library. For the Serialize method I started by writing the following extension methods: The following are the steps for the Serialize method:
1. Project each value into bits, according to the BitCount property of its message-part object
1. Project the value into bytes
2. Project the bytes into bits
2. Group the bits into a group of 8-bit each
3. Project each group into a byte
4. Project the IEnumerable of bytes into a byte array
(Did you notice the bug? I'll discuss it later) For implementing the deserialize method, I created the following extension methods: The following are the steps for the deserialize method:
1. Project the byte array into IEnumerable of bits
2. Slice the bits according to each message-part length and order
3. Project each slice into 4 bytes (sizeof(System.Int32))
4. Project each 4 bytes into a System.Int32
Looks better, Eh?

# Testing the implementation

It isn't always easy to debug LINQ operations, thus in order to debug, test and visualize my examples, I used OzCode which is an awesome debugging extension for Visual Studio. I installed the EAP (Early Access Program) edition,�which includes amazing LINQ debugging capabilities, and put it to work. This is the code according to the example above I used OzCode's LINQ analyser for visualizing and tracking the LINQ operations. Here we can see what the actual bits are, and their order in the concatenated message-parts.
But according to the BitCount property of Part 0, its value should be projected only to 3 bits. Fixing the bug: I added Pad and Take to be sure I project exactly the needed count of bits.
This looks much better. Here we can see the value of each byte:
Testing the Deserialize method, we can see the value of each message part:

# Summary

Even though actions that require bitwise operations are usually considered "low-level", I was able to use LINQ, which is considered a "high-level" API, to perform the same thing. This kind of abstraction saved me time and effort, and made my code more maintainable, readable and clean. I suggest to use abstractions whenever you can, and whenever it make sense. What I really learnt though, is that we can use skills we acquire in one domain to solve challenges in another. There is a saying that polyglot programmers write better code than those who specializes only in one language. I think it is yet another example of the same idea - each skill you acquire, will make you better at what you do, regardless how unrelated that skill might look like at that time.