Skip to content

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

Merged
headius merged 2 commits into
jruby:ruby-4.1from
zev:string-bitwise-ops
Dec 1, 2026
Merged

headius merged 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

…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>
@zev
zev force-pushed the string-bitwise-ops branch from 1d33c53 to 8d195ff Compare November 24, 2026 03:36
@zev
zev marked this pull request as ready for review November 24, 2026 03:37
@zev

zev commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@headius for after #10118 or if you want I can merge these into 1 PR.

@headius headius added this to the JRuby 10.2.0.0 milestone Dec 1, 2026
@headius
headius merged commit 6f4e39b into jruby:ruby-4.1 Dec 1, 2026
128 of 149 checks passed
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.

2 participants