Skip to content

Add String#bitwise_not, bitwise_and, bitwise_or, bitwise_xor and their ! forms (Feature #22118) - #10125

Draft
zev wants to merge 2 commits into
jruby:ruby-4.1from
zev:string-bitwise-ops
Draft

zev wants to merge 2 commits into
jruby:ruby-4.1from
zev:string-bitwise-ops

Conversation

@zev

@zev zev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Based on #10118 (String#bit_get and related methods). Please merge that one first; this diff includes only the second commit.

Adds String#bitwise_not, bitwise_and, bitwise_or, bitwise_xor and their ! forms from Ruby 4.1 (Feature #22118), an item on the #9746 checklist.

CRuby change: ruby/ruby@2461caebba. Current code: string.c#L8212-L8494.

Call CRuby 4.1 JRuby before JRuby after
"\x00\xAA".b.bitwise_not "\xFFU" NoMethodError: undefined method 'bitwise_not' ... "\xFFU"
"\xF0".bitwise_and("\xCC").encoding BINARY NoMethodError BINARY
"\xF0".b.bitwise_or("\x0C".b) "\xFC" NoMethodError "\xFC"
(+"\xF0").b.bitwise_xor!("\xCC".b) "<" NoMethodError "<"
"ab".bitwise_and("abc") ArgumentError: operands must have the same length (2 vs 3) NoMethodError same as CRuby
"a".freeze.bitwise_not! FrozenError: can't modify frozen String: "a" NoMethodError same as CRuby

Changes

  • org.jruby.util.StringBits gets the byte-wise operations, with a Bitwise enum for AND, OR and XOR. Like CRuby's kernels, the loops work on eight bytes at a time (with a VarHandle), then on the bytes left over.
  • As in CRuby, the argument is converted with to_str, and the lengths are compared before the frozen check. The plain forms return a new BINARY String, even for a subclass. The ! forms keep the receiver's encoding and clear its code range.
  • RubyString gets the eight @JRubyMethods.

Specs

spec/ruby/core/string/bitwise_{not,and,or,xor}_spec.rb already match CRuby's copies, so no spec files change.

Testing

  • The four bitwise_* spec files: 22 examples, 0 failures, 0 errors on JRuby (interpreter and forced JIT) and CRuby 4.1.0-dev. CRuby 4.0.7 runs 0 examples, because of the version guard. On the base branch, all 22 errored.
  • spec/ruby/core/string (mspec ci): 0 failures, 0 errors (95 tagged, as before).
  • test/mri/ruby/test_string.rb: 0 failures, as before.
  • CRuby's own test_bitwise (from CRuby master's test_string.rb, copied outside the tree) passes for String and the String subclass.
  • A script with about 90 calls (encodings, to_str, wrong types and lengths, frozen and shared strings, self as the argument, long strings, and to_str that changes the receiver) gives the same output as CRuby 4.1.0-dev, in both the interpreter and the JIT.

Not in this PR

Nothing new. The items listed in #10118 apply to this PR too.

🤖 Generated with Claude Code

zev and others added 2 commits November 17, 2026 23:46
…unt (Feature #22118)

These read, change and count single bits of a string's bytes. Bit 0 is
the least significant bit of the first byte; lsb_first: false numbers
the bits in each byte from the most significant bit instead.

bit_set, bit_clear, bit_flip and bit_count also take a region, as an
offset and a length or as a Range (Feature #22279). Writes raise
IndexError when the region runs past the end; bit_count clips it. The
frozen check comes before the bounds check, as in CRuby (Bug #22316).

The logic lives in the new org.jruby.util.StringBits. Bit positions
from 2**63 to 2**64-1 are kept as Long.MAX_VALUE: they are past the end
of every JRuby string, so the results are the same as CRuby's.

Updates the bit_set, bit_clear, bit_flip and bit_count specs from
CRuby's spec/ruby, which add the region and frozen cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r ! forms (Feature #22118)

bitwise_not inverts every byte. bitwise_and, bitwise_or and bitwise_xor
combine each byte with the byte at the same index of another string,
converted with to_str; both strings must have the same byte length.
The plain forms return a new BINARY string; the ! forms change self and
keep its encoding.

The loops work on eight bytes at a time, like CRuby's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant