- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Longest distance between 1s in binary JavaScript

We are required to write a JavaScript function that in a positive integer, say n. The function should find and return the longest distance between any two adjacent 1's in the binary representation of n.

If there are no two adjacent 1's, then we have to return 0.

Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.

For example −

If the input is 22, then the output should be 2,

because,

- The binary code for 22 is 10110
- The first adjacent pair of 1's is "10110" with a distance of 2.
- The second adjacent pair of 1's is "10110" with a distance of 1.
- The answer is the largest of these two distances, which is 2.

Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.

## Example

const num = 22; const binaryGap = (num = 1) => { let last = -1; let ans = 0; // go through every bit for (let i = 0; i < 32; i++) { // check whether the bit is `1`. // if true, calculate the longest distance with // previous `1` if `1` was previously found. if ((num >> i) & 1 > 0) { if (last >= 0) { ans = Math.max(ans, i - last); } last = i; } } return ans; }; console.log(binaryGap(num));

## Output

And the output in the console will be −

2

- Related Articles
- Program to find longest distance of 1s in binary form of a number using Python
- Finding longest line of 1s in a matrix in JavaScript
- Calculating 1s in binary representation of numbers in JavaScript
- Program to find longest consecutive run of 1s in binary form of n in Python
- Sorting according to number of 1s in binary representation using JavaScript
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Distance of nearest 0 in binary matrix in JavaScript
- Check if a binary string has a 0 between 1s or not in C++
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array in C++
- Shortest distance between objects in JavaScript
- Binary Tree Longest Consecutive Sequence in C++
- Find Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python
- Find Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in C++
- Find distance between two nodes of a Binary Tree in C++
- Hamming Distance between two strings in JavaScript

Advertisements