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

前端 未结 14 1799
抹茶落季
抹茶落季 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: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.)

提交回复
热议问题