[Question]
  [
## Goal
Given a non-negative integer, create a function that returns the starting position of number of largest consecutive 1's in that integer's binary value.
When given an input `0`, return `0`.
If the number has multiple streaks of equal length, you must return the position of the last streak.
## Input
An integer greater than **or equal to** 0.
## Output
An integer calculated as explained below.
## Rules
* This is code-golf, so the shortest code in bytes in each language wins.
* Standard loopholes are forbidden.
---
## Examples and Test Cases
### Example 1
* Your function is passed the integer 142
* 142 is equal to 10001110 in binary
* Longest streak is "111" (a streak of three ones)
* The streak starts at the 2^1 position
* **Your function returns 1 as the result**
### Example 2
* Your function is passed the integer 48
* 48 is equal to 110000 in binary
* Longest streak is "11" (a streak of two ones)
* The streak starts at the 2^4 position
* **Your function returns 4 as the result**
### Example 3
* Your function is passed the integer 750
* 750 is equal to 1011101110 in binary
* Longest streak is "111" (a streak of three ones)
* Since there are two streaks of equal length, we return the later streak.
* The later streak starts at the 2^5 position
* **Your function returns 5 as the result**
  
[Answer]
# JavaScript (ES6), ~~33~~ 32 bytes
```
f=x=>x&(k=x>>1)?f(x&k):k&&1+f(k)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zbbC1q5CTSPbtsLOzlDTPk2jQi1b0ypbTc1QO00jW/N/cn5ecX5Oql5OfrpGmoaBpiYXqoihiRGGmIkFhpC5KVDrfwA "JavaScript (Node.js) – Try It Online")
---
# JavaScript (ES6), ~~41 40 36~~ 34 bytes
*Saved 4 bytes thanks to @ThePirateBay*
```
f=x=>(k=x&x/2)?f(k):Math.log2(x)|0
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zbbC1k4j27ZCrULfSNM@TSNb08o3sSRDLyc/3UijQrPG4H9yfl5xfk4qSEQjTcNAU5MLVcTQxAhDzMQCQ8jcFKj1PwA "JavaScript (Node.js) – Try It Online")
### How?
**General case x > 0**
We recursively AND the input ***x*** with ***x / 2*** which progressively reduces the patterns of consecutive set bits until only the rightmost bit of the sequence remains. We keep a copy of the last non-zero value and determine the position of its most significant bit by floor-rounding its base-2 logarithm.
Below are the steps we go through for ***x = 750*** (***1011101110*** in binary).
```
    1011101110 --.
,----------------'
'-> 1011101110
AND 0101110111
    ----------
=   0001100110 --.
,----------------'
'-> 0001100110
AND 0000110011
    ----------
=   0000100010 --.  --> output = position of MSB = 5  (0000100010)
,----------------'                                         ^
'-> 0000100010
AND 0000010001
    ----------
=   0000000000
```
**Edge case x = 0**
The special case `x = 0` immediately leads to `Math.log2(0) | 0`. The logarithm of `0` evaluates to `-Infinity` and the binary bitwise OR forces a coercion to a 32-bit integer. In compliance with [the specification of the abstract operation **ToInt32**](https://www.ecma-international.org/ecma-262/6.0/#sec-toint32), this gives the expected `0`:
> 
> If *number* is **NaN**, **+0**, **−0**, **+∞**, or **−∞**, return
> **+0**
> 
> 
> 
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~10~~ 8 bytes
```
Ba\ÐƤṀċ¬
```
[Try it online!](https://tio.run/##AS8A0P9qZWxsef//QmFcw5DGpOG5gMSLwqz/xbzDh@KCrEf//zE0MiwgNDgsIDc1MCwgMA "Jelly – Try It Online")
### How it works
```
Ba\ÐƤṀċ¬  Main link. Argument: n
B         Binary; convert n to base 2.
   ÐƤ     Apply the link to the left to all postfixes of the binary array.
 a\         Cumulatively reduce by logical AND.
          For example, the array
          [1, 0, 1, 1, 1, 0, 1, 1, 1, 0]
          becomes the following array of arrays.
          [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
             [0, 0, 0, 0, 0, 0, 0, 0, 0]
                [1, 1, 1, 0, 0, 0, 0, 0]
                   [1, 1, 0, 0, 0, 0, 0]
                      [1, 0, 0, 0, 0, 0]
                         [0, 0, 0, 0, 0]
                            [1, 1, 1, 0]
                               [1, 1, 0]
                                  [1, 0]
                                     [0]
     Ṁ    Take the lexicographical maximum, i.e., the array with the most 1's at
          the beginning, breaking ties by length.
       ¬  Yield the logical NOT of n, i.e., 0 if n > 0 and 1 if n = 0.
      ċ   Count the occurrences of the result to the right in the one to the left.
```
[Answer]
# x86/x86-64 machine code, 14 bytes
(TODO: 13 bytes with input in EAX, return value in EDX).
Using [@Arnauld's algorithm](https://codegolf.stackexchange.com/questions/143000/calculate-the-longest-series-of-1s-in-an-integers-binary-value/143004#143004) of `x &= x>>1` and taking the highest set bit position in the step before `x` becomes `0`.
Callable from C/C++ with signature `unsigned longest_set(unsigned edi);` in the x86-64 System V ABI. The same machine-code bytes will decode the same way in 32-bit mode, but the standard 32-bit calling conventions don't put the first arg in `edi`. (Changing the input register to `ecx` or `edx` for Windows `_fastcall` / `_vectorcall` or `gcc -mregparm` could be done without breaking anything.)
```
   address   machine-code
             bytes
                         global longest_set
 1                       longest_set:
 2 00000000 31C0             xor  eax, eax    ; 0 for input = 0
 3                       
 4                       .loop:               ; do{
 5 00000002 0FBDC7           bsr  eax, edi    ;  eax = position of highest set bit
 6                           ;; bsr leaves output unmodified when input = 0 (and sets ZF)
 7                           ;; AMD documents this; Intel manuals say unspecified but Intel CPUs implement it
 8 00000005 89F9             mov  ecx, edi
 9 00000007 D1E9             shr  ecx, 1
10 00000009 21CF             and  edi, ecx
11 0000000B 75F5             jnz .loop        ; } while (x &= x>>1);
12                       
13 0000000D C3               ret
```
x86's [`BSR` instruction](http://felixcloutier.com/x86/BSR.html) (Bit Scan Reverse) is perfect for this, giving us the bit-index directly, rather than counting leading zeros. (`bsr` doesn't directly produce 0 for input=0 like `32-lzcnt(x)` would, but we need bsr=31-lzcnt for non-zero inputs, so `lzcnt` wouldn't even save instructions, let alone byte count. Zeroing eax before the loop works because of `bsr`'s semi-official behaviour of leaving the destination unmodified when the input is zero.)
This would be even shorter if we could return the MSB position of the longest run. In that case, `lea ecx, [rdi+rdi]` (3 bytes) would copy + *left*-shift instead of `mov` + `shr` (4 bytes).
See [this TIO link](https://tio.run/##VVLRatswFH3XV5ynkrA02G26lpi0lI5BHjYKYy8roci2HKvIkvGVM2ej3@5Kslun98FG5@qce3x8OZGoUnU815yqvldG7wXZZxJ2zeCqMw0geLfwDw8gQYTCoVLXrcUGEWNLZUy9xqdKkJv/QSKlD4lchk7Q2qA2JK00GqZAKfelGww3GKm0gZgkgasEPwiCaa0f2OrKFFLk@FsKPZnAjOvcswl/vs/f6fc/vjkbWVsJ7Rq2lLTAVluhUHHdckUgfnSSVItMBtXUyQ03Hh5/E2RVK@HZGD1V5uDsZ8PHBITKZkTicPY@fHPhwYC86H8IEU3ZvDr7UgnMOpxt0N3exvOEhcuNsKzfK5NyhWeyvLFseK3ZZAANOf2nhuov8dfdCR7mOlzuxkGFbFyqK6RH6zMswJv94SneLYGfxgqXCbe4Q8kJ978etltkJheIustiyZJJ90O43g1wLrIRPv3lTjw7H2LIuFI4WadT92M8vPuE@Q25uBzYdKQgMEmLTtqZ2wy3NHPWRyxeXbDVDbu@ivo7V28 "Assembly (nasm, x64, Linux) – Try It Online") for an asm caller that does `exit(longest_set(argc-1));`
Testing with a shell loop:
```
l=(); for ((i=0;i<1025;i++));do 
    ./longest-set-bitrun "${l[@]}";   # number of args = $i
    printf "$i %#x: $?\n" $i; 
    l+=($i); 
done | m
0 0: 0
1 0x1: 0
2 0x2: 1
3 0x3: 0
4 0x4: 2
5 0x5: 2
6 0x6: 1
7 0x7: 0
8 0x8: 3
9 0x9: 3
10 0xa: 3
11 0xb: 0
12 0xc: 2
13 0xd: 2
14 0xe: 1
15 0xf: 0
16 0x10: 4
17 0x11: 4
18 0x12: 4
19 0x13: 0
20 0x14: 4
21 0x15: 4
...
747 0x2eb: 5
748 0x2ec: 5
749 0x2ed: 5
750 0x2ee: 5
751 0x2ef: 0
752 0x2f0: 4
753 0x2f1: 4
754 0x2f2: 4
```
---
## 13 bytes with a custom calling convention
We could save a byte by taking the arg in EAX and returning in EDX: then we could use `cdq` (1 byte) to zero the return value in the case where input = 0 (so `bsr` leaves it unmodified). The value in other cases doesn't matter, overwritten by `bsr`.
So `cdq` replaces `xor eax, eax`, saving a byte.
We do need a `0` in a different register to (not) overwrite; in the non-zero case we can't let `bsr` replace the input value with its log2. So we can't avoid the instruction entirely, and taking input + returning in EAX (like GCC regparm) would mean we'd need to start with `mov edx, eax` or something to copy it somewhere else, while leaving the return value 0 for the input=0 case.
[Answer]
# [Python 2](https://docs.python.org/2/), 45 bytes
```
f=lambda x:f(x&x/2)if x&x/2else len(bin(x))-3
```
[Try it online!](https://tio.run/##HcdBDoIwEAXQfU/xV9AmQ9QC0ZDgRYyLEjtxkjoQZFFPXwm795bf9p7Vl8JjCp/pFZAHtrnKJ@@EcSCmb0SKaidRm51r2hIw4nEmtIRL5wnXfk93exrD8wqBKMJgsKyiG4RqNHfUBLbiyh8 "Python 2 – Try It Online")
*Saved plenty of bytes thanks to Dennis! (The heads up for `len(bin(...))-3` instead of `math.frexp`)*
*Thanks to @xnor for finding a bug, which fortunately was easily fixable!*
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~19~~  ~~17~~ 11 bytes
```
HÐĿ&\ḟ0ṪBL’
```
[Try it online!](https://tio.run/##AR4A4f9qZWxsef//SMOQxL8mXOG4nzDhuapCTOKAmf///zA "Jelly – Try It Online")
*-6 (!) bytes thanks to @Dennis's keen observations*
**How it Works**
```
HÐĿ&\ḟ0ṪBL’
HÐĿ         - halve repeatedly until reaching 0 due to rounding
   &\       - cumulative AND
     ḟ0Ṫ    - final non-zero, or 0 if all elements are 0
        BL  - length of binary representation (log base 2). 0->[0]->1
          ’ - decrement
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~14~~ ~~12~~ 11 bytes
```
bDγàŠrkrJgα
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/yeXc5sMLji4oyi7ySj@38f9/c1MDAA) or [run test cases](https://tio.run/##MzBNTDJM/X9u6/8kl3ObDy84uqAou8gr/dzG//@jDU2MdEwsdMxNDXQMYgE).
## Explanation
```
bDγàŠrkrJgα  Implicit input (ex: 750)
bD           Convert input to binary string and duplicate
                 '1011101110', '1011101110'
  γ          Split into groups of 1s or 0s
                 '1011101110', ['1', '0', '111', '0', '111', '0']
   à         Pull out max, parsing each as an integer
                 '1011101110', ['1', '0', '0', '111', '0'], '111'
    Šrk      Rearrange stack and get first index of max run of ones
                 ['1', '0', '0', '111', '0'], 2
       rJg   Reverse stack, join the array to a string, and get its length
                 2, 7
          α  Get absolute difference
                 5
```
[Answer]
# [Perl 6](https://perl6.org), ~~45~~ 35 bytes
*This highly improved version is courtesy of @nwellnhof.*
```
{.msb+1-(.base(2)~~m:g/1+/).max.to}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJKmYKvwv1ovtzhJ21BXQy8psThVw0izri7XKl3fUFtfUy83sUKvJL/2vzVXcSJIvYZKvKZCWn6RgqGJkY6JhY65qcF/AA "Perl 6 – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), ~~89~~ 78 bytes
```
m=t=i=j=0
for c in bin(input()):
 t=-~t*(c>'0');i+=1
 if t>m:j=i;m=t
print i-j
```
[Try it online!](https://tio.run/##Fc1BDoIwEADA@75iw4VWQ0IJRlKyvMIXWEvYRkrTLFEvfr3iB2bSR5YtdsVtD09VVZWVhJgCtTBvGR1yxDtHxTHtorS2gELNV07KTXVb65HPZAB5RplWG4jHA4CUOQpyE8pBArwWfnq85d1b9G/v8L8VA6bv4HppoR9@ "Python 2 – Try It Online")
EDIT: Saved 11 bytes thanks to Mr. Xcoder.
[Answer]
# [J](http://jsoftware.com/), ~~18~~ 17 bytes
```
(#-0{#\\:#.~\)@#:
```
[Try it online!](https://tio.run/##y/qfVqxga6VgoADE/zWUdQ2qlWNirJT16mI0HZSt/mtycSnpKain2VqpK@go1FoppBVzcaUmZ@QraFinaSoZKMQqGJoYKZhYKJibGgBNAAA)
## Explanation
```
(#-0{#\\:#.~\)@#:  Input: integer n
               #:  Binary
     #\            Length of each prefix
       \:          Sorted down using
         #.~\      Mixed base conversion on each prefix
   0{              Get the value at index 0
  -                Subtract from
 #                 Length
```
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 64 60 bytes
```
T=a=>(a&a/2)>0?T(a&a/2):Math.Log(a,2)<0?0:(int)Math.Log(a,2)
```
[Try it online!](https://tio.run/##dYy9CsIwGEVn8xSZJIFaY6go/R0EpxYEC84fMdRATaBJBSl99lhEB8Eul8s93CPsSphO@t4q3eDz0zp5T5BowVp8GpB14JTAx16LVGkX4Cly7OsMspzAEtac5qyoPzWuwN3C0jQEAk5TVrCYTAf6M/sEfbUPo664AqUJRQNaHIy2ppXhpVNOlkpLUhNGafKfbCI@y6L9LNpt38oRjf4F "C# (.NET Core) – Try It Online")
A C# version of [@Arnauld's answer](https://codegolf.stackexchange.com/a/143004/73579)
### Acknowledgements
4 bytes saved thanks to Kevin Cruijssen.
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 131 123+18=141 bytes
```
a=>{string s=Convert.ToString(a,2),t=s.Split('0').OrderBy(x=>x.Length).Last();return a<1?0:s.Length-s.IndexOf(t)-t.Length;}
```
[Try it online!](https://tio.run/##dY9RS8MwFIWfza/I2xJoQzcmilkrKAhCZUILPof0OgM10dzb0VH622sdE3zYXi6c83Eu51hMbYgwdej8jlcHJPjU7L9SpfPfmtnWIPLXgSEZcpY/dd5unKeEz6fgNc/5ZPJiQIq/Wcwfg99DJFWH6mgJk6xkQjmq6qt1JBbZQqptbCA@HESfF70qwe/oQ6rSIAmpI1AXPTeb5X12hyeaonr2DfTbd0EypZOrx0mzv2b74Br@YpwXkg3sau6BoQX1Fh3BvAVELTIp9XmyXK8usvXtRXRzfXw5snH6AQ "C# (.NET Core) – Try It Online")
+18 bytes for `using System.Linq;`
### Acknowledgements
8 bytes saved thanks to Grzegorz Puławski.
### Degolfed
```
a=>{
    string s=Convert.ToString(a,2),      // Convert to binary
    t=s.Split('0')                       // get largest group of 1's
       .OrderBy(x=>x.Length)
       .Last();
    return 
        a<1?0:                          // handle 0 case
        s.Length-s.IndexOf(t)-t.Length; // get position in reversed string
}
```
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 164 161 bytes
```
a=>{var s=Convert.ToString(a,2);int c=0,l=0,p=0,k=0,j=s.Length-1,i=j;for(;i>=0;i--){if(s[i]>'0'){if(i==j||s[i+1]<'1'){p=i;c=0;}if(++c>=l){l=c;k=p;}}}return j-k;}
```
[Try it online!](https://tio.run/##dY7NasMwEITP1VPoZhn/YIeUFJT1pdBTCoUYeig5CFVJ13YlI8mG4ujZXRHorTnMwM63DCNdIY1V6@RQX@jxx3n1zYkchHP0bSHOC4@Svkxa7lH7nEZraEuBrgKaZRaWOng2elbWl605ehtrmMg3KY@fVEKVD1FjVB/VgSsPSl/8V1HnCB0/G8s4NlBxLIp0wTNzH3hqkiq5HQjQXa8xyurTPqljOALy2MpDpFkmGxjSZQDJexh5CMEqP1lNu6LnYeXkb/9s8JO@CtQsJQt5iIOdGVT5btGrA2rFWlalKf@f1NvNXbZ9uot2j7fKQML6Cw "C# (.NET Core) – Try It Online")
A non-`Linq` solution, which I'm sure could be improved, though nothing is immediately apparent.
### Degolfed
```
a=>{
    var s=Convert.ToString(a,2); // Convert to binary
    int c=0,l=0,p=0,k=0,j=s.Length-1,i=j;
    for(;i>=0;i--)               // Loop from end of string
    {
        if(s[i]>'0')             // if '1'
        {
            if(i==j||s[i+1]<'1') // if first digit or previous digit was '0'
            {
                p=i;             // set the position of the current group
                c=0;             // reset the count
            }
            if(++c>=l)           // if count is equal or greater than current largest count
            {
                l=c;             // update largest count
                k=p;             // store position for this group
            }
        }
    }
    return j-k;                  // as the string is reversed, return string length minus position of largest group
}
```
[Answer]
# [Python 3](https://docs.python.org/3/), 46 bytes
```
f=lambda x:x&x>>1and f(x&x>>1)or len(bin(x))-3
```
[Try it online!](https://tio.run/##JczLCoMwEEbhfZ7iX9UMRPDWm2BepHQxYkMHdBRxkT59DHT3LQ5n@x3fVduUwjDzMk6M2MdL9L5mnRDs37TumD9qR1Ebico2MQa8Koe6axzu16zu4fBsbm9jQq4FouDeYNtFDyuuQOlRuLwUonQC "Python 3 – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 12 bytes
```
→S§-€¤≠Lo▲gḋ
```
[Try it online!](https://tio.run/##ATgAx/9odXNr/yBtyK9z4oKBcsK2/@KGklPCpy3igqzCpOKJoExv4payZ@G4i////zAKMTQyCjQ4Cjc1MA "Husk – Try It Online")
### Explanation
```
                 Implicit input, e.g                           750
           ḋ     Convert to binary                             [1,0,1,1,1,0,1,1,1,0]
          g      Group equal elements                          [[1],[0],[1,1,1],[0],[1,1,1],[0]]
        o▲       Maximum                                       [1,1,1]
    €            Index of that substring in the binary number  3
     ¤≠L         Absolute difference of lengths                abs (3 - 10) = 7
 S§-             Subtract the two                              7 - 3 = 4
→                Increment                                     5
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~14 13 12~~ 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Bµṣ0Ṫ$ƤMḢạL
```
A monadic link taking and returning non-negative integers.
**[Try it online!](https://tio.run/##ASQA2/9qZWxsef//4bmjMOG5qgpCwrXDh8akTeG4ouG6oUz///83NTA "Jelly – Try It Online")** or see the [test-suite](https://tio.run/##ATkAxv9qZWxsef//QsK14bmjMOG5qiTGpE3huKLhuqFM/zQ3UjtAMCwxNDIsNDgsNzUwwrXFvMOH4oKsR/8 "Jelly – Try It Online").
### How?
```
Bµṣ0Ṫ$ƤMḢạL - Main link: number, n                   e.g. 750
B           - convert to a binary list                    [1,0,1,1,1,0,1,1,1,0]
 µ          - monadic chain separation, call that b
      Ƥ     - map over prefixes:  (i.e. for [1], [1,0], [1,0,1], [1,0,1,1],...)
     $      -   last two links as a monad:                e.g.: [1,0,1,1,1,0,1,1]
   0        -     literal zero                                   0
  ṣ         -     split (prefix) at occurrences of (0)           [[1],[1,1,1],[1,1]]
    Ṫ       -     tail                                                        [1,1]
       M    - maximal indices                             [5,9]
        Ḣ   - head                                        5
          L - length (of b)                               10
         ạ  - absolute difference                         5
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 19 bytes
```
BŒgḄṀB
BwÇɓÇL+ɓBL_‘
```
[Try it online!](https://tio.run/##ASsA1P9qZWxsef//QsWSZ@G4hOG5gEIKQnfDh8mTw4dMK8mTQkxf4oCY////NzUw "Jelly – Try It Online")
Thanks to Jonathan Allan for saving ~~4~~ 6 bytes!
I've worked too much to just abandon this, although it is kind of long. I really wanted to add a solution that literally searches for the longest substring of `1`s in the binary representation...
# Explanation
```
BŒgḄṀB  - Monadic helper link. Will be used with Ç in the next link.
B       - Binary representation.
 Œg     - Group runs of consecutive equal elements.
   Ḅ    - Convert from binary to integer.
    Ṁ   - Maximum value.
     B  - Convert from integer to binary.
BwÇɓÇL+ɓBL_‘  - Main link.
B             - Binary representation (of the input).
  Ç           - Last link as a monad. Takes the input integer.
 w            - First sublist index.
   ɓ          - Start a separate dyadic chain. Reverses the arguments.
    Ç         - Last link as a monad.
     L        - Length.
      +       - Addition
       ɓ      - Start a separate dyadic chain. Reverses the arguments.
        B     - Binary.
         L    - Length.
          _‘  - Subtract and increment the result (because Jelly uses 1-indexing).
              - Implicitly output.
```
[Answer]
# [Kotlin](https://kotlinlang.org), 77 bytes
```
{val b=it.toString(2)
b.reversed().lastIndexOf(b.split(Regex("0+")).max()!!)}
```
## Beautified
```
{
    val b = it.toString(2)
    // Find the left position of the first instance of
    b.reversed().lastIndexOf(
            // The largest group of 1s
            b.split(Regex("0+")).max()!!)
}
```
## Test
```
var s:(Int)->Int =
{val b=it.toString(2)
b.reversed().lastIndexOf(b.split(Regex("0+")).max()!!)}
fun main(args: Array<String>) {
    r(0, 0)
    r(142, 1)
    r(48, 4)
    r(750, 5)
}
fun r(i: Int, i1: Int) {
    var v = s(i)
    println("$i -> $v [$i1] ${i1 == v}")
}
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~101~~ ~~98~~ ~~96~~ 75 bytes
```
snd.maximum.(`zip`[0..]).c
c 0=[0]
c n|r<-c$div n 2=sum[r!!0+1|mod n 2>0]:r
```
[Try it online!](https://tio.run/##FcjRCsIgFADQ977iDvZQtMndWBSR/YgJE82SvCbaImLfntXTgXNV@Xb2vlh@KjkYRurlaCK2HN8ujgIZkyumFxqQC5Q/w5wOra6Ne0KAnueJRKoqXHcz3c2/jij3qZByATjE5MIDaiAVwYLAphv6Ztg12w3K8tHWq0surY7xCw "Haskell – Try It Online") Usage: `snd.maximum.(`zip`[0..]).c $ 142` yields `1`.
### Explanation:
* `c` converts the input into binary while at the same time counting the length of streaks of one, collecting the results in a list. `r<-c$div n 2` recursively computes the rest `r` of this list, while `sum[r!!0+1|mod n 2>0]:r` adds the current length of the streak to `r`. The list comprehension checks if `mod n 2>0`, that is whether the current binary digit is a one, and if so, takes the length of the previous streak (the first element of `r`) and adds one. Otherwise the list comprehension is empty, and `sum[]` yields `0`. 
For the example input, `c 142` yields the list `[0,3,2,1,0,0,0,1,0]`.
* `(`zip`[0..])` adds the position to each element of the previous list as the second component of a tuple. For the example this gives `[(0,0),(3,1),(2,2),(1,3),(0,4),(0,5),(0,6),(1,7),(0,8)]`.
* `maximum` finds the lexicographically maximal tuple in this list, that is the streak lengths are considered first as they are the first component, and in the event of a tie the second component, namely the larger index, decides. This yields `(3,1)` in the example, and `snd` returns the second component of the tuple.
[Answer]
# Google Sheets, 94 bytes
```
=Len(Dec2Bin(A1))-Find(MAX(Split(Dec2Bin(A1),0)),Dec2Bin(A1))-Len(MAX(Split(Dec2Bin(A1),0)))+1
```
No, it's not very pretty. It'd be real nice to be able to store `Dec2Bin(A1)` as a variable for reference.
Key point: Like Excel, the `Dec2Bin` function has a max input value of `511`. Anything larger than that returns an error, as seen below.
[![Results](https://i.stack.imgur.com/ONvG4.png)](https://i.stack.imgur.com/ONvG4.png)
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~81~~ 80 bytes
```
i,l,c,r;f(n){for(i=l=c=r=0;n;n/=2,i++)c=n&1?c+1:c>=l?r=i-(l=c),0:0;n=c<l?r:i-c;}
```
[Try it online!](https://tio.run/##bcpBCsIwEIXhfU8RhEpCp9iUFEvj2Iu4kSmRgTpKcFd69pguuhHf5ofHR/WDKCWGGQiiD1rMEl5RM85IGLHx4uWELXBVGUI52pEqO9AV5zEi1zozA82QHdIlfwPX5Nf0vLNoUyyFyntHlk/Qh3JSVpXTTQ6grGtBBZ1jjP9Vbleu35Dr/5huN@eu2VDOptb0BQ "C (gcc) – Try It Online")
# [C (gcc)](https://gcc.gnu.org/), 43 bytes
A C version of @Arnauld's answer
```
k;f(n){n=(k=n&n/2)?f(k):(k=log2(n))<0?0:k;}
```
[Try it online!](https://tio.run/##bcpBCoMwFATQvacIguV/UBpDpGIqXqQbiSRI4m@p7sSrN00WbkpnMwzzdGW1DsEpA4Q79eB6utBV4GDAYRenf1oRP7zzgXdOHWEZZwLM9ozFvN4zbQbyYmI1K6YH5SWrpSiZgViI6lfJU8k2Idn@Mc1pbg1PKFZSR/ho40e7hsovXw "C (gcc) – Try It Online")
[Answer]
# MS SQL Server, 437 426 407 398 bytes
[SQL Fiddle](http://sqlfiddle.com/#!6/5806d)
I'm sure that I could remove line breaks, etc, but this is as compact as I was willing to make it:
```
create function j(@ int)
returns int
as BEGIN
declare @a varchar(max)='',@b int,@c int=0,@d int=0,@e int=0,@f int=0,@g int=0,@h int=0
while @>0 BEGIN SELECT @a=cast(@%2 as char(1))+@a,@=@/2
END
SET @b=len(@a)
while @<@b
BEGIN
select @c=@d,@d=cast(substring(@a,@b-@,1)as int)
IF @d=1
BEGIN IF @c=0
SELECT @e=@,@g=1
else SET @g+=1 END
IF @g>=@h BEGIN select @h=@g,@f=@e END
SET @+=1
END
return @f
END
```
Here's a more readable version:
```
create function BinaryString(@id int)
returns int
as BEGIN
  declare @bin varchar(max)
  declare @binLen int
  declare @pVal int = 0
  declare @Val int = 0
  declare @stC int = 0 --start of current string of 1s
  declare @stB int = 0 --start of biggest string of 1s
  declare @lenC int = 0 --length of current string of 1s
  declare @lenB int = 0 --length of biggest string of 1s
 
  set @bin = ''
  
    while @id>0
      BEGIN
        SET @bin = cast(@id%2 as varchar(1)) + @bin
        SET @id = @id/2
      END
    SET @binLen = len(@bin)
    
    while @id<@binLen
      BEGIN
        set @pVal = @Val
        set @Val = cast(substring(@bin,@binLen-@id,1) as int)
        IF @Val = 1 and @pVal = 0
          BEGIN 
            SET @stC = @id
            SET @lenC = 1
          END
        IF @Val = 1 and @pVal = 1
          BEGIN 
            SET @lenC = @lenC + 1
          END
        IF @lenC >= @lenB
          BEGIN
            set @lenB = @lenC
            set @StB = @StC
          END
          
        SET @id = @id + 1 
      END
      
  return @StB
END
```
The real trick is that as far as I could find, there is no native SQL functionality to convert a number from decimal to binary. As a result, I had to code the conversion to binary manually, then I could compare that as a string, one character at a time until I found the right number.
I'm sure there's a better way to do this, but I didn't see a(n) SQL answer, so I figured I'd throw it out there.
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 21 bytes ([SBCS](https://github.com/abrudz/SBCS))
[−1 thanks to rabbitgrowth.](https://chat.stackexchange.com/transcript/message/59264944#59264944)
Requires `⎕IO←0` which is default on many systems.
```
⊃⌽⍸b⍷⍨∨⌿↑⊆⍨b←⌽2⊥⍣¯1⊢⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qG9qZv6jtgkGXI862tP@P@pqftSz91HvjqRHvdsf9a541LHiUc/@R20TH3W1AblJQJVAeaNHXUsf9S4@tN7wUdcioAn/gXr/p3EZmhhxpXGZWAAJc1MDIGkAAA "APL (Dyalog Unicode) – Try It Online")
`⎕` prompt for input
`⊢` yield that (separates `¯1` from `⎕`)
`2⊥⍣¯1` convert to base-2, using as many positions as needed
`⌽` reverse
`b←` store as `b` (for **b**inary)
`⊆⍨b` self-partition `b` (i.e. the 1-streaks of `b`)
`↑` mix (make list of lists into matrix, padding with zeros)
`∨⌿` vertical OR reduction (yields longest streak)
`b⍷⍨` mark the start positions of that in `b`
`⍸` **ɩ**ndices of those starting positions
`⌽` reverse
`⊃` pick the first one (yields zero if none available)
[Answer]
# [MATL](https://github.com/lmendo/MATL), 15 bytes
```
`tt2/kZ&t]xBnq&
```
[Try it online!](https://tio.run/##y00syfn/P6GkxEg/O0qtJLbCKa9Q7f9/AwA "MATL – Try It Online")
Uses the half and AND idea. The `k` is necessary only to make it terminate for `1` - for some reason, 1 AND 0.5 returns 1, causing an infinite loop.
(alternate solution: `BtnwY'tYswb*&X>)-` by converting to binary and run-length encoding)
[Answer]
# R, 117 Bytes
```
z=rev(Reduce(function(x,y)ifelse(y==1,x+y,y),strtoi(intToBits(scan())),,,T));ifelse(!sum(z),0,33-which.max(z)-max(z))
```
[Answer]
# Excel VBA, ~~54~~ 44 Bytes
-10 Bytes thanks to @EngineerToast
Anonymous VBE immediate window function that takes input from range `[A1]` and outputs to the VBE immediate window
```
?Instr(1,StrReverse([Dec2Bin(A1)]),1)+[A1>0]
```
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~24~~ 21 bytes
```
63|-⊃⍒+/×\↑,⍨\⎕⊤⍨64⍴2
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQZcjzra0/6bGdfoPupqftQ7SVv/8PSYR20TdR71rogBqnrUtQTIMjN51LvF6D9Q7f80LkMTI640LhMLIGFuagAkDQA "APL (Dyalog Classic) – Try It Online")
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 17 bytes
This is a recursive function. The link includes `y` in order to call the function on the given input.
```
L|&K.&b/b2yKsl|b1
```
**[Verify all test cases.](https://pyth.herokuapp.com/?code=L%7C%26K.%26b%2Fb2yKsl%7Cb1y&input=750&test_suite=1&test_suite_input=750%0A142%0A48%0A0&debug=0)**
# [Pyth](https://github.com/isaacg1/pyth), 20 bytes
```
-lK.B|Q1+xKJeScK\0lJ
```
**[Verify all test cases.](https://pyth.herokuapp.com/?code=-lK.B%7CQ1%2BxKJeScK%5C0lJ&input=750&test_suite=1&test_suite_input=750%0A142%0A48%0A0&debug=0)**
[Answer]
# R, 66 Bytes
```
function(i){a=rle(intToBits(i));max(0,a[[1]][which(a[[2]]==1)])}
```
Explanation:
```
function(i){
  a = rle(                  # Run-length encode
    intToBits(i)            # The bits in i
  );                        # And assign it to a.
  max(0,                    # Return the maximum of zero and
      a[[1]][               # The lengths of a,
        which(a[[2]]==1)    # But only those where the repeated bit is 1
        ])
}
```
[Answer]
# [Python 2](https://docs.python.org/2/), 41 bytes
```
f=lambda n:n/2and(n&n/2<1)+f(n&n/2or n/2)
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIc8qT98oMS9FI08NyLAx1NROgzDzixSApOb/NBBDITNPoSgxLz1Vw9DAQFM72sRCR8HQxEhHwdzUINaKS6GgKDOvRCFPRwGoWfM/AA "Python 2 – Try It Online")
Based on [this solution by Mr. Xcoder](https://codegolf.stackexchange.com/a/143028/20260).
[Answer]
# Javascript, 54 chars
```
f=i=>i.toString(2).split(0).sort().reverse()[0].length
```
* `i.toString(2)` gets the binary string for the integer.
* The `.split(0)` gets each sequential ones part in an array element.
* `.sort().reverse()` gets us the highest value as first.
* The `[0].length` gives us the length of that first value.
[Answer]
# Perl 5, 45 + 1 (-p)
```
(sprintf"%b",$_)=~/(1+)(?!.*1\1)/;$_=length$'
```
If you write this out on the command line of most shells, you may have to type this as:
```
perl -pE'(sprintf"%b",$_)=~/(1+)(?!.*1\1)/;$_=length$'"'"
```
The dance of the quotes at the end is just to get perl see a `'`, which would otherwise be consumed by the shell.
[Answer]
# [Retina](https://github.com/m-ender/retina), ~~52~~ 43 bytes
Convert to binary, then replace with the length of what follows the largest string of ones.
```
.*
$*
+`(1+)\1
$+0
01
1
$'¶
O`
A-3`
^1+
.
```
[**Try it online**](https://tio.run/##K0otycxL/K/qrpHA9V9Pi0tFi0s7QcNQWzPGkEtF24DLwJDLkItLRf3QNi7/BC5HXeMErjhDbS4uvf//DbgMTYy4TCy4zE0NAA) - all test cases
*Saved 9 bytes thanks to Martin.*
]