views:

1681

answers:

9

Given that : 0000000000000000000000000000000000000000000000000000000000000001 = 1

What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ?

+40  A: 

I'd use:

if ((value & (1L << x)) != 0)
{
   // The bit was set
}

(You may be able to get away with fewer brackets, but I never remember the precedence of bitwise operations.)

Jon Skeet
you can remove the "!=0" because a non zero value is always true
ThibThib
As a matter of curiosity: why did you prefer the left-shift of 1 versus the right-shifting of value?
Erich Mirabal
That depends on the language. In java that isn't true.
harmanjd
That should be 1L, or (1 << 32) ends up with the same value as (1 << 0)
Matt Kane
"That depends on the language. In java that isn't true."You're right, I forgot that java was a strange language... ;-)
ThibThib
@Erich: I find it mentally easier to think of varying the mask rather than varying the value. The problem is more easily described as "is bit x set to 1" than "is the value shifted right by x bits 1 in the least bit". Personal taste I guess. @Matt: Fixed, thanks.
Jon Skeet
@ThibThib nothing strange about it. Please don't post stupid anti Java flame bait.
amischiefr
ThibThib: s/strange/strongly typed/
Tom Hawtin - tackline
Tom Hawtin - tackline
@Tom: Yes, I was wondering much the same thing :)
Jon Skeet
Paul Wagland
+3  A: 

For the nth LSB (least significant bit), the following should work:

bool isSet = (value & (1 << n)) != 0;
Noldorin
That should be 1L, or (1 << 32) ends up with the same value as (1 << 0)
Matt Kane
And `boolean` not `bool`.
Tom Hawtin - tackline
+1  A: 

The value of the 2^x bit is "variable & (1 << x)"

Artur Soler
As Matt Kane said in identical solutions: That should be 1L, or (1 << 32) ends up with the same value as (1 << 0)
drvdijk
+2  A: 

Bit shifting right by x and checking the lowest bit.

schnaader
+7  A: 

You can also use

bool isSet = ((value>>x) & 1) != 0;

EDIT: the difference between "(value>>x) & 1" and "value & (1<<x)" relies on the behavior when x is greater than the size of the type of "value" (32 in your case).

In that particular case, with "(value>>x) & 1" you will have the sign of value, whereas you get a 0 with "value & (1<<x)" (it is sometimes useful to get the bit sign if x is too large).

If you prefer to have a 0 in that case, you can use the ">>>" operator, instead if ">>"

So, "((value>>>x) & 1) != 0" and "(value & (1<<x)) != 0" are completely equivalent

ThibThib
+37  A: 

Another alternative:

if (BigInteger.valueOf(value).testBit(x)) {
    // ...
}
finnw
+1 for using code that is understandable by future maintainers
cherouvim
Not a very good solution if this code is going to be called often. You're replacing a one-line alternative with a one line alternative, and the bit shifts really aren't that hard.
wds
Length of line != readability of line, wds.You might be right that the previous solution is more efficient, but the difference is likely marginal, especially if testBit() gets inlined.
WCWedin
It should be noted this solution allocates memory and is much less efficient than bitwise operators. Yes, it often doesn't matter, but sometimes it is important to avoid unnecessary GC and/or inefficient code (Android apps, in a game's main loop, etc).
NateS
+6  A: 

I wonder if:

  if (((value >>> x) & 1) != 0) {

  }

.. is better because it doesn't matter whether value is long or not, or if its worse because it's less obvious.

Tom Hawtin - tackline Jul 7 at 14:16

_ande_turner_
I think it's better because there is less potential for errors - and if you think it isn't obvious, you can always extract the test into an appropriately named function (boolean isBitSet(long value, int x) or so)
hjhill
+2  A: 

You might want to check out BitSet: http://java.sun.com/javase/6/docs/api/java/util/BitSet.html

Yannick M.
A: 

Eliminate the bitshifting and its intricacies and use a LUT for the right and operand.

typeseven