Bitwise OR of a Range
Return the value obtained by combining every integer from lo to hi (both included) with bitwise OR (|). A bit of the answer is 1 exactly when at least one integer in the range has that bit set. The range can hold about two billion numbers, so a loop over all of them is too slow.
Example 1
lo 5
hi 7
returns 7
5 | 6 | 7 is 101 | 110 | 111, which is 111, or 7.
Example 2
lo 4
hi 6
returns 7
4 | 5 | 6 is 100 | 101 | 110, which is 111, or 7.
Constraints
0 <= lo <= hi < 2**31
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Different ORs of StretchesMedium
- Differing Bits Over All PairsMedium
- Flips to Make OR EqualMedium
- Insert a Bit FieldMedium
- Longest Pair With No Shared LetterMedium
- Next Gray CodeMedium
- Subsets With a Target XORMedium
- The One Not TripledMedium