Skip to content

Instantly share code, notes, and snippets.

@benjholla
Last active January 27, 2016 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjholla/e6f63887569984e92863 to your computer and use it in GitHub Desktop.
Save benjholla/e6f63887569984e92863 to your computer and use it in GitHub Desktop.
A quine-relay with the state of a Linear Feedback Shift Register embedded in and updated by one shift operation for each successive output quine.
/**
* A quine-relay with the state of a Linear Feedback Shift Register embedded
* in and updated by one shift operation for each successive output quine.
*
* @author Ben Holland
*/
public class LFSRQuineRelay {
public static void main(String[] args) {
// initialize the register, any non-zero start state is valid
boolean[] register = {true, false, false, true, false, false, true, false, true, true, true};
boolean[] registerCopy = java.util.Arrays.copyOf(register, register.length);
// location of the tap
final int TAP_LOCATION = 8;
// compute the next bit
boolean next = (register[register.length - 1] ^ register[TAP_LOCATION]);
// shift everything left by one position
for (int i = register.length - 1; i > 0; i--){
register[i] = register[i - 1];
}
// add the new bit to the right
register[0] = next;
char quote = 34;
String[] code = {
"/**",
" * A quine-relay with the state of a Linear Feedback Shift Register embedded",
" * in and updated by one shift operation for each successive output quine.",
" * ",
" * @author Ben Holland",
" */",
"public class LFSRQuineRelay {",
" public static void main(String[] args) {",
" // initialize the register, any non-zero start state is valid",
" boolean[] register = {true, false, false, true, false, false, true, false, true, true, true};",
" boolean[] registerCopy = java.util.Arrays.copyOf(register, register.length);",
" // location of the tap",
" final int TAP_LOCATION = 8; ",
" // compute the next bit",
" boolean next = (register[register.length - 1] ^ register[TAP_LOCATION]);",
" // shift everything left by one position",
" for (int i = register.length - 1; i > 0; i--){",
" register[i] = register[i - 1];",
" }",
" // add the new bit to the right",
" register[0] = next;",
" char quote = 34;",
" String[] code = {",
" };",
" // update the register state",
" String s1 = java.util.Arrays.toString(registerCopy).replace('[', '{').replace(']', '}');",
" String s2 = java.util.Arrays.toString(register).replace('[', '{').replace(']', '}');",
" code[9] = code[9].replace(s1,s2);",
" // print the header code",
" for(int i=0; i<23; i++){",
" System.out.println(code[i]);",
" }",
" // print the code array",
" for(int i=0; i<code.length; i++){",
" System.out.println(quote + code[i] + quote + ',');",
" }",
" // print this code",
" for(int i=23; i<code.length; i++){",
" System.out.println(code[i]);",
" }",
" }",
"}",
};
// update the register state
String s1 = java.util.Arrays.toString(registerCopy).replace('[', '{').replace(']', '}');
String s2 = java.util.Arrays.toString(register).replace('[', '{').replace(']', '}');
code[9] = code[9].replace(s1,s2);
// print the header code
for(int i=0; i<23; i++){
System.out.println(code[i]);
}
// print the code array
for(int i=0; i<code.length; i++){
System.out.println(quote + code[i] + quote + ',');
}
// print this code
for(int i=23; i<code.length; i++){
System.out.println(code[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment