Checking if a bit is set or not

C#.NetBit Manipulation

C# Problem Overview


How to check if a certain bit in a byte is set?

bool IsBitSet(Byte b,byte nPos)
{
   return .....;
}

C# Solutions


Solution 1 - C#

sounds a bit like homework, but:

bool IsBitSet(byte b, int pos)
{
   return (b & (1 << pos)) != 0;
}

pos 0 is least significant bit, pos 7 is most.

Solution 2 - C#

Based on Mario Fernandez's answer, I thought why not have it in my toolbox as a handy extension method not limited to datatype, so I hope it's OK to share it here:

/// <summary>
/// Returns whether the bit at the specified position is set.
/// </summary>
/// <typeparam name="T">Any integer type.</typeparam>
/// <param name="t">The value to check.</param>
/// <param name="pos">
/// The position of the bit to check, 0 refers to the least significant bit.
/// </param>
/// <returns>true if the specified bit is on, otherwise false.</returns>
public static bool IsBitSet<T>(this T t, int pos) where T : struct, IConvertible
{
 var value = t.ToInt64(CultureInfo.CurrentCulture);
 return (value & (1 << pos)) != 0;
}

Note: Do not use for performance critical operations, as this method always converts to long.

Solution 3 - C#

Equivalent to Mario F code, but shifting the byte instead of mask:

bool IsBitSet(byte b, int pos)
{
   return ((b >> pos) & 1) != 0;
}

Solution 4 - C#

Here is the solution in words.

Left shift an integer with initial value 1 n times and then do an AND with the original byte. If the result is non-zero, bit is Set otherwise not. :)

Solution 5 - C#

This also works (tested in .NET 4):

void Main()
{
    //0x05 = 101b
	Console.WriteLine(IsBitSet(0x05, 0)); //True
	Console.WriteLine(IsBitSet(0x05, 1)); //False
	Console.WriteLine(IsBitSet(0x05, 2)); //True
}

bool IsBitSet(byte b, byte nPos){
	return new BitArray(new[]{b})[nPos];
}

Solution 6 - C#

Right shift your input n bits down and mask with 1, then test whether you have 0 or 1.

Solution 7 - C#

something like

return ((0x1 << nPos) & b) != 0

Solution 8 - C#

To check the bits in a 16-bit word:

  Int16 WordVal = 16;
  for (int i = 0; i < 15; i++)
  {
     bitVal = (short) ((WordVal >> i) & 0x1);
     sL = String.Format("Bit #{0:d} = {1:d}", i, bitVal);
     Console.WriteLine(sL);
  }

Solution 9 - C#

x == (x | Math.Pow(2, y));

int x = 5;

x == (x | Math.Pow(2, 0)) //Bit 0 is ON
x == (x | Math.Pow(2, 1)) //Bit 1 is OFF
x == (x | Math.Pow(2, 2)) //Bit 2 is ON

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManjoorView Question on Stackoverflow
Solution 1 - C#Mario FView Answer on Stackoverflow
Solution 2 - C#Shimmy WeitzhandlerView Answer on Stackoverflow
Solution 3 - C#kaalusView Answer on Stackoverflow
Solution 4 - C#AamirView Answer on Stackoverflow
Solution 5 - C#Brian ChavezView Answer on Stackoverflow
Solution 6 - C#Mark ByersView Answer on Stackoverflow
Solution 7 - C#RedPaladinView Answer on Stackoverflow
Solution 8 - C#Jim LahmanView Answer on Stackoverflow
Solution 9 - C#Rafael TellesView Answer on Stackoverflow