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
code-golf math binary bitwise
New contributor
add a comment |
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
code-golf math binary bitwise
New contributor
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 returnFalse
instead of0
?
– 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
add a comment |
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
code-golf math binary bitwise
New contributor
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
code-golf math binary bitwise
New contributor
New contributor
New contributor
asked 3 hours ago
Dave
64
64
New contributor
New contributor
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 returnFalse
instead of0
?
– 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
add a comment |
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 returnFalse
instead of0
?
– 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
add a comment |
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!
Builtins strike again /s
– Logern
31 mins ago
add a comment |
up vote
1
down vote
Python 3, 34 bytes
f=lambda n:-1<n<2**63and-~f(2*n|1)
Try it online!
add a comment |
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.
add a comment |
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.
add a comment |
up vote
0
down vote
PowerShell, 44 bytes
param($a)64-[Convert]::ToString($a,2).Length
Takes input from a commandline argument.
add a comment |
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$.
add a comment |
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
add a comment |
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
This has precision issues; 4611686018427387903 returns 1.
– Dennis♦
40 mins ago
add a comment |
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!
This doesn't seem right, only leading zeroes should be counted.
– xnor
1 hour ago
add a comment |
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!
Builtins strike again /s
– Logern
31 mins ago
add a comment |
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!
Builtins strike again /s
– Logern
31 mins ago
add a comment |
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!
x86_64 machine language on Linux, 6 bytes
0: f3 48 0f bd c7 lzcnt %rdi,%rax
5: c3
Try it online!
answered 2 hours ago
ceilingcat
3,069719
3,069719
Builtins strike again /s
– Logern
31 mins ago
add a comment |
Builtins strike again /s
– Logern
31 mins ago
Builtins strike again /s
– Logern
31 mins ago
Builtins strike again /s
– Logern
31 mins ago
add a comment |
up vote
1
down vote
Python 3, 34 bytes
f=lambda n:-1<n<2**63and-~f(2*n|1)
Try it online!
add a comment |
up vote
1
down vote
Python 3, 34 bytes
f=lambda n:-1<n<2**63and-~f(2*n|1)
Try it online!
add a comment |
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!
Python 3, 34 bytes
f=lambda n:-1<n<2**63and-~f(2*n|1)
Try it online!
answered 2 hours ago
ovs
18.5k21059
18.5k21059
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
Jelly, 10 bytes
ḤBL65_ɓ>-×
A monadic Link accepting an integer (within range) which yields an integer.
Try it online! Or see the test-suite.
answered 2 hours ago
Jonathan Allan
50.4k534165
50.4k534165
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 2 hours ago
answered 2 hours ago
xnor
89.4k18184437
89.4k18184437
add a comment |
add a comment |
up vote
0
down vote
PowerShell, 44 bytes
param($a)64-[Convert]::ToString($a,2).Length
Takes input from a commandline argument.
add a comment |
up vote
0
down vote
PowerShell, 44 bytes
param($a)64-[Convert]::ToString($a,2).Length
Takes input from a commandline argument.
add a comment |
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.
PowerShell, 44 bytes
param($a)64-[Convert]::ToString($a,2).Length
Takes input from a commandline argument.
edited 3 hours ago
answered 3 hours ago
Gabriel Mills
1415
1415
add a comment |
add a comment |
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$.
add a comment |
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$.
add a comment |
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$.
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$.
edited 2 hours ago
answered 3 hours ago
Arnauld
70.8k688298
70.8k688298
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 2 hours ago
answered 3 hours ago
Jo King
20k245105
20k245105
add a comment |
add a comment |
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
This has precision issues; 4611686018427387903 returns 1.
– Dennis♦
40 mins ago
add a comment |
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
This has precision issues; 4611686018427387903 returns 1.
– Dennis♦
40 mins ago
add a comment |
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
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
answered 2 hours ago
Suever
9,4721345
9,4721345
This has precision issues; 4611686018427387903 returns 1.
– Dennis♦
40 mins ago
add a comment |
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
add a comment |
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!
This doesn't seem right, only leading zeroes should be counted.
– xnor
1 hour ago
add a comment |
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!
This doesn't seem right, only leading zeroes should be counted.
– xnor
1 hour ago
add a comment |
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!
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!
answered 1 hour ago
BMO
10.9k21881
10.9k21881
This doesn't seem right, only leading zeroes should be counted.
– xnor
1 hour ago
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 of0
?– 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