1

I have a class a binary masque:

  public class Masque
{
    public bool MasquerAdresse { get; set; }

    public bool MasquerCpVille { get; set; }

    public bool MasquerTelephone { get; set; }

    public bool MasquerFax { get; set; }

    public bool MasquerEmail { get; set; }
    public bool MasquerNom { get; set; }
}

and in the database I have a binary masque field: when i have the value 1=> MasquerAdresse is true, 2 =>MasquerCpVille is true, 4=> MasquerTelephone is true 3=> MasquerAdresse and MasquerCpVille are true etc..., which is the best methode to decode this binary masque in c#

user1428798
  • 1,534
  • 3
  • 24
  • 50
  • What are you getting the value as? As there are a couple ways to do this like [BitArray](https://msdn.microsoft.com/en-us/library/system.collections.bitarray(v=vs.110).aspx) what you are working with – jrbeverly May 03 '16 at 06:44
  • Do you know [Flags](https://msdn.microsoft.com/en-us/library/system.flagsattribute(v=vs.110).aspx) for enum and [Enum.HasFlag](https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx) to check for a set bit? – Sebastian Schumann May 03 '16 at 06:46
  • 1
    As @Toxantron wrote in the answer below, flag enums would be more appropriate for this (since they *are* basically integer flags) and there is no need to have multiple bool properties. Keep in mind that each `bool` field takes 1 byte in .NET, so if you can pack these flags in a single `Int32` enum (4 bytes), it will also be more efficient to pass around. **On the other hand**, note that enums cannot be extended (inherited from) like classes, which is why people sometimes actually [use your approach](http://stackoverflow.com/q/757684/69809), or other ways to allow for future extensions. – vgru May 03 '16 at 07:13
  • 1
    @Groo added part of your comment to my answer. Thanks for mentioning the memory difference. – Toxantron May 03 '16 at 07:20
  • "in the database I have a binary masque field". This *will* come back to bite you. Encoding multiple values into a single one within the database will make it far more difficult to perform ad-hoc querying in the future. – Damien_The_Unbeliever May 03 '16 at 07:26

1 Answers1

4

As others already pointed out in C# we usually use Flag Enums and Enum.HasFlag() to do this. As @Groo added in his comment this is also more memory efficient since every bool takes up an entire byte. Using enums it would look like this:

[Flags]
public enum Masque
{
    MasquerAdresse = 1,

    MasquerCpVille = 2,

    MasquerTelephone = 4,

    MasquerFax = 8,

    MasquerEmail = 16

    MasquerNom = 32
}

var masque = Masque.MasquerAdresse | Masque.MasquerTelephone;
var fromInt = (Masque) 5;
var trueResult = masque.HasFlag(Masque.MasquerTelephone);

If you are however determined to use a class it would look like this:

public class Masque
{
    public bool MasquerAdresse { get; set; }
    public bool MasquerCpVille { get; set; }
    public bool MasquerTelephone { get; set; }
    public bool MasquerFax { get; set; }
    public bool MasquerEmail { get; set; }
    public bool MasquerNom { get; set; }

    public static Masque FromBits(int bits)
    {
        return new Masque
        {
            MasquerAdresse = (bits & 1) > 0,
            MasquerCpVille = (bits & 2) > 0,
            ...
        };
    }
}

Using the binary & it will apply the bit mask which either returns the value or 0. You can either compare the result with the mask (bits & 2) == 2 or simply check for > 0.

Toxantron
  • 2,218
  • 12
  • 23