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

前端 未结 14 1736
抹茶落季
抹茶落季 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 02:01

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

    0 讨论(0)
  • 2020-11-29 02:02

    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

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