I have an initial String (binary) looking like this :
val mask = "00000000000000000000000000000000"
of length 32
Additionally, I have a list of positions i (0 <= i <= 31) at which I want the mask to have value 1.
For instance List(0,12,30,4)
should give the following result :
mask = "10001000000010000000000000000010"
How can I do this efficiently in scala ?
Thank you
A naive approach would be to fold over the positions with zero element 'mask' and successively update the char at the given position:
List(0,12,30,4).foldLeft(mask)((s, i) => s.updated(i, '1'))
- Daniel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With