Skip to content

Instantly share code, notes, and snippets.

@markdrake
Created December 30, 2015 16:43
Show Gist options
  • Save markdrake/51bd04b973337cb60aa4 to your computer and use it in GitHub Desktop.
Save markdrake/51bd04b973337cb60aa4 to your computer and use it in GitHub Desktop.
function binaryGapFnc(N) {
var binary = N.toString(2);
var regex = /10+1/g
var matches = binary.match(regex);
if(!matches) { return 0; }
var binaryGap = 0;
for(var i = 0; i < matches.length; i++) {
if (matches[i].length > binaryGap) {
binaryGap = matches[i].length - 2; // We remove the two 1's
}
}
return binaryGap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment