Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Clearing the most significant bit from an integer

Input

The input is a single positive integer n

Output

The output isn with its most significant bit set to 0.

Test Cases

1 -> 0
2 -> 0
10 -> 2
16 -> 0
100 -> 36
267 -> 11
350 -> 94
500 -> 244

For example: 350 in binary is 101011110. Setting its most significant bit (i.e. the leftmost 1 bit) to 0 turns it into 001011110 which is equivalent to the decimal integer 94, the output. This is OEIS A053645.

Answer*

Draft saved
Draft discarded
Cancel
10
  • \$\begingroup\$ The () is apparently unnecessary \$\endgroup\$ Commented Nov 14, 2017 at 19:16
  • \$\begingroup\$ 17 bytes \$\endgroup\$ Commented Nov 14, 2017 at 19:16
  • 1
    \$\begingroup\$ 17 bytes: #-2^⌊Log2@#⌋& \$\endgroup\$ Commented Nov 14, 2017 at 19:33
  • \$\begingroup\$ @Jenny_mathy Someone came up with the same exact program? I didn't know this was a sequence in OEIS \$\endgroup\$ Commented Nov 14, 2017 at 19:58
  • 1
    \$\begingroup\$ Too bad we can't do #~BitClear~-1& which would be 14 bytes. Seems like the natural extension of the syntax. Maybe in version 12. \$\endgroup\$ Commented Nov 14, 2017 at 20:07

AltStyle によって変換されたページ (->オリジナル) /