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

Post History

66%
+2 −0
Challenges Lone ​​​​​​ones

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 varia...

posted 7mo ago by Lundin‭  ·  edited 7mo ago by Lundin‭

Answer
#9: Post edited by user avatar Lundin‭ · 2026-03-05T07:42:37Z (7 months ago)
  • # [C (gcc)], 67 bytes
  • <!-- language-all: lang-c -->
  • 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!][TIO-mmd5q2lw]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mmd5q2lw]: https://tio.run/##dZFda4MwGIXv/RXBYlG0xTeJ8SNa2cUGY4NdrBe72I34tYCLxVqwlP71uYyVtW5rwgsvDyfnHEi@qPN8HDPeccErs2lljaR1qNrO5CImhFtZIufQxLGw7TRLcQRRNoe0s23HjVzelf2ukyjjx3EmZN7sihLF274Q7fJtpWmzoqyELNH69nltDs7eQlmiHqkRalBlDhZHm07IvjJ1A9zlDi1WyFgGL6hu@8goUDlsyrwvi6/d2L5K3RnU7Zy90yXJPtWfHvRIv7u5f9QtrmnKCL1nQpqWdtDQd6zroPNxlerEKQ5pyHwces6EY6A@DQijgeJw5nDpA//oQyXAZx4GxKXO31wg4Pr41ApP@zASMvCmuT896ZWe/pQTjAFjj/pwxYdMOQ4YIQAeUZWAXfSkBJjn@cz7xX3ww4CFEJxyj@NHXjVZvR0X6ueTHOAT "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Code Golf rule that a global may hold the result to save a `return`.
  • - 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 :)
  • # [C (gcc)], 67 bytes
  • <!-- language-all: lang-c -->
  • 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!][TIO-mmd5q2lw]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mmd5q2lw]: https://tio.run/##dZFda4MwGIXv/RXBYlG0xTeJ8SNa2cUGY4NdrBe72I34tYCLxVqwlP71uYyVtW5rwgsvDyfnHEi@qPN8HDPeccErs2lljaR1qNrO5CImhFtZIufQxLGw7TRLcQRRNoe0s23HjVzelf2ukyjjx3EmZN7sihLF274Q7fJtpWmzoqyELNH69nltDs7eQlmiHqkRalBlDhZHm07IvjJ1A9zlDi1WyFgGL6hu@8goUDlsyrwvi6/d2L5K3RnU7Zy90yXJPtWfHvRIv7u5f9QtrmnKCL1nQpqWdtDQd6zroPNxlerEKQ5pyHwces6EY6A@DQijgeJw5nDpA//oQyXAZx4GxKXO31wg4Pr41ApP@zASMvCmuT896ZWe/pQTjAFjj/pwxYdMOQ4YIQAeUZWAXfSkBJjn@cz7xX3ww4CFEJxyj@NHXjVZvR0X6ueTHOAT "C (gcc) – 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
  • ```c
  • 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 :)
#8: Post edited by user avatar Lundin‭ · 2026-03-05T07:42:01Z (7 months ago)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Code Golf rule that a global may hold the result to save a `return`.
  • - 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 :)
  • # [C (gcc)], 67 bytes
  • <!-- language-all: lang-c -->
  • 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!][TIO-mmd5q2lw]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mmd5q2lw]: https://tio.run/##dZFda4MwGIXv/RXBYlG0xTeJ8SNa2cUGY4NdrBe72I34tYCLxVqwlP71uYyVtW5rwgsvDyfnHEi@qPN8HDPeccErs2lljaR1qNrO5CImhFtZIufQxLGw7TRLcQRRNoe0s23HjVzelf2ukyjjx3EmZN7sihLF274Q7fJtpWmzoqyELNH69nltDs7eQlmiHqkRalBlDhZHm07IvjJ1A9zlDi1WyFgGL6hu@8goUDlsyrwvi6/d2L5K3RnU7Zy90yXJPtWfHvRIv7u5f9QtrmnKCL1nQpqWdtDQd6zroPNxlerEKQ5pyHwces6EY6A@DQijgeJw5nDpA//oQyXAZx4GxKXO31wg4Pr41ApP@zASMvCmuT896ZWe/pQTjAFjj/pwxYdMOQ4YIQAeUZWAXfSkBJjn@cz7xX3ww4CFEJxyj@NHXjVZvR0X6ueTHOAT "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Code Golf rule that a global may hold the result to save a `return`.
  • - 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 :)
#7: Post edited by user avatar trichoplax‭ · 2026-03-04T16:27:07Z (7 months ago)
Typo
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 :)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Code Golf rule that a global may hold the result to save a `return`.
  • - 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 :)
#6: Post edited by user avatar Lundin‭ · 2026-03-03T08:57:22Z (7 months ago)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 :)
#5: Post edited by user avatar Lundin‭ · 2026-03-03T08:56:05Z (7 months ago)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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 `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
#4: Post edited by user avatar Lundin‭ · 2026-03-02T15:57:30Z (7 months ago)
Actually we get away with no paranthesis around comma in this specific case
  • # [C (gcc)], 60 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?(r++,0):0;}
  • [Try it online!][TIO-mm9crkab]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9crkab]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziExO9t2XCt0xXGcySard3mBom2fy3b5ttK0WV6UsinQ@vZ5bQ7O3kJp7IpOjVSDSnOwBNp0sulLUzfAXe7QYoWMpf@CqrYPjRwVw6bI@iL/ehvb10Z3BnU7Z@90cbxP9KcHPdTvbu4fdUtomlqE3lPZmJZ20NB3reug83FV6sQpDmjAOA48Z8IxUE59wqivOJw5XO6Bf/KBCuAzD3ziUudvLxBwOT5Z4akPIwEDb9r740mvePIpJxgDxh7lcGUPmXLsM0IAPKKUgF14UgLM8zjzfnEOPPBZAP6p9zh@ZGWdVttxoX4@zgA@AQ "C (gcc) – 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
  • # [C (gcc)], 58 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?r++,0:0;}
  • [Try it online!][TIO-mm9d33no]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9d33no]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziHpbNtxQ1ccx5lssnqXFyja9rlsl28rTZvlRSmbAq1vn9fm4OwtlMau6NRINag0B0ugTSebvjR1A9zlDi1WyFj6L6hq@9DIUTFsiqwv8q@3sX1tdGdQt3P2ThfH@0R/etBD/e7m/lG3hKapReg9lY1paQcNfde6DjofV6VOnOKABozjwHMmHAPl1CeM@orDmcPlHvgnH6gAPvPAJy51/vYCAZfjkxWe@jASMPCmvT@e9Ionn3KCMWDsUQ5X9pApxz4jBMAjSgnYhSclwDyPM@8X58ADnwXgn3qP40dW1mm1HRfq5@MM4BM "C (gcc) – 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
