BitField.Extensions

.NET Library For BitField & Enum Extensions


  Description
CREATED BY: Latency McLaughlin
UPDATED: 4/9/2024
FRAMEWORK: [net452], [netstandard2.0], [netstandard2.1] ([Latest])
LANGUAGE: [C#] preview
OUTPUT TYPE: Library [API]
SUPPORTS: [Visual Studio]
GFX SUBSYS: [None]
TAGS: [BitField BitFields C# Enum Enums Library]
STATUS .NET
LICENSE: License
VERSION: GitHub Release

Sample image



<h2>Introduction</h2>

This article demonstrates a simple use of bit fields as flags for Windows forms. Bit fields allow packaging of data into simple structures, and they are particularly useful when bandwidth, memory or data storage is at a premium. This might not appear to be an issue with modern day equipment or every day applications, but we can save up to 16 times more memory and storage when using bit fields instead of other value types such as boolean.

<h2>Background</h2>

Storage

Consider a boolean value in .NET:

bool bVal;

Boolean value data types are stored as 16-bit (2-byte) numbers that can only be true or false. Consider an unsigned 16-bit number which ranges from 0 to 65535:

Decimal        Hexidecimal    Binary
0              0x0000         0000000000000000
65535          0xffff         1111111111111111

When numeric data types are converted to boolean values, 0 becomes false and all other values become true. When Boolean values are converted to numeric types, false becomes 0 and true becomes -1 (using a signed number).

If we want to use a boolean value to represent a two-state value of a flag or setting in our program (true <=> on, false <=> off), then this would be stored as a 16-bit number.

Consider using a binary digit to represent the same two-state value: (1 <=> on, 0 <=> off):

Decimal        Hexidecimal    Binary
0              0x0000         0000000000000000
1              0x0001         0000000000000001

We can use this same 16-bit number to represent 16 unique settings by inspecting each digit and determining if it is 1/on or 0/off:

Decimal        Hexidecimal    Binary
1              0x0001         0000000000000001
2              0x0002         0000000000000010
4              0x0004         0000000000000100
8              0x0008         0000000000001000
16             0x0010         0000000000010000
32             0x0020         0000000000100000
64             0x0040         0000000001000000
128            0x0080         0000000010000000
256            0x0100         0000000100000000
512            0x0200         0000001000000000
1024           0x0400         0000010000000000
2048           0x0800         0000100000000000
4096           0x1000         0001000000000000
8192           0x2000         0010000000000000
16384          0x4000         0100000000000000
32768          0x8000         1000000000000000

With only 16 settings, this might not appear to be an issue and one would more than likely store the settings as Boolean values, however, storing a history of those settings can quickly add up, and saving space by a factor of 16 can make a difference. Ever tried loading a 100MB file into memory and then manipulating it? How about a 1.6GB file?

Understanding bit shifting <<

f1 = 0x01        // 0x01        1    00000001
f2 = f1 << 1,    // 0x02        2    00000010
f3 = f2 << 1,    // 0x04        4    00000100
f4 = f3 << 1,    // 0x08        8    00001000

Shifting to the Left or the Right.

There are two operators: