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


  • 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
  • 14
    \$\begingroup\$ Welcome 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. \$\endgroup\$ – Arnauld Dec 9 '18 at 23:26
  • 1
    \$\begingroup\$ Can we return False instead of 0? \$\endgroup\$ – Jo King Dec 9 '18 at 23:33
  • 4
    \$\begingroup\$ @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? \$\endgroup\$ – Dave Dec 9 '18 at 23:40
  • 4
    \$\begingroup\$ Several 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. \$\endgroup\$ – slebetman Dec 10 '18 at 5:43
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Dave Dec 11 '18 at 0:26

32 Answers 32

1 2

Charcoal, 15 bytes


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.


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.

  • \$\begingroup\$ 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 \$\endgroup\$ – Embodiment of Ignorance Mar 10 '19 at 2:29
  • \$\begingroup\$ I didn't realize I was counting zeros of the binary form of the number. Mega oof \$\endgroup\$ – Benjamin Urquhart Mar 10 '19 at 2:34
1 2

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.