Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve written a .NET bitwise operations library as part of my projects (stuff ra

ID: 642127 • Letter: I

Question

I've written a .NET bitwise operations library as part of my projects (stuff ranging from get MSB set to some more complicated bitwise transformations) and I mean to release it as free software. I'm a bit confused about a design aspect of the library, though.

Many of the methods/transformations in the library come with different endianness. A simple example is a getBitAt method that regards index 0 as the least significant bit, or the most significant bit, depending on the version used.

In practice, I've found that using separate functions for different endianness results in much more comprehensible and reusable code than assuming all operations are little-endian or something.

I'm really stumped regarding how best to package the library.

    Should I have methods that have LE and BE versions take an enum parameter in their signature, e.g. Endianness.Little, Endianness.Big?
    Should I have different static classes with identically named methods? such as MSB.GetBit and LSB.GetBit

On a much wider note, is there a standard I could use in cases like this? Some guide? Is my design issue trivial? I have a perfectionist bent, and I sometimes get stuck on tricky design issues like this...

Note: I've sort of realized I'm using endianness somewhat colloquially to refer to the order/place value of digital component parts (be they bits, bytes, or words) in a larger whole, in any setting. I'm not talking about machine-level endianness or serial transmission endianness. Just about place-value semantics in general. So there isn't a context of targeting different machines/transmission techniques or something.

Explanation / Answer

As in most instances of the design problem, the solution should be based on intended usage and context, instead of any alleged absolute.

You need to establish your requirements. What do you use the library for? What is your target audience for this library?

If the intended usage is to mix little-endian logic together with big-endian logic, then it's beneficial to add an extra argument for each method, such as Endianness.Little, just like you suggested. I wouldn't suggest a clone for each method, as that would grow rather quickly, and with today's IDEs and auto-completion it would be a visual disturbance, and also would have a (probably negligible) impact on compile time.

However, if you intend to use the library for different platforms, but on each one pick one scheme, then you're probably better off with passing an Endianness strategy to the constructor of a class that has higher level methods.

If you want to get the best of both worlds, at the expense of simplicity, you can pass a strategy in a constructor, and accept an optional strategy in each method, which defaults to the one provided in the constructor.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote