Bits are actually numbered from right to left (seen from how they are printed).
But in a fixed amount of bits you could indeed number them the other way around. It does not matter that you write the leading zeros or not, everything to the left is zero anyway. When shifting, the right thing happens. Adding something like Integer>>#bitSliceFrom:to:size: based on your reverse numbering would be wrong, IMHO. I guess you could write it like n := 2r11110000101010001111000011110000. from := 8. to := 13. size := 32. (n >> (size - 13)) bitAnd: ((1 << ( to - from + 1)) - 1). But the size feels out of place. > On 4 Mar 2018, at 20:57, Esteban A. Maringolo <emaring...@gmail.com> wrote: > > I do bitshifts and masks on Integer. > > What if you want to take the bits from the 3rd to the 7th? You have to > do a some arithmetic to get the slice you want. > I'm simply asking for something more dev-friendlier that adds a > "layer" on top of that, but that internally does regular bitwise. > > What I don't like about Integers is that you "lose" information about > the zero bits to the left, and with a ByteArray you don't, because the > array is fixed size. > > E.g. > (1 << 16) printStringBase: 16. "'10000'" > #[1 0 0] hex "'010000'" > > Maybe I'm too lazy asking when I could have done it myself :) > > Regards, > > Esteban A. Maringolo > > > 2018-03-04 16:40 GMT-03:00 Sven Van Caekenberghe <s...@stfx.eu>: >> 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 <emaring...@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 >>> >> >> >