Sprawdzanie czy bit jest ustawiony czy nie

Jak sprawdzić, czy określony bit w bajcie jest ustawiony?

bool IsBitSet(Byte b,byte nPos)
{
   return .....;
}
Author: abatishchev, 2010-03-12

9 answers

Brzmi trochę jak praca domowa, ale:

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

Pos 0 jest najmniej znaczącym bitem, pos 7 jest najbardziej.

 125
Author: Mario F,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-12 09:50:00

Bazując na Mario Fernandez ' s answer , pomyślałem, dlaczego nie mieć go w moim toolbox jako przydatnej metody rozszerzenia Nie ograniczającej się do datatype, więc mam nadzieję, że jest w porządku, aby podzielić się nim tutaj:

/// <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;
}
 10
Author: Shimmy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:11

Oto rozwiązanie w słowach.

Przesuń w lewo liczbę całkowitą z wartością początkową 1 N razy, a następnie wykonaj an i z oryginalnym bajtem. Jeśli wynik jest niezerowy, bit jest ustawiany w przeciwnym razie nie. :)

 5
Author: Aamir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-12 10:00:03

To również działa (testowane w. 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];
}
 5
Author: Brian Chavez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-14 02:35:50

Przesuń w prawo wejściowe N bitów w dół i zamaskuj za pomocą 1, a następnie sprawdź, czy masz 0 Czy 1.

 4
Author: Mark Byers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-12 09:50:10
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;
 2
Author: Rafael Telles,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-28 19:57:54

Odpowiednik kodu Mario F, ale przesunięcie bajtu zamiast maski:

bool IsBitSet(byte b, int pos)
{
   return ((b >> pos) & 1) != 0;
}
 1
Author: kaalus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-17 18:35:34

Coś jak

return ((0x1 << nPos) & b) != 0
 0
Author: RedPaladin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-27 07:36:36

Aby sprawdzić bity w 16-bitowym słowie:

  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);
  }
 0
Author: Jim Lahman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-09 19:57:44