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

60%
+1 −0
Challenges Reverse the bits in a Byte

C (gcc), 47 bytes r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;} Try it online! I didn't manage to come up with anything better than the most obvious recursive im...

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

Answer
#3: Post edited by user avatar Lundin‭ · 2023-08-23T10:59:44Z (9 months ago)
  • # [C (gcc)], 47 bytes
  • <!-- language-all: lang-c -->
  • r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;}
  • [Try it online!][TIO-llnk7ocv]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-llnk7ocv]: https://tio.run/##JY5BCsIwEEX3OcWnYElIKm1BEZL2HC7cSDAyC1MZ6qrm7DGpqz8z7w183z29z5mNt0GS2vixfjjCa@0ubSt5YueGL7WDKXieB6UM25QprnjdKUolNgEgLCzrjabegtx4OpfQWgGV7grAU298Ef7bm8tDkM3hOF7RYUYdbrExIIPaRe1iEin/AA "C (gcc) – Try It Online"
  • I didn't manage to come up with anything better than the most obvious recursive implementation posted above. I tried to use a different algorithm based on iterating two counters instead, but it got quite a bit longer...
  • Or in case binary output is acceptable:
  • # [C (gcc)], 42 bytes
  • <!-- language-all: lang-c -->
  • c;f(i){c++<8&&(printf("%d",i&1),f(i>>1));}
  • [Try it online!][TIO-llnmdl5i]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-llnmdl5i]: https://tio.run/##LY3NCsIwEITveYohYNglFaygCKl9F1mJ7MFaansqefY0sZ7m5xsYOb5EcpYQSXkV77ubczROOsyR7OFpG3UtN4X2fcscUi4E74cOxGY1AOJnotrp/RSg3flyLeI9A5X@JoAUuLv687fjMn/J2j0mk/IG "C (gcc) – Try It Online"
  • # [C (gcc)], 47 bytes
  • <!-- language-all: lang-c -->
  • r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;}
  • [Try it online!][TIO-llnk7ocv]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-llnk7ocv]: https://tio.run/##JY5BCsIwEEX3OcWnYElIKm1BEZL2HC7cSDAyC1MZ6qrm7DGpqz8z7w183z29z5mNt0GS2vixfjjCa@0ubSt5YueGL7WDKXieB6UM25QprnjdKUolNgEgLCzrjabegtx4OpfQWgGV7grAU298Ef7bm8tDkM3hOF7RYUYdbrExIIPaRe1iEin/AA "C (gcc) – Try It Online"
  • I didn't manage to come up with anything better than the most obvious recursive implementation posted above. I tried to use a different algorithm based on iterating two counters instead, but it got quite a bit longer...
#2: Post edited by user avatar Lundin‭ · 2023-08-23T10:56:52Z (9 months ago)
  • # [C (gcc)], 47 bytes
  • <!-- language-all: lang-c -->
  • r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;}
  • [Try it online!][TIO-llnk7ocv]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-llnk7ocv]: https://tio.run/##JY5BCsIwEEX3OcWnYElIKm1BEZL2HC7cSDAyC1MZ6qrm7DGpqz8z7w183z29z5mNt0GS2vixfjjCa@0ubSt5YueGL7WDKXieB6UM25QprnjdKUolNgEgLCzrjabegtx4OpfQWgGV7grAU298Ef7bm8tDkM3hOF7RYUYdbrExIIPaRe1iEin/AA "C (gcc) – Try It Online"
  • I didn't manage to come up with anything better than the most obvious recursive implementation posted above. I tried to use a different algorithm based on iterating two counters instead, but it got quite a bit longer...
  • # [C (gcc)], 47 bytes
  • <!-- language-all: lang-c -->
  • r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;}
  • [Try it online!][TIO-llnk7ocv]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-llnk7ocv]: https://tio.run/##JY5BCsIwEEX3OcWnYElIKm1BEZL2HC7cSDAyC1MZ6qrm7DGpqz8z7w183z29z5mNt0GS2vixfjjCa@0ubSt5YueGL7WDKXieB6UM25QprnjdKUolNgEgLCzrjabegtx4OpfQWgGV7grAU298Ef7bm8tDkM3hOF7RYUYdbrExIIPaRe1iEin/AA "C (gcc) – Try It Online"
  • I didn't manage to come up with anything better than the most obvious recursive implementation posted above. I tried to use a different algorithm based on iterating two counters instead, but it got quite a bit longer...
  • Or in case binary output is acceptable:
  • # [C (gcc)], 42 bytes
  • <!-- language-all: lang-c -->
  • c;f(i){c++<8&&(printf("%d",i&1),f(i>>1));}
  • [Try it online!][TIO-llnmdl5i]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-llnmdl5i]: https://tio.run/##LY3NCsIwEITveYohYNglFaygCKl9F1mJ7MFaansqefY0sZ7m5xsYOb5EcpYQSXkV77ubczROOsyR7OFpG3UtN4X2fcscUi4E74cOxGY1AOJnotrp/RSg3flyLeI9A5X@JoAUuLv687fjMn/J2j0mk/IG "C (gcc) – Try It Online"
#1: Initial revision by user avatar Lundin‭ · 2023-08-23T10:00:59Z (9 months ago)
# [C (gcc)], 47 bytes

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

    r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;}

[Try it online!][TIO-llnk7ocv]

[C (gcc)]: https://gcc.gnu.org/
[TIO-llnk7ocv]: https://tio.run/##JY5BCsIwEEX3OcWnYElIKm1BEZL2HC7cSDAyC1MZ6qrm7DGpqz8z7w183z29z5mNt0GS2vixfjjCa@0ubSt5YueGL7WDKXieB6UM25QprnjdKUolNgEgLCzrjabegtx4OpfQWgGV7grAU298Ef7bm8tDkM3hOF7RYUYdbrExIIPaRe1iEin/AA "C (gcc) – Try It Online"

I didn't manage to come up with anything better than the most obvious recursive implementation posted above. I tried to use a different algorithm based on iterating two counters instead, but it got quite a bit longer...