Find the number of leading zeroes in a 64-bit integer

18

3

Problem:

Find the number of leading zeroes in a 64-bit signed integer

Rules:

  • The input cannot be treated as string; it can be anything where math and bitwise operations drive the algorithm
  • The output should be validated against the 64-bit signed integer representation of the number, regardless of language
  • Default code golf rules apply
  • Shortest code in bytes wins

Test cases:

These tests assume two's complement signed integers. If your language/solution lacks or uses a different representation of signed integers, please call that out and provide additional test cases that may be relevant. I've included some test cases that address double precision, but please feel free to suggest any others that should be listed.

input                output   64-bit binary representation of input (2's complement)
-1                   0        1111111111111111111111111111111111111111111111111111111111111111
-9223372036854775808 0        1000000000000000000000000000000000000000000000000000000000000000
9223372036854775807  1        0111111111111111111111111111111111111111111111111111111111111111
4611686018427387903  2        0011111111111111111111111111111111111111111111111111111111111111
1224979098644774911  3        0001000011111111111111111111111111111111111111111111111111111111
9007199254740992     10       0000000000100000000000000000000000000000000000000000000000000000
4503599627370496     11       0000000000010000000000000000000000000000000000000000000000000000
4503599627370495     12       0000000000001111111111111111111111111111111111111111111111111111
2147483648           32       0000000000000000000000000000000010000000000000000000000000000000
2147483647           33       0000000000000000000000000000000001111111111111111111111111111111
2                    62       0000000000000000000000000000000000000000000000000000000000000010
1                    63       0000000000000000000000000000000000000000000000000000000000000001
0                    64       0000000000000000000000000000000000000000000000000000000000000000

Dave

Posted 2018-12-09T23:08:42.553

Reputation: 325

14Welcome to PPCG! What's the reason behind "the input cannot be treated as string"? This disqualifies all languages that can't handle 64-bit integers and is unlikely to lead to more answers that take an integer, because this is the straightforward way when available anyway. – Arnauld – 2018-12-09T23:26:06.153

1Can we return False instead of 0? – Jo King – 2018-12-09T23:33:31.757

4@Arnauld There are already similar questions here and on other sites that specifically call for string-based solutions, but nothing that opens the question to math and logical operations. My hope is to see a bunch of different approaches to this problem that are not already answered elsewhere.
Should this be opened to string solutions as well to be all-inclusive?
– Dave – 2018-12-09T23:40:17.500

4Several CPUs including x86 and ARM have specific instructions for this (x86 actually have several). I've always wondered why programming languages don't expose this feature since in most programming languages today you can't invoke assembly instructions. – slebetman – 2018-12-10T05:43:42.973

It's 64 - log2.

– user202729 – 2018-12-10T13:30:10.130

@user202729 For positive input. – Dennis – 2018-12-10T13:32:45.260

@Dave Unobservable requirements are discouraged, although it's ok to simply encourage using methods you want.

– user202729 – 2018-12-10T13:33:05.887

1@user202729 I think I worded this poorly: 'The output should be validated against the 64-bit signed integer representation of the number, regardless of language'

What I mean by that is that this question defines the number of zeros as the number of zeros in a 64-bit signed integer. I guess I made that constraint to eliminate signed vs unsigned integers. – Dave – 2018-12-11T00:26:48.213

@slebetman I thought that built-ins were excluded as part of the default code golf rules, but I just saw that it's only a suggested rule.

I assume changing the requirements at this point is poor form, but I'll remember that in the future! – Dave – 2018-12-11T00:35:16.240

The equation for this is 64 if 0, 0 if -ve and 63-floor(log2(n)) otherwise – fəˈnɛtɪk – 2018-12-11T02:41:54.010

Don't "Do X without Y" – cat – 2018-12-11T22:28:26.037

1

@slebetman: many languages do have builtins for this, almost to the point where C and C++ are notable for their failure to portably expose modern CPU features like popcnt and bit-scan. (Especially given their use-case of low-overhead highly-optimized code). Most C compilers have their own incompatible and/or arch-specific intrinsics and builtins for this stuff. Rust is fantastic; all the primitive integer types have popcnt, bit-scan, rotate, endian, saturating add/sub, and wrapping vs. overflow-checked vs. non-overflowing add/sub/mul/div etc. https://doc.rust-lang.org/std/primitive.i32.html

– Peter Cordes – 2018-12-12T01:06:04.897

Answers

41

x86_64 machine language on Linux, 6 bytes

0:       f3 48 0f bd c7          lzcnt  %rdi,%rax
5:       c3                      ret

Requires Haswell or K10 or higher processor with lzcnt instruction.

Try it online!

ceilingcat

Posted 2018-12-09T23:08:42.553

Reputation: 5 503

20Builtins strike again /s – Logern – 2018-12-10T02:06:20.247

1I recommend specifying the calling convention used (though you did say on Linux) – qwr – 2018-12-12T08:02:51.027

@qwr It looks like SysV calling convention because the parameter is passed in %rdi and it is returned in %rax. – Logern – 2018-12-15T04:21:41.050

24

Hexagony, 78 70 bytes

2"1"\.}/{}A=<\?>(<$\*}[_(A\".{}."&.'\&=/.."!=\2'%<..(@.>._.\=\{}:"<><$

Try it online!

Isn't this challenge too trivial for a practical language? ;)

side length 6. I can't fit it in a side length 5 hexagon.

Explanation

user202729

Posted 2018-12-09T23:08:42.553

Reputation: 14 620

3I laughed really hard at the "explanation". :D – Eric Duminil – 2018-12-10T18:45:18.103

1

I think you may have overcomplicated handling negative numbers/zero. I managed to fit a similar program into side length 5 by not doing that hefty 2^64 calculation. It clearly isn't well golfed yet, though!

– FryAmTheEggman – 2018-12-10T22:56:09.600

@fry Ah right, negative numbers always have 0 leading zeroes... which definitely leads to shorter program because generates 2^64 is long. – user202729 – 2018-12-11T05:39:46.150

12

Python, 31 bytes

lambda n:67-len(bin(-n))&~n>>64

Try it online!

The expresson is the bitwise & of two parts:

67-len(bin(-n)) & ~n>>64

The 67-len(bin(-n)) gives the correct answer for non-negative inputs. It takes the bit length, and subtracts from 67, which is 3 more than 64 to compensate for the -0b prefix. The negation is a trick to adjust for n==0 using that negating it doesn't produce a - sign in front.

The & ~n>>64 makes the answer instead be 0 for negative n. When n<0, ~n>>64 equals 0 (on 64-bit integers), so and-ing with it gives 0. When n>=0, the ~n>>64 evaluates to -1, and doing &-1 has no effect.


Python 2, 36 bytes

f=lambda n:n>0and~-f(n/2)or(n==0)*64

Try it online!

Arithmetical alternative.

xnor

Posted 2018-12-09T23:08:42.553

Reputation: 115 687

9

C (gcc), 14 bytes

__builtin_clzl

Works fine on tio

C (gcc), 35 29 bytes

f(long n){n=n<0?0:f(n-~n)+1;}

Try it online!

Than Dennis for 6 bytes

C (gcc) compiler flags, 29 bytes by David Foerster

-Df(n)=n?__builtin_clzl(n):64

Try it online!

l4m2

Posted 2018-12-09T23:08:42.553

Reputation: 5 985

3Worth noting that it's only for 64-bit machines (or any others with LP64/ILP64/etc. ABI) – Ruslan – 2018-12-10T06:43:26.510

1

Geez, that’s even shorter than any use of the GCC built-in __builtin_clzl with which I can come up.

– David Foerster – 2018-12-10T11:49:58.233

@Ruslan: good point, on systems where long is 32 bits (including Windows x64), you need __builtin_clzll (unsigned long long). https://godbolt.org/z/MACCKf. (Unlike Intel intrinsics, GNU C builtins are supported regardless of the operation being doable with one machine instruction. On 32-bit x86, clzll compiles to a branch or cmov to do lzcnt(low half)+32 or lzcnt(high half). Or bsr if lzcnt isn't available.

– Peter Cordes – 2018-12-12T01:00:09.200

The test cases include "0" but __builtin_clz(l)(l) is undefined behavior for zero: "If x is 0, the result is undefined."

– MCCCS – 2018-12-12T13:36:43.860

1@MCCCS If it works, it counts. That's also why I keep the last answer – l4m2 – 2018-12-12T13:43:25.717

9

Java 8, 32 26 bytes.

Long::numberOfLeadingZeros

Builtins FTW.

-6 bytes thanks to Kevin Cruijssen

Try it online!

lukeg

Posted 2018-12-09T23:08:42.553

Reputation: 191

Ah, completely forgot about numberOfLeadingZeros.. You can golf it to 28 bytes btw: n->n.numberOfLeadingZeros(n) – Kevin Cruijssen – 2018-12-10T10:43:51.943

2Actually, Long::numberOfLeadingZeros is even shorter (26 bytes). – Kevin Cruijssen – 2018-12-10T10:46:31.943

6Wow, it doesn't happen very often that Java beats Python. Congrats! – Eric Duminil – 2018-12-10T18:46:06.310

6

JavaScript (Node.js), 25 bytes

Takes input as a BigInt literal.

f=x=>x<0?0:x?f(x/2n)-1:64

Try it online!

Or 24 bytes by returning false instead of \$0\$.

Arnauld

Posted 2018-12-09T23:08:42.553

Reputation: 111 334

Wouldn't n=>n<1?0:n.toString(2)-64 perform the same? – Ismael Miguel – 2018-12-10T12:59:29.440

@IsmaelMiguel I suppose you meant n=>n<1?0:n.toString(2).length-64, but that would not work anyway. This would, I think.

– Arnauld – 2018-12-10T13:28:46.347

You're right. Thank you :( I though I found an alternative for you :/ – Ismael Miguel – 2018-12-10T13:41:37.867

1

@IsmaelMiguel No worries. :) It's indeed possible to have the .toString() approach working, but we still need a BigInt literal as input. Otherwise, we only have 52 bits of mantissa, leading to invalid results when precision is lost.

– Arnauld – 2018-12-10T13:48:12.840

1The fact that the BigInt suffix is the same character as your parameter is very confusing... – Neil – 2018-12-11T09:25:17.840

1@Neil Unreadable code on PPCG?? This can't be! Fixed! :p – Arnauld – 2018-12-11T12:04:18.267

6

Perl 6, 35 28 26 bytes

-2 bytes thanks to nwellnhof

{to .fmt("%064b")~~/^0*/:}

Try it online!

Anonymous code block that takes a number and returns a number. This converts the number to a binary string and counts the leading zeroes. It works for negative numbers because the first character is a - e.g. -00000101, so there are no leading zeroes.

Explanation:

{                        }  # Anonymous code block
    .fmt("%064b")           # Format as a binary string with 64 digits
                 ~~         # Smartmatch against
                   /^0*/    # A regex counting leading zeroes
 to                     :   # Return the index of the end of the match

Jo King

Posted 2018-12-09T23:08:42.553

Reputation: 38 234

5

Python 3, 34 bytes

f=lambda n:-1<n<2**63and-~f(2*n|1)

Try it online!

ovs

Posted 2018-12-09T23:08:42.553

Reputation: 21 408

5

J, 18 bytes

0{[:I.1,~(64$2)#:]

Try it online!

J, 19 bytes

1#.[:*/\0=(64$2)#:]

Try it online!

Explanation:

                #:  - convert 
                  ] - the input to
          (64$2)    - 64 binary digits
         =          - check if each digit equals 
        0           - zero
   [:*/\            - find the running product
1#.                 - sum

Galen Ivanov

Posted 2018-12-09T23:08:42.553

Reputation: 13 815

11#.[:*/\1-_64{.#: (17) is close but doesn't work for negative numbers :( – Conor O'Brien – 2018-12-10T22:12:09.133

@Conor O'Brien Nice approach too! – Galen Ivanov – 2018-12-11T04:41:38.093

5

Perl 6, 18 bytes

-2 bytes thanks to Jo King

64-(*%2**64*2).msb

Try it online!

nwellnhof

Posted 2018-12-09T23:08:42.553

Reputation: 10 037

5

Ruby, 22 bytes

->n{/[^0]/=~"%064b"%n}

Try it online!

G B

Posted 2018-12-09T23:08:42.553

Reputation: 11 099

Could you explain? – dfeuer – 2019-03-10T06:21:09.953

4

Haskell, 56 bytes

Thanks xnor for spotting a mistake!

f n|n<0=0|1>0=sum.fst.span(>0)$mapM(pure[1,0])[1..64]!!n

Might allocate quite a lot of memory, try it online!

Maybe you want to test it with a smaller constant: Try 8-bit!

Explanation

Instead of using mapM(pure[0,1])[1..64] to convert the input to binary, we'll use mapM(pure[1,0])[1..64] which essentially generates the inverted strings \$\lbrace0,1\rbrace^{64}\$ in lexicographic order. So we can just sum the \$1\$s-prefix by using sum.fst.span(>0).

ბიმო

Posted 2018-12-09T23:08:42.553

Reputation: 15 345

4

05AB1E, 10 9 bytes

·bg65αsd*

I/O are both integers

Try it online or verify all test cases.

Explanation:

·         # Double the (implicit) input
          #  i.e. -1 → -2
          #  i.e. 4503599627370496 → 9007199254740992
 b        # Convert it to binary
          #  i.e. -2 → "ÿ0"
          #  i.e. 9007199254740992 → 100000000000000000000000000000000000000000000000000000
  g       # Take its length
          #  i.e. "ÿ0" → 2
          #  i.e. 100000000000000000000000000000000000000000000000000000 → 54
   65α    # Take the absolute different with 65
          #  i.e. 65 and 2 → 63
          #  i.e. 65 and 54 → 11
      s   # Swap to take the (implicit) input again
       d  # Check if it's non-negative (>= 0): 0 if negative; 1 if 0 or positive
          #  i.e. -1 → 0
          #  i.e. 4503599627370496 → 1
        * # Multiply them (and output implicitly)
          #  i.e. 63 and 0 → 0
          #  i.e. 11 and 1 → 11

Kevin Cruijssen

Posted 2018-12-09T23:08:42.553

Reputation: 67 575

3

Jelly,  10  9 bytes

-1 thanks to a neat trick by Erik the Outgolfer (is-non-negative is now simply )

ḤBL65_×AƑ

A monadic Link accepting an integer (within range) which yields an integer.

Try it online! Or see the test-suite.


The 10 was ḤBL65_ɓ>-×

Here is another 10 byte solution, which I like since it says it is "BOSS"...

BoṠS»-~%65

Test-suite here

...BoṠS63r0¤i, BoṠS63ŻṚ¤i, or BoṠS64ḶṚ¤i would also work.


Another 10 byter (from Dennis) is æ»64ḶṚ¤Äċ0 (again æ»63r0¤Äċ0 and æ»63ŻṚ¤Äċ0 will also work)

Jonathan Allan

Posted 2018-12-09T23:08:42.553

Reputation: 67 804

9 bytes – Erik the Outgolfer – 2018-12-11T18:18:13.660

@EriktheOutgolfer I thought to myself "there must be a golfier way to multiply by isNonNegative" and didn't think of the Ƒ quick at all, very nice work! – Jonathan Allan – 2018-12-11T19:11:20.397

1Actually, I've been theorizing about for quite some while now. Be warned that it doesn't vectorize! ;-) It's actually "flatten and then check if all elements are nonnegative". – Erik the Outgolfer – 2018-12-11T19:14:46.630

3

Powershell, 51 bytes

param([long]$n)for(;$n-shl$i++-gt0){}($i,65)[!$n]-1

Test script:

$f = {

param([long]$n)for(;$n-shl$i++-gt0){}($i,65)[!$n]-1

}

@(
    ,(-1                   ,0 )
    ,(-9223372036854775808 ,0 )
    ,(9223372036854775807  ,1 )
    ,(4611686018427387903  ,2 )
    ,(1224979098644774911  ,3 )
    ,(9007199254740992     ,10)
    ,(4503599627370496     ,11)
    ,(4503599627370495     ,12)
    ,(2147483648           ,32)
    ,(2147483647           ,33)
    ,(2                    ,62)
    ,(1                    ,63)
    ,(0                    ,64)
) | % {
    $n,$expected = $_
    $result = &$f $n
    "$($result-eq$expected): $result"
}

Output:

True: 0
True: 0
True: 1
True: 2
True: 3
True: 10
True: 11
True: 12
True: 32
True: 33
True: 62
True: 63
True: 64

mazzy

Posted 2018-12-09T23:08:42.553

Reputation: 4 832

3

Java 8, 38 bytes

int f(long n){return n<0?0:f(n-~n)+1;}

Input as long (64-bit integer), output as int (32-bit integer).

Port of @l4m2's C (gcc) answer.

Try it online.

Explanation:

 int f(long n){       // Recursive method with long parameter and integer return-type
   return n<0?        //  If the input is negative:
           0          //   Return 0
          :           //  Else:
           f(n-~n)    //   Do a recursive call with n+n+1
                  +1  //   And add 1

EDIT: Can be 26 bytes by using the builtin Long::numberOfLeadingZeros as displayed in @lukeg's Java 8 answer.

Kevin Cruijssen

Posted 2018-12-09T23:08:42.553

Reputation: 67 575

3

APL+WIN, 34 bytes

+/×\0=(0>n),(63⍴2)⊤((2*63)××n)+n←⎕

Explanation:

n←⎕ Prompts for input of number as integer

((2*63)××n) If n is negative add 2 to power 63

(63⍴2)⊤ Convert to 63 bit binary

(0>n), Concatinate 1 to front of binary vector if n negative, 0 if positive

+/×\0= Identify zeros, isolate first contiguous group and sum if first element is zero

Graham

Posted 2018-12-09T23:08:42.553

Reputation: 3 184

3

C# (Visual C# Interactive Compiler), 42 bytes

x=>x!=0?64-Convert.ToString(x,2).Length:64

Try it online!

C# (Visual C# Interactive Compiler), 31 bytes

int c(long x)=>x<0?0:c(x-~x)+1;

Even shorter, based off of @l4m2's C (gcc) answer. Never knew that you could declare functions like that, thanks @Dana!

Try it online!

Embodiment of Ignorance

Posted 2018-12-09T23:08:42.553

Reputation: 7 014

2

Swift (on a 64-bit platform), 41 bytes

Declares a closure called f which accepts and returns an Int. This solution only works correctly 64-bit platforms, where Int is typealiased to Int64. (On a 32-bit platform, Int64 can be used explicitly for the closure’s parameter type, adding 2 bytes.)

let f:(Int)->Int={$0.leadingZeroBitCount}

In Swift, even the fundamental integer type is an ordinary object declared in the standard library. This means Int can have methods and properties, such as leadingZeroBitCount (which is required on all types conforming to the standard library’s FixedWidthInteger protocol).

NobodyNada - Reinstate Monica

Posted 2018-12-09T23:08:42.553

Reputation: 510

interesting. reminds me of Rust. i think it should count as 20 bytes, .leadingZeroBitCount – don bright – 2019-03-10T02:20:45.727

2

Perl 5, 37 bytes

sub{sprintf("%064b",@_)=~/^0*/;$+[0]}

Try it online!

Or this 46 bytes if the "stringification" is not allowed: sub z

sub{my$i=0;$_[0]>>64-$_?last:$i++for 1..64;$i}

Kjetil S.

Posted 2018-12-09T23:08:42.553

Reputation: 1 049

s/length$&/$+[0]/ (-3 bytes) ;) – Dada – 2018-12-10T12:59:21.410

IMO, you're not allowed to remove the sub keyword from answers containing Perl 5 functions. – nwellnhof – 2018-12-10T14:36:41.213

I've seen whats similar to removing sub in answers for other languages, perl6, powershell and more. – Kjetil S. – 2018-12-10T14:58:40.397

In Perl6, I think you don't need sub{} to make a (anonymous?) sub, which explain why it's omitted from Perl6 answers. I agree with @nwellnhof that you shouldn't be allowed to remove sub. (when I was still active, like a year ago or so, that was the rule) – Dada – 2018-12-10T15:03:29.277

changed now. And included $+[0]. – Kjetil S. – 2018-12-10T15:10:06.453

2

Haskell, 24 bytes

f n|n<0=0
f n=1+f(2*n+1)

Try it online!

This is basically the same as Kevin Cruijssen's Java solution, but I found it independently.

The argument should have type Int for a 64-bit build, or Int64 for anything.

Explanation

If the argument is negative, the result is immediately 0. Otherwise, we shift left, filling in with ones, until we reach a negative number. That filling lets us avoid a special case for 0.

Just for reference, here's the obvious/efficient way:

34 bytes

import Data.Bits
countLeadingZeros

dfeuer

Posted 2018-12-09T23:08:42.553

Reputation: 1 016

1

Clean, 103 bytes

Uses the same "builtin" as ceilingcat's answer.

f::!Int->Int
f _=code {
instruction 243
instruction 72
instruction 15
instruction 189
instruction 192
}

Try it online!

Clean, 58 bytes

import StdEnv
$0=64
$i=until(\e=(2^63>>e)bitand i<>0)inc 0

Try it online!

Οurous

Posted 2018-12-09T23:08:42.553

Reputation: 7 916

1

Stax, 10 bytes

în»╧3(∞┼⌠g

Run and debug it

It's a port of Kevin's 05AB1E solution.

recursive

Posted 2018-12-09T23:08:42.553

Reputation: 8 616

1

Wolfram Language (Mathematica), 41 bytes

The formula for positive numbers is just 63-Floor@Log2@#&. Replacement rules are used for the special cases of zero and negative input.

The input need not be a 64-bit signed integer. This will effectively take the floor of the input to turn it into an integer. If you input a number outside of the normal bounds for a 64-bit integer, it will tell return a negative number indicating how many more bits would be needed to store this integer.

63-Floor@Log2[#/.{_?(#<0&):>2^63,0:>.5}]&

Try it online!

@LegionMammal978's solution is quite a bit shorter at 28 bytes. The input must be an integer. Per the documentation: "BitLength[n] is effectively an efficient version of Floor[Log[2,n]]+1. " It automatically handles the case of zero correctly reporting 0 rather than -∞.

Wolfram Language (Mathematica), 28 bytes

Boole[#>=0](64-BitLength@#)&

Try it online!

Kelly Lowder

Posted 2018-12-09T23:08:42.553

Reputation: 3 225

1Boole[#>=0](64-BitLength@#)& is a good bit shorter at 28 bytes. It uses the same basic concept as yours, but applies BitLength and Boole. – LegionMammal978 – 2018-12-12T02:13:08.193

I totally forgot about BitLength! – Kelly Lowder – 2018-12-14T02:29:33.213

1

Perl 5 -p, 42 bytes

1while$_>0&&2**++$a-1<$_;$_=0|$_>=0&&64-$a

Try it online!

Longer than a bitstring based solution, but a decent math based solution.

Xcali

Posted 2018-12-09T23:08:42.553

Reputation: 7 671

Doesn't really work if I'm not mistaken – Dada – 2018-12-11T12:23:44.340

@Dada I see that there are a few cases where the floating point division doesn't quite work out properly. I put in an int call that should solve the issue. – Xcali – 2018-12-11T15:55:26.203

Sorry, I failed my copy-past it would seem. This is what I wanted to send ;)

– Dada – 2018-12-11T17:36:33.063

1

APL(NARS), 15 chars, 30 bytes

{¯1+1⍳⍨⍵⊤⍨64⍴2}

test for few numbers for see how to use:

  f←{¯1+1⍳⍨⍵⊤⍨64⍴2}
  f ¯9223372036854775808
0
  f 9223372036854775807
1

RosLuP

Posted 2018-12-09T23:08:42.553

Reputation: 3 036

1

Rust, 18 bytes

i64::leading_zeros

Try it online!

Hannes Karppila

Posted 2018-12-09T23:08:42.553

Reputation: 3 090

1

K (ngn/k), 6 bytes

64-#2\

Try it online!

2\ encode the argument in binary

# length

64- subtract from 64

ngn

Posted 2018-12-09T23:08:42.553

Reputation: 11 449

# = length ... looks string based – Titus – 2018-12-13T14:37:38.310

2@Titus 2\ gives a list of integers and # finds its length. no strings are involved here. – ngn – 2018-12-13T15:38:41.420

1

PHP, 50 46 bytes

for(;0<$n=&$argn;$n>>=1)$i++;echo$n<0?0:64-$i;

Run as pipe with -R or try it online,

<?=$argn<0?0:0|64-log($argn+1,2); has rounding issues; so I took the long way.

Titus

Posted 2018-12-09T23:08:42.553

Reputation: 13 814

1

bitNumber - math.ceil (math.log(number) / math.log(2))

e.g 64 bit NUMBER : 9223372036854775807 math.ceil (math.log(9223372036854775807) / math.log(2)) ANS: 63

user90293

Posted 2018-12-09T23:08:42.553

Reputation: 11

If you could add the language name to this, that would be great,as people are likely to down vote answers that don't have the language name included – Lyxal – 2019-11-08T09:44:37.587

0

Charcoal, 15 bytes

I⁻⁶⁴L↨﹪NX²¦⁶⁴¦²

Try it online! Link is to verbose version of code. Explanation:

    L           Length of
       N        Input as a number
      ﹪         Modulo
         ²      Literal 2
        X       To the power
           ⁶⁴   Literal 64
     ↨          Converted to base
              ² Literal 2
 ⁻              Subtracted from
  ⁶⁴            Literal 64
I               Cast to string
                Implicitly print

The ¦s serve to separate adjacent integer literals. Conveniently, Charcoal's arbitrary numeric base conversion converts 0 into an empty list, however for negative numbers it simply inverts the sign of each digit, so the number is converted to the equivalent unsigned 64-bit integer first.

Neil

Posted 2018-12-09T23:08:42.553

Reputation: 95 035

0

Java 8, 49 bytes (broken)


I misread the problem, check back later for a new solution

n->{int i=0;while(n%10==0){n/=10;i++;}return i;};

Try it online!
I opted for a loop-based solution instead of recursion or built-ins.

Benjamin Urquhart

Posted 2018-12-09T23:08:42.553

Reputation: 1 262

This doesn't work for some of the test cases, for example it returns 0 for 2 and 2147483647. But if it did work, something you could golf is to replace the while loop with for(;n%10==0;i++)n/=10; to save 3 bytes – Embodiment of Ignorance – 2019-03-10T02:29:41.533

I didn't realize I was counting zeros of the binary form of the number. Mega oof – Benjamin Urquhart – 2019-03-10T02:34:08.500