Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Lone ​​​​​​ones

+3
−0

Given an integer, output the number of lone 1s in its binary representation.

Input

  • An integer from $0$ to $4294967295$ (that is, $2^{32}-1$). In binary that's from 00000000000000000000000000000000 to 11111111111111111111111111111111.
  • You may take input in any form that does not contribute to solving the challenge. For example:
    • A signed or unsigned integer of $32$ bits or more.
    • A string of decimal digits.
    • A binary string.
    • A sequence of bits.

Output

  • The number of 1s in the binary representation that do not have a 1 to their left or right.
  • For avoidance of doubt:
    • A 1 at the left end of the binary representation does not have a 1 to its left.
    • A 1 at the right end of the binary representation does not have a 1 to its right.

Examples

Input $4$ corresponds to 100 in binary. The output is $1$ because there is $1$ 1 bit that is not adjacent to another 1 bit.

Input $5$ corresponds to 101 in binary. The output is $2$ because there are $2$ 1 bits that are not adjacent to another 1 bit.

Input $6$ corresponds to 110 in binary. The output is $0$ because each of the 1 bits is adjacent to another 1 bit.

Test cases

Test cases are in the format input : output.

Unsigned integer test cases

0 : 0
4294967295 : 0
2147483648 : 1
1 : 1
2147483649 : 2
98304 : 0
1310720 : 2
4294639615 : 1
4294967294 : 0
2147483647 : 0
3221225471 : 1
4294967293 : 1
2863311530 : 16
1431655765 : 16
1717986918 : 0

Binary test cases

00000000000000000000000000000000 : 0
11111111111111111111111111111111 : 0
10000000000000000000000000000000 : 1
00000000000000000000000000000001 : 1
10000000000000000000000000000001 : 2
00000000000000011000000000000000 : 0
00000000000101000000000000000000 : 2
11111111111110101111111111111111 : 1
11111111111111111111111111111110 : 0
01111111111111111111111111111111 : 0
10111111111111111111111111111111 : 1
11111111111111111111111111111101 : 1
10101010101010101010101010101010 : 16
01010101010101010101010101010101 : 16
01100110011001100110011001100110 : 0

Scoring

This is a code golf challenge. Your score is the number of bytes in your code. Lowest score for each language wins.

Explanations are optional, but I'm more likely to upvote answers that have one.

History

0 comment threads

7 answers

+2
−0

C (gcc), 67 bytes

a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;return a;}

Try it online!


General algorithm:

  • This is a little state machine where the variable 'alone' a can have the value 0=zero, 1=alone candidate, 2=not alone. Essentially a 'carry' but with several states.

    The non-golfed code would look something like if(!a) a=1; else a=2; which becomes a=a?2:1.

  • Then the outer if-else non-golfed would look something like

    if(n&1l<<i)
      a=a?2:1;
    else
    {
      if(a&1)
        r++;
      a=0;
    }
    

    ...which turns into the one-liner ?: mess.

  • When the state machine has found an alone candidate and transits from value 1 to zero, increase count r

  • Whenever there's a zero, reset the state machine.

  • To solve the special case at the end where there might be a lone 1, iterate one more than 32 times to get comparison with a zero in there. Which will only work if the function parameter is not sign extended! To cheat I used long which is 64 bit on Linux that tio.run uses - won't work on 32 bit long systems. Making the parameter unsigned would be portable but unsigned has more digits than long. Same thing with the 1l prefix, which needs to be 1ull to be portable.

C-specific stuff:

  • Plain int is 32 bits.
  • Operator precedence ++ over << over & over ?: over = over ,.
  • Shifting negative numbers is of course wildly poorly-specified behavior but the code gets away with it on gcc/Linux.

Test cases:

  • Taken from the challenge. An ugly macro takes a digit and expected output, then prints OK or FAIL.
  • As usual, the function-only solution relying on zero init of global variables need to be reset by the caller between tests.
  • Also print test cases as hex because the decimal numbers really don't tell me much :)
History

1 comment thread

Missing default rule for using globals (4 comments)
+1
−0

Bash, 53 bytes

sed 's/\(.\)/\1\n/g' /dev/stdin|uniq -c|grep -c '1 1'
  1. Add a newline after each character.
  2. Count the number of characters in each group with uniq
  3. Count the number of groups with only one '1' with grep
History

3 comment threads

wrong output for some test cases (1 comment)
shorten? (1 comment)
How to use (3 comments)
+1
−0

Vyxal, 4 bytes

Ġċ†∑

Try it Online!

Takes input as a string of 32 bits.

Explained

Ġċ†∑­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌­
Ġ     # ‎⁡Group the input by consecutive values. Basically, split the input on runs.
 ċ    # ‎⁢Push whether each run != 1. Basically, an inverted check for "is this run a '1' or multiple 1s/0s".
  †   # ‎⁣Logical not each comparison, because the above check was inverted.
   ∑  # ‎⁤Summate the list, returning the count of truthy comparisons.
💎

Created with the help of Luminespire.

History

0 comment threads

+1
−0

Perl, 28 bytes

Takes input as newline-separated list of binary strings.

perl -ple's/11+//g;$_=y/1//'
  • -p - assign each line of input to $_, manipulate, then print
  • -l - strip trailing newline from input, add back to output
  • -e - program follows
  • s/11+//g - strip runs of two or more 1s
  • y/1// - count remaining 1s, assign to $_
History

0 comment threads

+1
−0

awk, 35 bytes

Takes input as newline-separated list of binary strings.

awk '{gsub(/11+|0+/,_);$0=length}1'
  • strip all runs of 0s and two or more 1s
  • print length of remaining string
History

0 comment threads

+1
−0

Bash, 28 bytes

Takes input as a single binary string.

grep -Po '(^|0)1(?!1)'|wc -l
  • -P (non-standard) - use PCRE-compatible regex syntax
  • -o (non-standard) - output each matching substring as separate line
  • wc -l - count lines
History

0 comment threads

+1
−0

C (gcc), 44 bytes

Borrowing/modifying test harness from @Lundin:

#define f(n)__builtin_popcount(n&~(n*2|n/2))

Try it online!

n & ~(n<<1) & ~(n>>1) is non-zero for any bit precisely when the original bit of n is non-zero and the preceding and following bits aren't.

History

1 comment thread

Relevant discussion about using a global instead of `return` (3 comments)

Sign up to answer this question »