Repository navigation
Add String#bitwise_not, bitwise_and, bitwise_or, bitwise_xor and their ! forms (Feature #22118) - #10125
Merged
Conversation
…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
force-pushed
the
string-bitwise-ops
branch
from
November 24, 2026 03:36
1d33c53 to
8d195ff
Compare
zev
marked this pull request as ready for review
November 24, 2026 03:37
Contributor
Author
headius
approved these changes
Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_xorand 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.
"\x00\xAA".b.bitwise_not"\xFFU"NoMethodError: undefined method 'bitwise_not' ..."\xFFU""\xF0".bitwise_and("\xCC").encodingBINARYNoMethodErrorBINARY"\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"a".freeze.bitwise_not!FrozenError: can't modify frozen String: "a"NoMethodErrorChanges
org.jruby.util.StringBitsgets the byte-wise operations, with aBitwiseenum for AND, OR and XOR. Like CRuby's kernels, the loops work on eight bytes at a time (with aVarHandle), then on the bytes left over.to_str, and the lengths are compared before the frozen check. The plain forms return a new BINARYString, even for a subclass. The!forms keep the receiver's encoding and clear its code range.RubyStringgets the eight@JRubyMethods.Specs
spec/ruby/core/string/bitwise_{not,and,or,xor}_spec.rbalready match CRuby's copies, so no spec files change.Testing
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.test_bitwise(from CRuby master'stest_string.rb, copied outside the tree) passes forStringand theStringsubclass.to_str, wrong types and lengths, frozen and shared strings, self as the argument, long strings, andto_strthat 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