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











up vote
1
down vote

favorite












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 compliment 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 compliment)
-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









share|improve this question







New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    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
    3 hours ago






  • 1




    Can we return False instead of 0?
    – Jo King
    3 hours ago










  • @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
    2 hours ago

















up vote
1
down vote

favorite












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 compliment 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 compliment)
-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









share|improve this question







New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    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
    3 hours ago






  • 1




    Can we return False instead of 0?
    – Jo King
    3 hours ago










  • @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
    2 hours ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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 compliment 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 compliment)
-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









share|improve this question







New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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 compliment 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 compliment)
-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






code-golf math binary bitwise






share|improve this question







New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Dave

64




64




New contributor




Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3




    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
    3 hours ago






  • 1




    Can we return False instead of 0?
    – Jo King
    3 hours ago










  • @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
    2 hours ago
















  • 3




    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
    3 hours ago






  • 1




    Can we return False instead of 0?
    – Jo King
    3 hours ago










  • @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
    2 hours ago










3




3




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
3 hours ago




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
3 hours ago




1




1




Can we return False instead of 0?
– Jo King
3 hours ago




Can we return False instead of 0?
– Jo King
3 hours ago












@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
2 hours ago






@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
2 hours ago












9 Answers
9






active

oldest

votes

















up vote
5
down vote













x86_64 machine language on Linux, 6 bytes



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


Try it online!






share|improve this answer





















  • Builtins strike again /s
    – Logern
    31 mins ago


















up vote
1
down vote














Python 3, 34 bytes





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


Try it online!






