29
\$\begingroup\$

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.

\$\endgroup\$
  • 19
    \$\begingroup\$ Clearing the most significant bit from 10 obviously gives 0 :D \$\endgroup\$ – clabacchio Nov 15 '17 at 10:26
  • \$\begingroup\$ @clabacchio I.. it... er... wha? (nice one) \$\endgroup\$ – Baldrickk Nov 15 '17 at 10:46
  • 12
    \$\begingroup\$ 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". \$\endgroup\$ – Michael Kay Nov 16 '17 at 18:54

75 Answers 75

1 2 3
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Actually, 7 bytes

;╘L2ⁿ@-

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 7 bytes

bS1¸-JC

Try it online! or Try all test cases

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

\$\endgroup\$
  • \$\begingroup\$ The logarithm version is a bit shorter, too bad we need to handle 1 though :(, otherwise the 3-byter would be the shortest \$\endgroup\$ – Mr. Xcoder Nov 14 '17 at 20:04
  • \$\begingroup\$ @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 \$\endgroup\$ – NieDzejkob Nov 14 '17 at 20:22
  • \$\begingroup\$ @NieDzejkob The answer I linked to is my own answer and does use logarithms. \$\endgroup\$ – Mr. Xcoder Nov 14 '17 at 20:24
  • \$\begingroup\$ @Mr.Xcoder then the StackExchange app is utterly broken, let me see \$\endgroup\$ – NieDzejkob Nov 14 '17 at 20:25
0
\$\begingroup\$

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

$_-=2**int((log)/log 2)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

GolfScript - 13 bytes

~2base(;2base

Explanation

~      # Parse argument
2base  # Convertion to base 2
(;     # Cut away first byte
2base  # Convertion from base 2
\$\endgroup\$
0
\$\begingroup\$

Tcl, 45 bytes

puts [scan [regsub 1 [format %b $argv] 0] %b]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

C++, 42 bytes

int f(int n){return n-exp2((int)log2(n));}
\$\endgroup\$
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – James Nov 16 '17 at 21:20
  • \$\begingroup\$ 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. \$\endgroup\$ – uKER Nov 16 '17 at 21:26
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – James Nov 16 '17 at 21:28
  • \$\begingroup\$ I see. No problem then. \$\endgroup\$ – uKER Nov 16 '17 at 21:31
  • 1
    \$\begingroup\$ 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));} \$\endgroup\$ – CSM Nov 18 '17 at 14:37
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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;
\$\endgroup\$
1 2 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.