Skip to content

Bitmask compatible with JavaScript big-bit-mask - the bitmask serializable into a base64-like, url-safe string.

License

Notifications You must be signed in to change notification settings

ASolomatin/BigBitMask.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BigBitMask.NET

NuGet NuGet downloads Travis-CI Coverage Status NuGet Package GitHub


When bits is not enough ...

This library implements a .NET, fully compatible, version of big-bit-mask - the bitmask serializable into a base64-like, url-safe string.

Other platform compatibility

Platform Repository Package
JavaScript / TypeScript big-bit-mask NPM
PHP php-big-bit-mask Packagist

Install

> dotnet add package BigBitMask.NET

Usage

Namespace

using BigBitMask.NET;

What next?

Now we can create new empty bitmask

var bitmask = new BitMask();

or load it from string

var bitmask = new BitMask("CE3fG_gE-56");

//Let's see what inside now
var content = "";
for (var i = 0; i < 11 * 6; i++) // Each character contains 6 bits, as in base64
    content += bitmask[i] ? "1" : "0";
Console.WriteLine(content);

output: 010000001000111011111110011000111111000001001000011111100111010111

Then we can change some bits and get back our string representation

bitmask[65] = false;
bitmask[64] = false;
bitmask[63] = false;
bitmask[61] = false;

bitmask[19] = false;
bitmask[5] = true;

Console.WriteLine(bitmask.ToString());

output: iE3dG_gE-5

But what if I want to have a named flags?

You can extend BitMask class with your model:

class MyCoolCheckboxes : BitMask
{
    public const int CHECKBOX_0 = 0;
    public const int CHECKBOX_1 = 1;
    public const int CHECKBOX_2 = 2;
    public const int CHECKBOX_3 = 3;
    public const int CHECKBOX_4 = 4;
    public const int CHECKBOX_5 = 5;
    public const int CHECKBOX_6 = 6;
    public const int CHECKBOX_7 = 7;
    public const int CHECKBOX_8 = 8;
    public const int CHECKBOX_9 = 9;

    public bool Checkbox0 { get => this[CHECKBOX_0]; set => this[CHECKBOX_0] = value; }
    public bool Checkbox1 { get => this[CHECKBOX_1]; set => this[CHECKBOX_1] = value; }
    public bool Checkbox2 { get => this[CHECKBOX_2]; set => this[CHECKBOX_2] = value; }
    public bool Checkbox3 { get => this[CHECKBOX_3]; set => this[CHECKBOX_3] = value; }
    public bool Checkbox4 { get => this[CHECKBOX_4]; set => this[CHECKBOX_4] = value; }
    public bool Checkbox5 { get => this[CHECKBOX_5]; set => this[CHECKBOX_5] = value; }
    public bool Checkbox6 { get => this[CHECKBOX_6]; set => this[CHECKBOX_6] = value; }
    public bool Checkbox7 { get => this[CHECKBOX_7]; set => this[CHECKBOX_7] = value; }
    public bool Checkbox8 { get => this[CHECKBOX_8]; set => this[CHECKBOX_8] = value; }
    public bool Checkbox9 { get => this[CHECKBOX_9]; set => this[CHECKBOX_9] = value; }
}

or

class MyCoolCheckboxes : BitMask
{
    public enum Checkboxes
    {
        CHECKBOX_0,
        CHECKBOX_1,
        CHECKBOX_2,
        CHECKBOX_3,
        CHECKBOX_4,
        CHECKBOX_5,
        CHECKBOX_6,
        CHECKBOX_7,
        CHECKBOX_8,
        CHECKBOX_9,
    }

    public bool this[Checkboxes checkboxId] { get => this[(int)checkboxId]; set => this[(int)checkboxId] = value; }

    public bool Checkbox0 { get => this[Checkboxes.CHECKBOX_0]; set => this[Checkboxes.CHECKBOX_0] = value; }
    public bool Checkbox1 { get => this[Checkboxes.CHECKBOX_1]; set => this[Checkboxes.CHECKBOX_1] = value; }
    public bool Checkbox2 { get => this[Checkboxes.CHECKBOX_2]; set => this[Checkboxes.CHECKBOX_2] = value; }
    public bool Checkbox3 { get => this[Checkboxes.CHECKBOX_3]; set => this[Checkboxes.CHECKBOX_3] = value; }
    public bool Checkbox4 { get => this[Checkboxes.CHECKBOX_4]; set => this[Checkboxes.CHECKBOX_4] = value; }
    public bool Checkbox5 { get => this[Checkboxes.CHECKBOX_5]; set => this[Checkboxes.CHECKBOX_5] = value; }
    public bool Checkbox6 { get => this[Checkboxes.CHECKBOX_6]; set => this[Checkboxes.CHECKBOX_6] = value; }
    public bool Checkbox7 { get => this[Checkboxes.CHECKBOX_7]; set => this[Checkboxes.CHECKBOX_7] = value; }
    public bool Checkbox8 { get => this[Checkboxes.CHECKBOX_8]; set => this[Checkboxes.CHECKBOX_8] = value; }
    public bool Checkbox9 { get => this[Checkboxes.CHECKBOX_9]; set => this[Checkboxes.CHECKBOX_9] = value; }
}

and use it

var checkboxes = new MyCoolCheckboxes();
checkboxes.Checkbox5 = true;
checkboxes.Checkbox7 = true;
checkboxes.Checkbox8 = true;

Console.WriteLine(checkboxes.ToString());

output: gG


License

MIT

Copyright (C) 2020 Aleksej Solomatin

About

Bitmask compatible with JavaScript big-bit-mask - the bitmask serializable into a base64-like, url-safe string.

Resources

License

Stars

Watchers

Forks

Packages

No packages published