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

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

• 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. – Arnauld Dec 9 '18 at 23:26
• Can we return False instead of 0? – Jo King Dec 9 '18 at 23:33
• @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 Dec 9 '18 at 23:40
• 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. – slebetman Dec 10 '18 at 5:43
• @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 Dec 11 '18 at 0:26

# Charcoal, 15 bytes

Ｉ⁻⁶⁴Ｌ↨﹪ＮＸ²¦⁶⁴¦²


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

    Ｌ           Length of
Ｎ        Input as a number
﹪         Modulo
²      Literal 2
Ｘ       To the power
⁶⁴   Literal 64
↨          Converted to base
² Literal 2
⁻              Subtracted from
⁶⁴            Literal 64
Ｉ               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.

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