Take a 24-bit number and you want to isolate the first 5 (these are actually the last, higher order) bits. n := 2r101010001111000011110000. n >> (16+3). If necessary, you can apply a mask (assume there are bits earlier/later still). (n >> (16+3)) bitAnd: 2r11111 Large integers behave as bit strings, see the 'bit manipulation' protocol, and are efficient at it.
On 4 Mar 2018, at 20:29, Esteban A. Maringolo <emaringolo@gmail.com> wrote:
Is there any package/library that makes bitwise operations as simple as with an Integer, but for larger numbers (as in a ByteArray).
Something that allows me to "slice" a sequence of bits, or extract some using the same protocol as with a String of ones and zeros.
Now when I need to work with sequence of bits, I convert an Integer to a zero padded version of it up a known size, and then do #copyFrom:to: to extract what I need and read back the number from it.
I could use a bytearray for it, but as its name implies, it is oriented towards bytes rather than bits (as in the case of Integer).
Now I do stuff like the following to to extract the first 5 bits of a fixed length 256 bit array (an Integer).
Integer readFrom: (((SHA256 hashMessage: message)) asInteger printStringBase: 2 length: 256 padded: true) copyFrom: 1 to: 5) base: 2
I understand bitwise operations, but I couldn't find something that does the above in a conciser way.
Performance in my case isn't critical, but working with strings is probably two orders of magnitude slower than manipulating bits in integers or ByteArrays
Regards,
Esteban A. Maringolo