Reputation: 665
I am trying to use a signed long as an array of 1s and 0s. I need to be able to assign any bit in a long, or read any bit in a long. I keep getting stuck because I can't seem to read or write to the sign bit without messing up the rest of the bits (due to two's-complement). I could use a boolean array to represent the 1s and 0s, but that takes up too much memory. Does anyone know of a way to individually read and write to any bit in a long? Or can anyone suggest a super memory efficient alternative?
Upvotes: 1
Views: 164
Reputation: 1071
For this problem, BitSet is the appropriate library class. Internally, it is implemented using the long and if required grows to using BigInteger. For more info, read the javadoc.
Some example code in online java ide demonstrating the use of BitSet.
https://www.codiva.io/p/f6f0ee80-2ad3-4fb6-a197-aa1e549fba80
Upvotes: 1