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

前端 未结 14 1735
抹茶落季
抹茶落季 2020-11-29 01:14

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

相关标签:
14条回答
  • 2020-11-29 01:45

    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 "); 
            }
        } 
    }
    
    0 讨论(0)
  • 2020-11-29 01:48

    If someone is not very comfortable with bitwise operators, then below code can be tried to programatically decide it. There are two ways.

    1) Use java language functionality to get the binary format string and then check character at specific position

    2) Keep dividing by 2 and decide the bit value at certain position.

    public static void main(String[] args) {
        Integer n =1000;
        String binaryFormat =  Integer.toString(n, 2);
        int binaryFormatLength = binaryFormat.length();
        System.out.println("binaryFormat="+binaryFormat);
        for(int i = 1;i<10;i++){
            System.out.println("isBitSet("+n+","+i+")"+isBitSet(n,i));
            System.out.println((binaryFormatLength>=i && binaryFormat.charAt(binaryFormatLength-i)=='1'));
        }
    
    }
    
    public static boolean isBitSet(int number, int position){
        int currPos =1;
        int temp = number;
        while(number!=0 && currPos<= position){
            if(temp%2 == 1 && currPos == position)
                return true;
            else{
                temp = temp/2;
                currPos ++;
            }
        }
        return false;
    }
    

    Output

    binaryFormat=1111101000
    isBitSet(1000,1)false
    false
    isBitSet(1000,2)false
    false
    isBitSet(1000,3)false
    false
    isBitSet(1000,4)true
    true
    isBitSet(1000,5)false
    false
    isBitSet(1000,6)true
    true
    isBitSet(1000,7)true
    true
    isBitSet(1000,8)true
    true
    isBitSet(1000,9)true
    true
    
    0 讨论(0)
  • 2020-11-29 01:50

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

    0 讨论(0)
  • 2020-11-29 01:56

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

    boolean isSet = (value & (1 << n)) != 0;
    
    0 讨论(0)
  • 2020-11-29 01:56

    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!

    0 讨论(0)
  • 2020-11-29 01:57

    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.)

    0 讨论(0)
提交回复
热议问题