LFSR.java


This is the syntax highlighted version of LFSR.java from 2.5 Arrays of
Introduction to Computer Science by Robert Sedgewick and Kevin Wayne.

/*************************************************************************
 *  Compilation:  javac LFSR.java
 *  Execution:    java LFSR N
 *  
 *  Simulate a LFSR for N steps and print results.
 *
 *  % java LFSR 40
 *  0100110010000001100010001011101010111100
 *
 *************************************************************************/


public class LFSR {

    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]);
        boolean[] b = { false, true, false, false, false, false, true, false, true, true, false };

        for (int i = 0; i < N; i++) {
            boolean bit = b[3] ^ b[10];
            for (int j = b.length - 1; j >= 1; j--)
                b[j] = b[j-1];
            b[0] = bit;
            if (bit) System.out.print(1);
            else     System.out.print(0);
        }
        System.out.println();
    }
}


Last updated: Sat Jun 26 14:12:37 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.