#3: Post edited by user avatar Lundin‭ · 2026-03-02T15:55:33Z (7 months ago)
  • # [C (gcc)], 60 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?(r++,0):0;}
  • [Try it online!][TIO-mm9crkab]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9crkab]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziExO9t2XCt0xXGcySard3mBom2fy3b5ttK0WV6UsinQ@vZ5bQ7O3kJp7IpOjVSDSnOwBNp0sulLUzfAXe7QYoWMpf@CqrYPjRwVw6bI@iL/ehvb10Z3BnU7Z@90cbxP9KcHPdTvbu4fdUtomlqE3lPZmJZ20NB3reug83FV6sQpDmjAOA48Z8IxUE59wqivOJw5XO6Bf/KBCuAzD3ziUudvLxBwOT5Z4akPIwEDb9r740mvePIpJxgDxh7lcGUPmXLsM0IAPKKUgF14UgLM8zjzfnEOPPBZAP6p9zh@ZGWdVttxoX4@zgA@AQ "C (gcc) – 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
  • # [C (gcc)], 60 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?(r++,0):0;}
  • [Try it online!][TIO-mm9crkab]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9crkab]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziExO9t2XCt0xXGcySard3mBom2fy3b5ttK0WV6UsinQ@vZ5bQ7O3kJp7IpOjVSDSnOwBNp0sulLUzfAXe7QYoWMpf@CqrYPjRwVw6bI@iL/ehvb10Z3BnU7Z@90cbxP9KcHPdTvbu4fdUtomlqE3lPZmJZ20NB3reug83FV6sQpDmjAOA48Z8IxUE59wqivOJw5XO6Bf/KBCuAzD3ziUudvLxBwOT5Z4akPIwEDb9r740mvePIpJxgDxh7lcGUPmXLsM0IAPKKUgF14UgLM8zjzfnEOPPBZAP6p9zh@ZGWdVttxoX4@zgA@AQ "C (gcc) – 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
