Atomically clearing lowest non-zero bit of an unsigned integer
Question: I'm looking for the best way to clear the lowest non-zero bit of a unsigned atomic like std::atomic_uint64_t in a threadsafe fashion without using an extra mutex or the like. In addition, I also need to know, which bit got cleared. Example: Lets say, if the current value stored is 0b0110 I want to know that the lowest non-zero bit is bit 1 (0-indexed) and set the variable to 0b0100 . The best version I came up with is this : #include <atomic> #include <cstdint> inline uint64_t with_lowest_non_zero_cleared(std::uint64_t v){ return v-1 & v; } inline uint64_t only_keep_lowest_non_zero