`BooleanArray(64) { (long shr it) and 1 != 0 }`
# announcements
k
BooleanArray(64) { (long shr it) and 1 != 0 }
l
i see, and how would i convert back?
k
Hmm that's not quite as convenient, probably just a for loop is best.
You can also consider using a
BitSet
from Java, it can convert back and forth to a long array.
It's not actually that bad:
arr.foldIndexed(0L) { i, a, b -> a + if (b) 1 shr i else 0 }
l
now you've lost me
k
Take a look at the source code of
fold
, it applies the lambda repeatedly to the previous result and the next element of the array. It's a common functional idiom, I'm sure there's lots of information about this online.
l
ok ty
k
And in this case it basically ends up being
sumByLong
which is missing from the stdlib I think.
l
hmm your code to convert back to Long always returns 1 but I can't find the bug
k
It has to be
shl
.
l
ty
d
You can also make your own class wrapping the long value with functions to set bits, if you're specifically looking for 64 values.
l
could i make an inline class that uses [] syntax to access the individual bits? and yes i am specifically looking at
Long
values
thanks!