--- Begin Message ---Just put that in Integer. ----------------------------bitsSliceFrom: start to: end " Extract bits 4 to 6, i.e. 111 which equals to 7 2r10000111000 bitsSliceFrom: 4 to: 6 " | num mask |self < 0 ifTrue: [self error: 'This operation is not allowed for negative numbers!']. num := self >> (start - 1). mask := (1 << ((end - start) + 1)) - 1. ^num bitAnd: mask.-----------------------------bitsSliceInPlaceFrom: start to: end " Extract bits 4 to 6, IN PLACE, i.e. 111 which equals to 2r00000111000 = 56 2r10000111000 bitsSliceInPlaceFrom: 4 to: 6 " | | ^(self bitsSliceFrom: start to: end) << (start - 1)---------------------------- This should do the job! hth ----------------- Benoît St-Jean Yahoo! Messenger: bstjean Twitter: @BenLeChialeux Pinterest: benoitstjean Instagram: Chef_Benito IRC: lamneth Blogue: endormitoire.wordpress.com "A standpoint is an intellectual horizon of radius zero". (A. Einstein) On Sunday, March 4, 2018, 2:59:16 PM EST, 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 >> > >
--- End Message ---
Re: [Pharo-users] Bitwise operations in ByteArray (slicing, etc.)
Benoit St-Jean via Pharo-users Sun, 04 Mar 2018 18:13:19 -0800
- Re: [Pharo-users] Bitwise oper... Esteban A. Maringolo
- Re: [Pharo-users] Bitwise ... Richard Sargent
- Re: [Pharo-users] Bitwise ... Stephane Ducasse
- Re: [Pharo-users] Bit... Esteban A. Maringolo
- Re: [Pharo-users]... Richard O'Keefe
- Re: [Pharo-users]... Esteban A. Maringolo
- Re: [Pharo-users]... Richard O'Keefe
- Re: [Pharo-users]... Richard Sargent
- Re: [Pharo-users]... Henrik Sperre Johansen
- Re: [Pharo-users]... Esteban A. Maringolo
- Re: [Pharo-users] Bitwise operatio... Benoit St-Jean via Pharo-users
- Re: [Pharo-users] Bitwise oper... Richard O'Keefe
- Re: [Pharo-users] Bitwise ... Stephane Ducasse
- Re: [Pharo-users] Bitwise ... Serge Stinckwich
- Re: [Pharo-users] Bitwise ... Esteban A. Maringolo
- Re: [Pharo-users] Bitwise operations in Byt... Benoit St-Jean via Pharo-users