Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created October 27, 2011 14:46
Show Gist options
  • Save tsaniel/1319739 to your computer and use it in GitHub Desktop.
Save tsaniel/1319739 to your computer and use it in GitHub Desktop.
Count the consecutive zero bits
function(a){
return
';<W=XIS>YQOJTL?CZVHRPNKBUGMAF@ED'// the lookup table
.charCodeAt((a&-a)*125613361>>>27) - 59 // the unique index
}
function(a){return';<W=XIS>YQOJTL?CZVHRPNKBUGMAF@ED'.charCodeAt((a&-a)*125613361>>>27)-59}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "countZeros",
"description": "Count the consecutive zero bits on the right",
"keywords": [
"bitwise",
"binary",
"zeros"
]
}
<!DOCTYPE html>
<title>log base 2</title>
<div>Expected value: <b>30</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(a){return';<W=XIS>YQOJTL?CZVHRPNKBUGMAF@ED'.charCodeAt((a&-a)*125613361>>>27)-59};
document.getElementById( "ret" ).innerHTML = myFunction(1073741824);
</script>
@maettig
Copy link

maettig commented Nov 17, 2011

Here is a shorter version (90 bytes).

function(a){return';<W=XIS>YQOJTL?CZVHRPNKBUGMAF@ED'.charCodeAt((a&-a)*125613361>>>27)-59}

@tsaniel
Copy link
Author

tsaniel commented Dec 3, 2011

@maettig: Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment