In order to check hihest bit is on I use code:
byte data = 0x88 ;
bool is_on= ((data & 0x80) == 0x80);
How to do this in more elegant and C# way ?
In order to check hihest bit is on I use code:
byte data = 0x88 ;
bool is_on= ((data & 0x80) == 0x80);
How to do this in more elegant and C# way ?
public static bool Get(byte b, int position)
{
if (position > 7)
throw new ArgumentException();
return ((b & (byte)(1 << position)) != 0);
}
Since you're checking a bit, the only thing I might do is write out the literal so you can see the individual bits, like this:
int mask = 0b1000_0000;
byte data = 0x88 ;
bool is_on= ((data & mask) == mask);
I might further abstract this as a method:
public static bool MatchesMask(this int data, int mask)
{
return (data&mask)==mask;
}
So I could call it like this:
int mask = 0b1000_0000;
byte data = 0x88 ;
bool is_on = data.MatchesMask(mask);
If you are dealing with bit setting and getting consider some auxiliary classes to help you. Look at the Flag
structure below
You initialize a Flag
with a mask Flag flag = 0x20
, or set a specific bit Flag flag = Flag.Bit(5)
.
You poll a bit with flag.Get(value)
and you set a bit with value = flag.Set(value);
to set to 1, and value = flag.Set(value, false);
to set it 0.
10110011 & 00100000 = True
10010011 & 00100000 = False
public struct Flag
{
readonly byte mask;
public Flag(byte mask)
{
this.mask=mask;
}
public byte Mask => this.mask;
public static Flag Bit(int position)
=> new Flag((byte)(1<<position));
public static implicit operator byte(Flag bit) => bit.mask;
public static implicit operator Flag(byte mask) => new Flag(mask);
public bool Get(byte value) => (value & mask)!=0;
public byte Set(byte value, bool bit = true)
{
if (bit)
{
return (byte)(value | mask);
}
else
{
return (byte)(value & ~mask);
}
}
}
static class Program
{
public static string Bin(this byte data)
=> Convert.ToString(data, 2).PadLeft(8, '0');
static void Main(string[] args)
{
byte value = 0xB3;
Flag flag = 0x20;
// or alernativly
// var flag = Flag.Bit(6); // mask = 0b0010000
Console.WriteLine($"{Bin(value)} & {Bin(flag.Mask)} = {flag.Get(value)}");
// not set the bit to 0
value = flag.Set(value, false);
Console.WriteLine($"{Bin(value)} & {Bin(flag.Mask)} = {flag.Get(value)}");
}
}