# Calculate the longest series of 1's in an integer's binary value

## 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
• You need a winning criterion, like code-golf
– Okx
Commented Sep 16, 2017 at 19:51
• @Okx It had been mentioned in the body itself so I added the tag. Commented Sep 16, 2017 at 20:04
• Make sure people test 0. That's an important test case. Commented Sep 16, 2017 at 23:39
• Instead of "last streak" or "latest streak", I'd suggest "streak with the largest place value". Commented Sep 16, 2017 at 23:50
• @Okx Why is a winning criterion necessary? Why can't it simply be a puzzle? Commented Sep 18, 2017 at 18:46

# Objective-C, 96 chars

for(int h=0,c=0,l=0,p=0;;x/=2,p++){if(x%2==0){if(c<=h){h=c;l=p;}c=0;if(x==0){return p;}}else{c++;}}

• If x is even the highest count is checked and count resets
• If x is also 0 then return the highest count
• If x isn't even, then the last digit is 1 and count increments
• x is divided by two
• Isn't this supposed to return the bit position of the highest count, not the count value. Commented Sep 18, 2017 at 20:07
• Yes it is. I did not catch that. Thank you for pointing that out. Commented Sep 18, 2017 at 23:12

# Java 8, 81 bytes

int c(int n){int x=(int)(Math.log(n)/Math.log(2));return(n&=n/2)>0?c(n):x<0?0:x;}


Try it here.

# Javascript, 71 bytes

I was thinking about regular expressions and...

x=>+x.toString(2).replace(/.*?0?(1*)(?!.*\1[1])(.*)/,(x,m,a)=>a.length)


# Python 3, 83 bytes

len(bin(num)[2:].split('1'*(max(list(map(len,bin(num)[2:].split('0'))))))[1])


Try it online!

It does not support the condition that two or more arrays of 1s are longest.

# Mathematica, 44 bytes

I feel like I can trim off a few more bytes, though I'm not sure how yet.

We save space by using an assignment within If, using #0 for pure function recursion, and using infix notation when possible.

If[(k=BitAnd[#,#/2])>0,#0@k,Log2@#~BitOr~0]&


Try it online!

# Zsh, 53 bytes

b=$[[##2]$1];set ${(Ons:0:)b};<<<$[$#b-${(SE)b#$1}+1]  b is argument $1 in binary. Then we set $1 again, to the longest sequence of 1s in b. ${(SE)b#$1 finds the first occurrence (from the left) of $1 in b, and returns the index+1 of the last matching char. However since the question is counting by powers of 2 we do a subtraction from the length of b.

Other approaches reverse b, but that took more bytes.