12
\$\begingroup\$

Bitshift Variations in C Minor by Robert Miles is a Code Golf music piece written in C (with some additional bash commands). It was originally presented in a 2016 Computerphile video Code Golf & the Bitshift Variations. You can listen to clean renderings of it here (by Lucia Ceionia).

The original 2016 source code presented in the description under the YouTube video was 245 characters long:

echo "g(i,x,t,o){return((3&x&(i*((3&i>>16?\"BY}6YB6%\":\"Qj}6jQ6%\")[t%8]+51)>>o))<<4);};main(i,n,s){for(i=0;;i++)putchar(g(i,1,n=i>>14,12)+g(i,s=i>>17,n^i>>13,10)+g(i,s/3,n+((i>>11)%3),10)+g(i,s/5,8+n-((i>>10)%3),9));}"|gcc -xc -&&./a.out|aplay

A tweet from 2017 (or some other Rob Miles tweet, not sure) stands 216 characters:

gcc -xc -oa -<<<'i;n;g(x,t,o){return(3&x&(i*((3&i>>16?"BY}6YB6%":"Qj}6jQ6%")[t%8]+51)>>o))<<4;}main(s){for(;;)putchar(g(1,n=++i>>14,12)+g(s=i>>17,n^i>>13,10)+g(s/3,n+(i>>11)%3,10)+g(s/5,8+n-(i>>10)%3,9));}';./a|aplay

Another version using only 185 characters (written by MayMeta from Manifold Markets):

gcc -xc -oa -<<<'i;n;g(m,t,o){return("GTj?TG?5"[7&t]+!(n&12|t&2)*9)*i>>o&m&24;}main(s){for(;;)putchar(g(8,n=++i>>14,8)+g(n,n^(s=i>>10)/8,6)+g(n/3,n+s/2%3,6)+g(n/5,n-s%3,5));}';./a|aplay

Will someone be able to shrink it down below 185 characters, or the limit has been reached?

Extra Credit (Resolution criteria for a related betting market):

  • The bash command compiles the C source code and runs + plays it using aplay (or similar)
  • You ARE allowed to slightly change the played notes, but their relative frequencies should not differ more than 1.4% from the original, and the entire piece should not be shifted more than one semitone (6%) from the original.
  • Compiles and works as intended on an average linux machine.
  • Does not use the internet

A winning entry will be the shortest entry that is less than 185 characters, and follows the Extra Credit guidelines. Entries that do not follow the extra credit are appreciated, but will not be selected as the winner. Solutions that could be argued to be following the spirit extra credit guidelines in their attempt to get under 185 will be considered to be winners as well, as it's a bit subjective.

Related Links:

Deep analysis of Bitshift Variations in C Minor code

BitShift-Variations-unrolled

Bitshift Variations Humanized + Rust version

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Note that the MayMeta version outputs a different song than the original (it's shifted in pitch significantly). Do you care which version is golfed? \$\endgroup\$
    – nneonneo
    Jun 5 at 11:54
  • \$\begingroup\$ I think if the extra credit pitch criteria is maintained relative to either version, I would consider the solution acceptable. \$\endgroup\$
    – brubsby
    Jun 6 at 5:25

2 Answers 2

10
\$\begingroup\$

183 bytes

This isn't a big improvement or super impressive or anything, but saves 2 bytes in the main loop with some rearranging.

gcc -xc -oa -<<<'i;n;g(m,t,o){return("GTj?TG?5"[7&t]+!(n&12|t&2)*9)*i>>o&m&24;}main(s){for(;;putchar(g(8,n=s>>4,8)+g(n,n^s/8,6)+g(n/3,n+s/2%3,6)+g(n/5,n-s%3,5)))s=++i>>10;}';./a|aplay

The changes are more visible when they are put side by side:

for(;;)putchar(g(8,n=++i>>14,8)+g(n,n^(s=i>>10)/8,6)+g(n/3,n+s/2%3,6)+g(n/5,n-s%3,5));
for(;;putchar(g(8,n=s>>4,8)+g(n,n^s/8,6)+g(n/3,n+s/2%3,6)+g(n/5,n-s%3,5)))s=++i>>10;
\$\endgroup\$
4
\$\begingroup\$

182

"Compiles and works as intended on an average linux machine."...
In common distros, cc is symlinked to gcc. Thus the first byte can be shaved off.

cc -xc -oa -<<<'i;n;g(m,t,o){return("GTj?TG?5"[7&t]+!(n&12|t&2)*9)*i>>o&m&24;}main(s){for(;;putchar(g(8,n=s>>4,8)+g(n,n^s/8,6)+g(n/3,n+s/2%3,6)+g(n/5,n-s%3,5)))s=++i>>10;}';./a|aplay
\$\endgroup\$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.