# 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.

• Clearing the most significant bit from 10 obviously gives 0 :D – clabacchio Nov 15 '17 at 10:26
• @clabacchio I.. it... er... wha? (nice one) – Baldrickk Nov 15 '17 at 10:46
• It seems to me that the zeroes are just as significant as the ones. When you say "the most significant bit" you mean "the most significant bit that is set to one". – Michael Kay Nov 16 '17 at 18:54

# SNOBOL4 (CSNOBOL4), 74 65 bytes

	I =INPUT
J =1
R	J =GE(I - J) J + J :S(R)
OUTPUT =I - J / 2
END


Try it online!

	I =INPUT			;*read in input
J =1				;*set J to 1
R	J =GE(I - J) J + J :S(R)	;*if I-J>=0, double J and repeat, otherwise
OUTPUT =I - J / 2		;*output I-J/2
END


# Python 2, 30 bytes

f=lambda n:n-1and 2*f(n/2)+n%2


Try it online!

Uses no built-in methods. 3 bytes longer than this solution using bin.

# Pip, 8 bytes

FB+@>TBa


Try it online!

### Explanation

     TBa  Convert cmdline arg to binary
@>     Take all but the leftmost character
+       Convert to number (required for turning "" into 0 so FB works properly)
FB        Convert from binary to decimal


# Actually, 7 bytes

;╘L2ⁿ@-


Try it online!

# 05AB1E, 7 bytes

bS1¸-JC

b       # Convert to binary
S      # Split
1¸-   # Subtract 1 from the first element
JC # Join and convert to decimal


b¦C works except for an input of 1

• The logarithm version is a bit shorter, too bad we need to handle 1 though :(, otherwise the 3-byter would be the shortest – Mr. Xcoder Nov 14 '17 at 20:04
• @Mr.Xcoder the answer that you've linked doesn't look like it's using logarithms. However, the formula n-2^floor(log2(n)) handles 1 correctly, if that's what you are referring to – NieDzejkob Nov 14 '17 at 20:22
• @NieDzejkob The answer I linked to is my own answer and does use logarithms. – Mr. Xcoder Nov 14 '17 at 20:24
• @Mr.Xcoder then the StackExchange app is utterly broken, let me see – NieDzejkob Nov 14 '17 at 20:25

# Perl 5, 23 + 1 (-p) = 24 bytes

$_-=2**int((log)/log 2)  Try it online! # GolfScript - 13 bytes ~2base(;2base  ### Explanation ~ # Parse argument 2base # Convertion to base 2 (; # Cut away first byte 2base # Convertion from base 2  # Tcl, 45 bytes puts [scan [regsub 1 [format %b$argv] 0] %b]


Try it online!

## JavaScript (ES6), 23 bytes

f=x=>x^1&&f(x/2)*2|x&1


Not the shortest JS solution overall, but it beats the .toString(2) solution.

# C++, 63 bytes

[](decltype(0ul)x){auto y=x;return x^_BitScanReverse(&y,x)<<y;}


Usage (works in MS Visual Studio):

#include <intrin.h>
#include <stdio.h>

int main()
{
auto h = [](decltype(0ul)x){auto y=x;return x^_BitScanReverse(&y,x)<<y;};

printf("%d\n", h(10));
}


I added an artificial constraint on my code:

It must use _BitScanReverse

The effect this had on my code is quite severe.

1. I used decltype(0ul) as an euphemism for unsigned long.
2. _BitScanReverse outputs its result by pointer, so I had to declare another unsigned long variable. This time, I did it with auto y=x.
3. The output of _BitScanReverse is "whether the input is nonzero", which is 1 (luckily).
4. The returned expression modifies y and then uses it, which is Undefined Behavior. There are no warnings though, and it works correctly.

# ARM (Thumb mode) machine code, 12 bytes

b0 fa 80 f1 01 31 88 40 c8 40 70 47


Disassembly:

fab0 f180       clz     r1, r0
3101            adds    r1, #1
4088            lsls    r0, r1
40c8            lsrs    r0, r1
4770            bx      lr


# C++, 42 bytes

int f(int n){return n-exp2((int)log2(n));}

• Could you provide a link to an online compiler where this works? I doubt this works without int n in the declaration, and I'm not sure that C++ has exp2 and log2 functions without some #include – James Nov 16 '17 at 21:20
• Added the return type. About the includes, I don't see them counted in any of the previous C/C++ replies and I don't see you downvoting them or hassling them about it. But thanks anyway. – uKER Nov 16 '17 at 21:26
• Oh I didn't downvote you! I'm sorry if you got that impression. That's due to a bug that sometimes hits new users with short posts. Which is really unfortunate, but SE seems to not care. – James Nov 16 '17 at 21:28
• I see. No problem then. – uKER Nov 16 '17 at 21:31
• In K&R C, any undeclared functions are assumed return an int, and take whatever type is passed in. Most modern compilers accept this, but create a warning. In golf we can make use of this. However, in your submission, you marked it as C++, not C. C++ does not allow undeclared functions. Also you are passing an int into exp2 (and log2), which won't be promoted to float. However, you can remove the int for the return and input for your function f - saving 8 bytes. This should work, for 50 bytes, when split over two lines #include<math.h> f(n){return n-exp2((int)log2(n));} – CSM Nov 18 '17 at 14:37

# Pyth - 8 Bytes

ig.BQ2 2


Explanation:

ig.BQ2 2
i      2 Convert to base 2
g   2   Slice from position 1 to the end (g slices from b-1)
.B     Binary String representation of
Q    Input


# J, 8 Bytes

#.@}.@#:


### How it works:

  @  @       | Verb conjunction. Makes sure it isn’t executed as a hook
#:     | Converts Number to binary, with no leading 0s (except if arg is 0)
}.        | Drop leading 1
#.           | Convert back to int


Works for 1 and 0 since #. will convert an empty array into 0

# Manufactoria, 24 Bytes, 3 Blocks

c12:6f3;c12:8f3;p12:7f2;

?lvl=32&code=c12:6f3;c12:8f3;p12:7f2;&ctm=Level_Name!;Level_description!;:*;5;3;0;