How to set/unset a bit at specific position of a long?

前端 未结 5 508
粉色の甜心
粉色の甜心 2020-12-24 03:35

How to set/unset a bit at specific position of a long in Java ?

For example,

long l = 0b001100L ; // bit representation

I want to s

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 03:47

    • Convert long to a bitset
    • Set the bit you need to
    • Convert bitset back to long

    See this post BitSet to and from integer/long for methods to convert long to bitset and vice versa

提交回复
热议问题