use >> 1 for bit manipulation, / 2 for arithmetic halving#240
Merged
Conversation
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.
What
Use the operator that matches intent:
>> 1where we are doing bit manipulation — walking powers of two, triebit positions, navigating the implicit segment-tree by index, binary
exponentiation, Hilbert-curve bit planes, masking with
& -bc./ 2where we are arithmetic halving a value — midpoints, averaging twoindices, splitting a count/length in two.
Both are identical for the non-negative operands here (
/truncates towardzero,
>>floors toward −∞, and every site is provably>= 0), so this ispurely about spelling intent consistently — one of the pillars of the library.
>> 1— bit manipulationbit_uncommon/walk.hpp:i >>= 1— Fenwick binary lifting (i = bit_floor(...))seg_tree.hpp(both variants):i >>= 1,l >>= 1, r >>= 1— implicitbinary-heap index navigation
strings/binary_trie.hpp:bit >>= 1(×2) — walking bit positions of a keyuncommon/hilbert_mos.hpp:s >>= 1— Hilbert-curve bit planestrees/uncommon/linear_kth_par.hpp:(bc >> 1)— bit offset inside& -bcmath/tetration_mod.hpp+math/count_paths.test.cpp(local modpow):e >>= 1— binary exponentiation (paired withe & 1)bit_width(EXPR / 2u)— log2 / shift-amount computationbit_width(x + 0u) - 1is rewritten asbit_width(x / 2u): same value, but itreads as "the shift amount", and
/ 2ukeeps the argument unsigned asstd::bit_widthrequires. Sites:rmq(both),disjoint_rmq,seg_treenxt,seg_tree_uncommon/{min_left,max_right}(both),range_parallel_dsu,ladder_decomposition,linear_kth_par./ 2— arithmetic halving (kept/left as division)All midpoints and count/length halving stay as
/ 2:seg_tree_midpoint.hpp(both),
min_plus_convolution_convex_and_arbitrary.hpp,count_paths/count_paths_triangle.hpp,manacher.hpp,longest_from_index.hpp,longest_palindrome_query.hpp,uncommon/deque_op.hpp, and the binary-searchmidpoints / count halving in the aizu tests (
bit_ordered_set,kth_smallest_pst,mode_query,pq_ds_undo_sliding_window,sa_sort_pairs,lcp_array,two_edge_components,mono_st_asserts.hpp,handmade_tests/seg_tree_midpoint).Also in this PR
walkcleanup: the callback-basedwalk(f)inwalk_lambda.hppisremoved and
walk2(sum)is renamed towalk(sum)in both[l,r)and[l,r]variants (single, sum-based API). Tests and the cppcheck suppression list are
updated accordingly.
Why (assembly)
g++ -O3. For signed operands the difference is only the round-toward-zerocorrection; for the unsigned/non-negative sites here both lower to a single shift:
Local testing
clang-formatdry-run (--Werror --style=file:tests/.config/dev.clang-format)— passes on all changed files
seg_tree_midpoint,seg_tree_find,hilbert_mos,manacher,kth_par— passsplitequivalence over 9,000,000 range pairs — identicalBIT::walkanddeqrebuild vs brute force — passlongest_pal_queryvs brute force over 20,000 random strings × all substrings— identical (start index always
>= 0)Full library-checker/aizu verification runs in CI.