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

前端 未结 14 1832
抹茶落季
抹茶落季 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条回答
  •  旧时难觅i
    2020-11-29 01:56

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

    boolean isSet = (value & (1 << n)) != 0;
    

提交回复
热议问题