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

Return to Revisions

3 of 5
added 650 characters in body
Ayb4btu
  • 571
  • 1
  • 4
  • 7

C# (.NET Core), 28+13=41 bytes

n=>n-(1<<(int)Math.Log(n,2))

Try it online!

All credit for this answer goes to NieDzejkob; the mathematical approach is significantly better than binary string manipulation (below).

C# (.NET Core), 86+13=99 bytes

n=>{var t=Convert.ToString(n,2).Remove(0,1);return t.Length<1?0:Convert.ToInt32(t,2);}

Try it online!

+13 bytes for using System;

###UnGolfed###

n=>{
 var t = Convert.ToString(n,2).Remove(0,1); 
 return t.Length < 1 ? 0
 : Convert.ToInt32(t,2);
}

Unfortunately Convert.ToInt32 throws an exception on "", instead of returning 0.

Ayb4btu
  • 571
  • 1
  • 4
  • 7

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