#2: Post edited by user avatar Lundin‭ · 2026-03-02T15:55:01Z (7 months ago)
  • # [C (gcc)], 60 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?(r++,0):0;}
  • [Try it online!][TIO-mm9crkab]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9crkab]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziExO9t2XCt0xXGcySard3mBom2fy3b5ttK0WV6UsinQ@vZ5bQ7O3kJp7IpOjVSDSnOwBNp0sulLUzfAXe7QYoWMpf@CqrYPjRwVw6bI@iL/ehvb10Z3BnU7Z@90cbxP9KcHPdTvbu4fdUtomlqE3lPZmJZ20NB3reug83FV6sQpDmjAOA48Z8IxUE59wqivOJw5XO6Bf/KBCuAzD3ziUudvLxBwOT5Z4akPIwEDb9r740mvePIpJxgDxh7lcGUPmXLsM0IAPKKUgF14UgLM8zjzfnEOPPBZAP6p9zh@ZGWdVttxoX4@zgA@AQ "C (gcc) – 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 without need for a dedicated variable.
  • 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
  • # [C (gcc)], 60 bytes
  • <!-- language-all: lang-c -->
  • a;r;i;f(long n){for(;i<33;)a=n&1l<<i++?a?2:1:a&1?(r++,0):0;}
  • [Try it online!][TIO-mm9crkab]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-mm9crkab]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziExO9t2XCt0xXGcySard3mBom2fy3b5ttK0WV6UsinQ@vZ5bQ7O3kJp7IpOjVSDSnOwBNp0sulLUzfAXe7QYoWMpf@CqrYPjRwVw6bI@iL/ehvb10Z3BnU7Z@90cbxP9KcHPdTvbu4fdUtomlqE3lPZmJZ20NB3reug83FV6sQpDmjAOA48Z8IxUE59wqivOJw5XO6Bf/KBCuAzD3ziUudvLxBwOT5Z4akPIwEDb9r740mvePIpJxgDxh7lcGUPmXLsM0IAPKKUgF14UgLM8zjzfnEOPPBZAP6p9zh@ZGWdVttxoX4@zgA@AQ "C (gcc) – 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
  • ```c
  • 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
  • - Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
  • - This uses the Gode Golf rule that a global may hold the result to save a `return`.
  • - 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 say me much :)
#1: Initial revision by user avatar Lundin‭ · 2026-03-02T15:53:58Z (7 months ago)
# [C (gcc)], 60 bytes

<!-- language-all: lang-c -->

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

[Try it online!][TIO-mm9crkab]

[C (gcc)]: https://gcc.gnu.org/
[TIO-mm9crkab]: https://tio.run/##dZFda4MwFIbv/RXBYlG0xZPERI1WdrHB2GAX68UudiN@LeC0WAuW0r8@l7Gy1m1NOBAeXs77QLJFlWXjmIpOSFGaddtUqLEOZduZQkaECCuNmznUUSRtO0kTHEKYziExO9t2XCt0xXGcySard3mBom2fy3b5ttK0WV6UsinQ@vZ5bQ7O3kJp7IpOjVSDSnOwBNp0sulLUzfAXe7QYoWMpf@CqrYPjRwVw6bI@iL/ehvb10Z3BnU7Z@90cbxP9KcHPdTvbu4fdUtomlqE3lPZmJZ20NB3reug83FV6sQpDmjAOA48Z8IxUE59wqivOJw5XO6Bf/KBCuAzD3ziUudvLxBwOT5Z4akPIwEDb9r740mvePIpJxgDxh7lcGUPmXLsM0IAPKKUgF14UgLM8zjzfnEOPPBZAP6p9zh@ZGWdVttxoX4@zgA@AQ "C (gcc) – 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 without need for a dedicated variable.

  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 

   ```c
   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
- Operator precedence `++` over `<<` over `&` over `?:` over `=` over `,`.
- This uses the Gode Golf rule that a global may hold the result to save a `return`.
- 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 say me much :)