From: "kernigh (George Koehler)" Date: 2012-05-04T06:04:42+09:00 Subject: [ruby-core:44858] [ruby-trunk - Feature #6361] Bitwise string operations Issue #6361 has been updated by kernigh (George Koehler). =begin A new BinaryString (or Blob) class would entail several changes. For example, Array#pack and IO#read(n) would need to return BinaryString, not String. I prefer to keep String for binary strings. The main reason, to perform bitwise operations on a String, is to use this String as an array of bits. The purpose of each bitwise operation is to clear or set some bits in this array. For example, a sieve of Erathosthenes might clear a bit in a String to show that a number is not prime. String has no bitwise operations. So, the code to clear a bit is long, not simple. sieve = "\xAC(\x8A\xA2(" # 25 is not prime. Clear bit 0x02 of byte 3. sieve.setbyte(3, sieve.getbyte(3) & ~0x02) A simpler way, with current Ruby, is to unpack this String into an Array of Integers. The code can then perform bitwise operations with these Integers. sieve = "\xAC(\x8A\xA2(".unpack("C*") # 25 is not prime. Clear bit 0x02 of byte 3. sieve[3] &= ~0x02 The sieve in [1] uses an Array of 16-bit Fixnums. This works because Fixnum has bitwise operations. A sieve at [2] uses an Array of true and false. This also works, though it uses more memory. [1] https://github.com/ruby/ruby/blob/trunk/lib/prime.rb [2] http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Ruby If String would have bitwise operations, a sieve of Eratosthenes might use them. If String#& and String#~ would exist, then they can clear a bit. sieve = "\xAC(\x8A\xA2(" sieve[3] &= ~"\x02" With this design, String#& and String#~ never modify the original string. This design forgot that Strings are mutable. String#& must allocate a new string, before String#[]= modifies the original string. A better design might provide destructive methods that use bitwise operations to modify a slice of the original string. =end ---------------------------------------- Feature #6361: Bitwise string operations https://bugs.ruby-lang.org/issues/6361#change-26433 Author: MartinBosslet (Martin Bosslet) Status: Feedback Priority: Normal Assignee: Category: core Target version: I know this has been discussed a lot in the past (and if there's still an open issue for this, I apologize, I couldn't find one), for example in [1]. While it is generally no problem to implement this on the fly, I still find that built-in support would be a real improvement. There are quite some use cases in cryptography where this would come in very handy, but I'm sure there are lots of other areas, too. While of course I understand the reasons that were given in the previous threads that ultimately lead to rejection, I still would like to reopen the discussion as I felt that in every thread so far the consensus was that having bitwise string operations would indeed be quite valuable. [1] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/34586 -- http://bugs.ruby-lang.org/