Post History
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...
#9: Post edited
- # [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
# [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
- # [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
- # [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
- # [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
# [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
- # [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
- # [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
# [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 :)