share|improve this answer




























    up vote
    1
    down vote














    Jelly, 10 bytes



    ḤBL65_ɓ>-×


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



    Try it online! Or see the test-suite.






    share|improve this answer




























      up vote
      1
      down vote














      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.






      share|improve this answer






























        up vote
        0
        down vote













        PowerShell, 44 bytes



        param($a)64-[Convert]::ToString($a,2).Length


        Takes input from a commandline argument.






        share|improve this answer






























          up vote
          0
          down vote














          JavaScript (Node.js), 25 bytes



          Takes input as a BigInt literal.





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


          Try it online!



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






          share|improve this answer






























            up vote
            0
            down vote














            Perl 6, 35 bytes





            $!={$_??($_>0)*($!($_/2+|0)-1)!!64}


            Try it online!



            32 bytes if we can return false instead of 0.





            $!={$_??$_>0&&$!($_/2+|0)-1!!64}


            Try it online!



            28 bytes if we can use string operations:





            {.fmt("%064b")~~/^(0)*/;+$0}


            Try it online!



            Anonymous code block that takes a number and returns a number.



            Explanation:



            {                         }  # Anonymous code block
            .fmt("%064b") # Format as a binary number with 64 digits
            ~~ # Smartmatch against
            /^(0)*/ # A regex counting leading zeroes
            ;+$0 # Return the number of leading zeroes





            share|improve this answer






























              up vote
              0
              down vote













              MATL, 14 bytes



              4Y%3Z%64&BfX<q


              Try it at MATL Online



              Explanation



                      % Implicitly grab input as a double floating point number
              4Y% % Cast the value to a signed 64-bit integer
              3Z% % Cast the result as an unsigned 64-bit integer
              64&B % Convert to a binary number with 64 bits
              f % Find the indices of all 1's
              X< % Find the index of the minimum
              q % Subtract 1 to get the number of leading 0's
              % Implicitly display the result





              share|improve this answer





















              • This has precision issues; 4611686018427387903 returns 1.
                – Dennis
                40 mins ago


















              up vote
              0
              down vote














              Haskell, 43 bytes





              f n|n<0=0|1>0=sum$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!






              share|improve this answer





















              • This doesn't seem right, only leading zeroes should be counted.
                – xnor
                1 hour ago











              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "200"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });






              Dave is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177271%2ffind-the-number-of-leading-zeroes-in-a-64-bit-integer%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote













              x86_64 machine language on Linux, 6 bytes



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


              Try it online!






              share|improve this answer





















              • Builtins strike again /s
                – Logern
                31 mins ago















              up vote
              5
              down vote













              x86_64 machine language on Linux, 6 bytes



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


              Try it online!






              share|improve this answer





















              • Builtins strike again /s
                – Logern
                31 mins ago













              up vote
              5
              down vote










              up vote
              5
              down vote









              x86_64 machine language on Linux, 6 bytes



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


              Try it online!






              share|improve this answer












              x86_64 machine language on Linux, 6 bytes



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


              Try it online!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 hours ago









              ceilingcat

              3,069719




              3,069719












              • Builtins strike again /s
                – Logern
                31 mins ago


















              • Builtins strike again /s
                – Logern
                31 mins ago
















              Builtins strike again /s
              – Logern
              31 mins ago




              Builtins strike again /s
              – Logern
              31 mins ago










              up vote
              1
              down vote














              Python 3, 34 bytes





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


              Try it online!






              share|improve this answer

























                up vote
                1
                down vote














                Python 3, 34 bytes





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


                Try it online!






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote










                  Python 3, 34 bytes





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


                  Try it online!






                  share|improve this answer













                  Python 3, 34 bytes





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


                  Try it online!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  ovs

                  18.5k21059




                  18.5k21059






















                      up vote
                      1
                      down vote














                      Jelly, 10 bytes



                      ḤBL65_ɓ>-×


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



                      Try it online! Or see the test-suite.






                      share|improve this answer

























                        up vote
                        1
                        down vote














                        Jelly, 10 bytes



                        ḤBL65_ɓ>-×


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



                        Try it online! Or see the test-suite.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote










                          Jelly, 10 bytes



                          ḤBL65_ɓ>-×


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



                          Try it online! Or see the test-suite.






                          share|improve this answer













                          Jelly, 10 bytes



                          ḤBL65_ɓ>-×


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



                          Try it online! Or see the test-suite.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          Jonathan Allan

                          50.4k534165




                          50.4k534165






















                              up vote
                              1
                              down vote














                              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.






                              share|improve this answer



























                                up vote
                                1
                                down vote














                                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.






                                share|improve this answer

























                                  up vote
                                  1
                                  down vote










                                  up vote
                                  1
                                  down vote










                                  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.






                                  share|improve this answer















                                  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.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited 2 hours ago

























                                  answered 2 hours ago









                                  xnor

                                  89.4k18184437




                                  89.4k18184437






















                                      up vote
                                      0
                                      down vote













                                      PowerShell, 44 bytes



                                      param($a)64-[Convert]::ToString($a,2).Length


                                      Takes input from a commandline argument.






                                      share|improve this answer



























                                        up vote
                                        0
                                        down vote













                                        PowerShell, 44 bytes



                                        param($a)64-[Convert]::ToString($a,2).Length


                                        Takes input from a commandline argument.






                                        share|improve this answer

























                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          PowerShell, 44 bytes



                                          param($a)64-[Convert]::ToString($a,2).Length


                                          Takes input from a commandline argument.






                                          share|improve this answer














                                          PowerShell, 44 bytes



                                          param($a)64-[Convert]::ToString($a,2).Length


                                          Takes input from a commandline argument.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited 3 hours ago

























                                          answered 3 hours ago









                                          Gabriel Mills

                                          1415




                                          1415






















                                              up vote
                                              0
                                              down vote














                                              JavaScript (Node.js), 25 bytes



                                              Takes input as a BigInt literal.





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


                                              Try it online!



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






                                              share|improve this answer



























                                                up vote
                                                0
                                                down vote














                                                JavaScript (Node.js), 25 bytes



                                                Takes input as a BigInt literal.





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


                                                Try it online!



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






                                                share|improve this answer

























                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote










                                                  JavaScript (Node.js), 25 bytes



                                                  Takes input as a BigInt literal.





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


                                                  Try it online!



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






                                                  share|improve this answer















                                                  JavaScript (Node.js), 25 bytes



                                                  Takes input as a BigInt literal.





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


                                                  Try it online!



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







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 2 hours ago

























                                                  answered 3 hours ago









                                                  Arnauld

                                                  70.8k688298




                                                  70.8k688298






















                                                      up vote
                                                      0
                                                      down vote














                                                      Perl 6, 35 bytes





                                                      $!={$_??($_>0)*($!($_/2+|0)-1)!!64}


                                                      Try it online!



                                                      32 bytes if we can return false instead of 0.





                                                      $!={$_??$_>0&&$!($_/2+|0)-1!!64}


                                                      Try it online!



                                                      28 bytes if we can use string operations:





                                                      {.fmt("%064b")~~/^(0)*/;+$0}


                                                      Try it online!



                                                      Anonymous code block that takes a number and returns a number.



                                                      Explanation:



                                                      {                         }  # Anonymous code block
                                                      .fmt("%064b") # Format as a binary number with 64 digits
                                                      ~~ # Smartmatch against
                                                      /^(0)*/ # A regex counting leading zeroes
                                                      ;+$0 # Return the number of leading zeroes





                                                      share|improve this answer



























                                                        up vote
                                                        0
                                                        down vote














                                                        Perl 6, 35 bytes





                                                        $!={$_??($_>0)*($!($_/2+|0)-1)!!64}


                                                        Try it online!



                                                        32 bytes if we can return false instead of 0.





                                                        $!={$_??$_>0&&$!($_/2+|0)-1!!64}


                                                        Try it online!



                                                        28 bytes if we can use string operations:





                                                        {.fmt("%064b")~~/^(0)*/;+$0}


                                                        Try it online!



                                                        Anonymous code block that takes a number and returns a number.



                                                        Explanation:



                                                        {                         }  # Anonymous code block
                                                        .fmt("%064b") # Format as a binary number with 64 digits
                                                        ~~ # Smartmatch against
                                                        /^(0)*/ # A regex counting leading zeroes
                                                        ;+$0 # Return the number of leading zeroes





                                                        share|improve this answer

























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote










                                                          Perl 6, 35 bytes





                                                          $!={$_??($_>0)*($!($_/2+|0)-1)!!64}


                                                          Try it online!



                                                          32 bytes if we can return false instead of 0.





                                                          $!={$_??$_>0&&$!($_/2+|0)-1!!64}


                                                          Try it online!



                                                          28 bytes if we can use string operations:





                                                          {.fmt("%064b")~~/^(0)*/;+$0}


                                                          Try it online!



                                                          Anonymous code block that takes a number and returns a number.



                                                          Explanation:



                                                          {                         }  # Anonymous code block
                                                          .fmt("%064b") # Format as a binary number with 64 digits
                                                          ~~ # Smartmatch against
                                                          /^(0)*/ # A regex counting leading zeroes
                                                          ;+$0 # Return the number of leading zeroes





                                                          share|improve this answer















                                                          Perl 6, 35 bytes





                                                          $!={$_??($_>0)*($!($_/2+|0)-1)!!64}


                                                          Try it online!



                                                          32 bytes if we can return false instead of 0.





                                                          $!={$_??$_>0&&$!($_/2+|0)-1!!64}


                                                          Try it online!



                                                          28 bytes if we can use string operations:





                                                          {.fmt("%064b")~~/^(0)*/;+$0}


                                                          Try it online!



                                                          Anonymous code block that takes a number and returns a number.



                                                          Explanation:



                                                          {                         }  # Anonymous code block
                                                          .fmt("%064b") # Format as a binary number with 64 digits
                                                          ~~ # Smartmatch against
                                                          /^(0)*/ # A regex counting leading zeroes
                                                          ;+$0 # Return the number of leading zeroes






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited 2 hours ago

























                                                          answered 3 hours ago









                                                          Jo King

                                                          20k245105




                                                          20k245105






















                                                              up vote
                                                              0
                                                              down vote













                                                              MATL, 14 bytes



                                                              4Y%3Z%64&BfX<q


                                                              Try it at MATL Online



                                                              Explanation



                                                                      % Implicitly grab input as a double floating point number
                                                              4Y% % Cast the value to a signed 64-bit integer
                                                              3Z% % Cast the result as an unsigned 64-bit integer
                                                              64&B % Convert to a binary number with 64 bits
                                                              f % Find the indices of all 1's
                                                              X< % Find the index of the minimum
                                                              q % Subtract 1 to get the number of leading 0's
                                                              % Implicitly display the result





                                                              share|improve this answer





















                                                              • This has precision issues; 4611686018427387903 returns 1.
                                                                – Dennis
                                                                40 mins ago















                                                              up vote
                                                              0
                                                              down vote













                                                              MATL, 14 bytes



                                                              4Y%3Z%64&BfX<q


                                                              Try it at MATL Online



                                                              Explanation



                                                                      % Implicitly grab input as a double floating point number
                                                              4Y% % Cast the value to a signed 64-bit integer
                                                              3Z% % Cast the result as an unsigned 64-bit integer
                                                              64&B % Convert to a binary number with 64 bits
                                                              f % Find the indices of all 1's
                                                              X< % Find the index of the minimum
                                                              q % Subtract 1 to get the number of leading 0's
                                                              % Implicitly display the result





                                                              share|improve this answer





















                                                              • This has precision issues; 4611686018427387903 returns 1.
                                                                – Dennis
                                                                40 mins ago













                                                              up vote
                                                              0
                                                              down vote










                                                              up vote
                                                              0
                                                              down vote









                                                              MATL, 14 bytes



                                                              4Y%3Z%64&BfX<q


                                                              Try it at MATL Online



                                                              Explanation



                                                                      % Implicitly grab input as a double floating point number
                                                              4Y% % Cast the value to a signed 64-bit integer
                                                              3Z% % Cast the result as an unsigned 64-bit integer
                                                              64&B % Convert to a binary number with 64 bits
                                                              f % Find the indices of all 1's
                                                              X< % Find the index of the minimum
                                                              q % Subtract 1 to get the number of leading 0's
                                                              % Implicitly display the result





                                                              share|improve this answer












                                                              MATL, 14 bytes



                                                              4Y%3Z%64&BfX<q


                                                              Try it at MATL Online



                                                              Explanation



                                                                      % Implicitly grab input as a double floating point number
                                                              4Y% % Cast the value to a signed 64-bit integer
                                                              3Z% % Cast the result as an unsigned 64-bit integer
                                                              64&B % Convert to a binary number with 64 bits
                                                              f % Find the indices of all 1's
                                                              X< % Find the index of the minimum
                                                              q % Subtract 1 to get the number of leading 0's
                                                              % Implicitly display the result






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered 2 hours ago









                                                              Suever

                                                              9,4721345




                                                              9,4721345












                                                              • This has precision issues; 4611686018427387903 returns 1.
                                                                – Dennis
                                                                40 mins ago


















                                                              • This has precision issues; 4611686018427387903 returns 1.
                                                                – Dennis
                                                                40 mins ago
















                                                              This has precision issues; 4611686018427387903 returns 1.
                                                              – Dennis
                                                              40 mins ago




                                                              This has precision issues; 4611686018427387903 returns 1.
                                                              – Dennis
                                                              40 mins ago










                                                              up vote
                                                              0
                                                              down vote














                                                              Haskell, 43 bytes





                                                              f n|n<0=0|1>0=sum$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!






                                                              share|improve this answer





















                                                              • This doesn't seem right, only leading zeroes should be counted.
                                                                – xnor
                                                                1 hour ago















                                                              up vote
                                                              0
                                                              down vote














                                                              Haskell, 43 bytes





                                                              f n|n<0=0|1>0=sum$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!






                                                              share|improve this answer





















                                                              • This doesn't seem right, only leading zeroes should be counted.
                                                                – xnor
                                                                1 hour ago













                                                              up vote
                                                              0
                                                              down vote










                                                              up vote
                                                              0
                                                              down vote










                                                              Haskell, 43 bytes





                                                              f n|n<0=0|1>0=sum$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!






                                                              share|improve this answer













                                                              Haskell, 43 bytes





                                                              f n|n<0=0|1>0=sum$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!







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered 1 hour ago









                                                              BMO

                                                              10.9k21881




                                                              10.9k21881












                                                              • This doesn't seem right, only leading zeroes should be counted.
                                                                – xnor
                                                                1 hour ago


















                                                              • This doesn't seem right, only leading zeroes should be counted.
                                                                – xnor
                                                                1 hour ago
















                                                              This doesn't seem right, only leading zeroes should be counted.
                                                              – xnor
                                                              1 hour ago




                                                              This doesn't seem right, only leading zeroes should be counted.
                                                              – xnor
                                                              1 hour ago










                                                              Dave is a new contributor. Be nice, and check out our Code of Conduct.










                                                              draft saved

                                                              draft discarded


















                                                              Dave is a new contributor. Be nice, and check out our Code of Conduct.













                                                              Dave is a new contributor. Be nice, and check out our Code of Conduct.












                                                              Dave is a new contributor. Be nice, and check out our Code of Conduct.
















                                                              If this is an answer to a challenge…




                                                              • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                              • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                Explanations of your answer make it more interesting to read and are very much encouraged.


                                                              • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                                              More generally…




                                                              • …Please make sure to answer the question and provide sufficient detail.


                                                              • …Avoid asking for help, clarification or responding to other answers (use comments instead).






                                                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                              Please pay close attention to the following guidance:


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177271%2ffind-the-number-of-leading-zeroes-in-a-64-bit-integer%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                                                              Alexandru Averescu

                                                              Trompette piccolo