C# Binary Gap
static int BinaryGap(int n)
{
    var binary = Convert.ToString(n, 2).Trim('0');
    var gap = 0;

    foreach (var zero in binary.Split('1'))
    {
        gap = Math.Max(gap, zero.Length);
    }

    return gap;
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Elixir Binary Gap
defmodule BinaryGap do
  def binary_gap(n) do
    n
    |> Integer.to_string(2)
    |> String.trim("0")
    |> String.split("1")
    |> Enum.map(&String.length/1)
    |> Enum.max()
  end
end

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Erlang Binary Gap
-module(binary_gap).
-export([binary_gap/1]).

binary_gap(N) ->
    Bin = integer_to_list(N, 2),
    Trimmed = string:trim(Bin, both, "0"),
    Groups = string:split(Trimmed, "1", all),
    lists:max([length(G) || G <- Groups]).

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Go Binary Gap
func binaryGap(n int) int {
	trimmed := strings.Trim(strconv.FormatInt(int64(n), 2), "0")

	gap := 0
	for _, zeroes := range strings.Split(trimmed, "1") {
		if len(zeroes) > gap {
			gap = len(zeroes)
		}
	}

	return gap
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Haskell Binary Gap
import Data.Char (intToDigit)
import Data.List (dropWhileEnd)
import Numeric (showIntAtBase)

binaryGap :: Int -> Int
binaryGap n = maximum (0 : map length (words spaced))
  where
    bin     = showIntAtBase 2 intToDigit n ""
    trimmed = dropWhileEnd (== '0') (dropWhile (== '0') bin)
    spaced  = map (\c -> if c == '1' then ' ' else c) trimmed

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Java Binary Gap
public class Solution {
    public static int binaryGap(int n) {
        String bin = Integer.toBinaryString(n).replaceAll("^0+|0+$", "");
        String[] zeroes = bin.split("1", -1);

        int gap = 0;
        for (String zero : zeroes) {
            gap = Math.max(gap, zero.length());
        }

        return gap;
    }
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Lisp Binary Gap
(defun binary-gap (n)
  (let* ((bits (write-to-string n :base 2))
         (trimmed (string-trim "0" bits))
         (groups (split-on-char trimmed #\1))
         (gap 0))
    (dolist (zero groups)
      (setf gap (max gap (length zero))))
    gap))

(defun split-on-char (s ch)
  (loop with start = 0
        for pos = (position ch s :start start)
        collect (subseq s start pos)
        while pos
        do (setf start (1+ pos))))

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

PHP Binary Gap
function binaryGap(int $n): int
{
    $gap    = 0;
    $zeroes = explode('1', trim(decbin($n), '0'));
    foreach ($zeroes as $zero) {
        $len = strlen($zero);
        $gap = $len > $gap ? $len : $gap;
    }

    return $gap;
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Python Binary Gap
def binary_gap(n: int) -> int:
    zeroes = bin(n)[2:].strip("0").split("1")
    return max((len(z) for z in zeroes), default=0)

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Rust Binary Gap
fn binary_gap(n: i64) -> i64 {
    let bits = format!("{n:b}");
    let trimmed = bits.trim_matches('0');

    trimmed
        .split('1')
        .map(|zeroes| zeroes.len() as i64)
        .max()
        .unwrap_or(0)
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.