32
5
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
1You need a winning criterion, like [tag:code-golf] – Okx – 2017-09-16T19:51:06.197
@Okx It had been mentioned in the body itself so I added the tag. – totallyhuman – 2017-09-16T20:04:09.243
Make sure people test
0
. That's an important test case. – mbomb007 – 2017-09-16T23:39:47.3272Instead of "last streak" or "latest streak", I'd suggest "streak with the largest place value". – aschepler – 2017-09-16T23:50:55.817
@Okx Why is a winning criterion necessary? Why can't it simply be a puzzle? – corsiKa – 2017-09-18T18:46:59.350
@corsiKa Because those are the rules on PPCG. – Okx – 2017-09-18T18:51:40.397
@Okx Interesting - apparently "puzzle" is specifically defined as "I have this code that you must change to win the puzzle" - an arbitrary definition that has apparently confused more than just myself in the past. Thank you for the accurate, if terse, response. – corsiKa – 2017-09-18T19:30:46.157
Congratulations to Dennis! A solution of only 8 bytes, written in Jelly. https://codegolf.stackexchange.com/a/143030/67327
– Defacto – 2017-09-18T21:41:35.557