11

In C#, I have a 32 bit value which I am storing in an int. I need to see if a particular bit is set. The bit I need is 0x00010000.

I came up with this solution:

Here is what I am looking for:

Hex:       0    0    0    1     0    0    0    0    0 
Binary   0000|0000|0000|0001|0000|0000|0000|0000|0000

So I right bit shift 16, which would give me:

Hex:       0    0    0    0     0    0    0    0    1
Binary   0000|0000|0000|0000|0000|0000|0000|0000|0001

I then bit shift left 3, which would give me:

Hex:       0    0    0    0     0    0    0    0   8 
Binary   0000|0000|0000|0000|0000|0000|0000|0000|1000

I then case my 32 bit value to a byte, and see if it equals 8.

So my code would be something like this:

int value = 0x102F1032;
value = value >> 16;
byte bits = (byte)value << 3;
bits == 8 ? true : false;

Is there a simpler way to check if a particular bit is set without all the shifting?

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
user489041
  • 27,916
  • 55
  • 135
  • 204
  • 1
    You just need to use the bitwise & operator for this. – Paul Sasik Nov 08 '11 at 16:50
  • 2
    cant you just write (number & 0x00010000 ) != 0 ? – Felice Pollano Nov 08 '11 at 16:51
  • 3
    Just out of curiosity, even though shifting isn't the best way of doing this, why didn't you just check if the result was 1 after the first shift? i.e. why shift back left 3? – Ray Nov 08 '11 at 16:56
  • @Ray Yes odd logic to shift right by a certain amount of places then immediately shift left by another amount... – El Ronnoco Nov 08 '11 at 16:59
  • @Ray I did it that way because any of the other bits could be set, I did the shift to bring in 0's so that I could be sure that no other bits were set. If other bits were set, I wouldnt know what to compare it to. If I do it that way, I am sure that it will be 8. – user489041 Nov 08 '11 at 16:59
  • 1
    @user489041 but the bit to the left of your tested bit could still be set. – Ray Nov 08 '11 at 17:09
  • possible duplicate of [C# :: Checking if a bit is set or not](http://stackoverflow.com/questions/2431732/c-sharp-checking-if-a-bit-is-set-or-not) – Ray Nov 08 '11 at 17:10
  • @Ray I thought if I casted it to a byte, then the bits to the left would be dropped off? But, this is why I am glad I asked this question. I wasnt sure on a a lot of this. – user489041 Nov 08 '11 at 17:12
  • @user489041 a byte would still be too big, since it's 8 bits not 4. But yeah I see what you're trying to do now. – Ray Nov 08 '11 at 17:17

9 Answers9

18

You can use the bitwise & operator:

int value = 0x102F1032;
int checkBit = 0x00010000;
bool hasBit = (value & checkBit) == checkBit;
Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
13

It's much easier than that. Just use the bitwise AND operator like this

(value & 0x00010000) != 0
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

You can just check like so:

bool bitSet = (value & 0x10000) == 0x10000;
Ray
  • 45,695
  • 27
  • 126
  • 169
4

You can just do a bitwise AND.

int result = yourByte & 16;
if (result != 0)
{
    // do what you need to when that bit is set
}
3

Use the bitwise and operator &:

return (value & 0x100000) != 0;
Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

And if you don't like the bit mask approach:

int data = 0;

var bits = new BitArray(new int[] { data });

bits.Get(21);

(I may have got the wrong count and it's not 21 you are looking for)

This might me a bit abusive for a case where you only have 32 bits, but if you need to work with longer bit arrays this is much clearer.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
2

you can say:

if( (number & 0x00010000 ) != 0 )
{
  //the bit is set...
}
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
2
int someInt = 8;
int BitToTest = 3;
bool isSet = (someInt & (1 << BitToTest)) != 0;

And it with the shifted value, bit is set if the answer is nonzero. If you are doing one bit a lot use a constant for (1 << BitToTest), if a lot but different bits, a static array to look up 2 ^ BitToTest.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
1

Additionally, but maybe not better you can use the BitVector32 structure.

int value =  0x102F1032;
var vector = new BitVector32(value);
return vector[0x1000]; //true
Ray
  • 45,695
  • 27
  • 126
  • 169