Java: Checking if a bit is 0 or 1 in a long

2019-01-06 12:01发布

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

14条回答
孤傲高冷的网名
2楼-- · 2019-01-06 12:19

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

boolean isSet = (value & (1 << n)) != 0;
查看更多
爷的心禁止访问
3楼-- · 2019-01-06 12:19

I coded a little static class which is doing some of the bit operation stuff.

public final class Bitfield {

  private Bitfield() {}

  // ********************************************************************
  // * TEST
  // ********************************************************************

  public static boolean testBit(final int pos, final int bitfield) {
      return (bitfield & (1 << pos)) == (1 << pos);
  }

  public static boolean testNum(final int num, final int bitfield) {
      return (bitfield & num) == num;
  }

  // ********************************************************************
  // * SET
  // ********************************************************************

  public static int setBit(final int pos, final int bitfield) {
     return bitfield | (1 << pos);
  }

  public static int addNum(final int number, final int bitfield) {
      return bitfield | number;
  }

  // ********************************************************************
  // * CLEAR
  // ********************************************************************

  public static int clearBit(final int pos, final int bitfield) {
      return bitfield ^ (1 << pos);
  }

  public static int clearNum(final int num, final int bitfield) {
      return bitfield ^ num;
  }

  }

If there are some questions flying around, just write me an email.

Good Programming!

查看更多
仙女界的扛把子
4楼-- · 2019-01-06 12:22

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

查看更多
倾城 Initia
5楼-- · 2019-01-06 12:23

My contribution - ignore previous one

public class TestBits { 

    public static void main(String[] args) { 

        byte bit1 = 0b00000001;     
        byte bit2 = 0b00000010;
        byte bit3 = 0b00000100;
        byte bit4 = 0b00001000;
        byte bit5 = 0b00010000;
        byte bit6 = 0b00100000;
        byte bit7 = 0b01000000;

        byte myValue = 9;                        // any value

        if (((myValue >>> 3) & bit1 ) != 0) {    //  shift 3 to test bit4
            System.out.println(" ON "); 
        }
    } 
}
查看更多
一夜七次
6楼-- · 2019-01-06 12:29

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

查看更多
够拽才男人
7楼-- · 2019-01-06 12:29

declare a temp int and make it equal the original. then shift temp >> x times, so that the bit you want to check is at the last position. then do temp & 0xf to drop the preceding bits. Now left with last bit. Finally do if (y & 1 == 0), if last bit is a 1, that should equal 0, else will equal 1. Its either that or if (y+0x1 == 0)... not too sure. fool around and see

查看更多
登录 后发表回答