Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BinaryGap, javascript - Find longest sequence of zeros in binary representation of an integer.
// javascript var iTheNumber = 85, iTheZeros = 0; function binaryGap(num, theZeros, binary) { var iGapCurrently = 0, iGapHigher = 0; sBinary = (iTheNumber >>> 0).toString(2); for (var position = 0; position < sBinary.length; position++) { if (sBinary.charAt(position) == '0') { iGapCurrently += 1; iTheZeros += 1; } else { if (iGapCurrently > iGapHigher) iGapHigher = iGapCurrently; iGapCurrently = 0; } if (iGapCurrently > iGapHigher) iGapHigher = iGapCurrently; } print("Binaryr: " + sBinary); print("Sequence of zeros: " + iTheZeros); return iGapHigher; } print("Number: " + iTheNumber); print("Gap Higher: " + binaryGap(iTheNumber));
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Add more strength to input color
empty
a pipeline generator
Dum
BF.JS
class Stack
Strange Attractors
LazyListJS
Aa
JS exam part 2 - Count Smileys
Please log in to post a comment.