import java.util.*;

public class Command
{
    public static final String MASK = "mask";
    public static final String MEM = "mem";
    public static final String OPEN_BRACE = "[";
    public static final String CLOSE_BRACE = "]";
    public static final String EQUALS = "=";

    public static final char FLOATING_BIT = 'X';
    public static final char ONE_BIT = '1';
    public static final char ZERO_BIT = '0';

    public static final int INTEGER_SIZE = 36;

    public Command (String mask, Vector<String> cmds, boolean debug)
    {
        _mask = mask;
        _commands = cmds;
        _debug = debug;
    }

    public void execute (Memory mem)
    {
        String theMask = _mask.substring(_mask.indexOf(EQUALS) +1).trim();

        if (_debug)
            System.out.println("Bitmask is: "+theMask);

        for (int i = 0; i < _commands.size(); i++)
        {
            int startIndex = _commands.elementAt(i).indexOf(OPEN_BRACE);
            int endIndex = _commands.elementAt(i).indexOf(CLOSE_BRACE);
            String addrStr = _commands.elementAt(i).substring(startIndex +1, endIndex);
            Long addr = Long.parseLong(addrStr);

            if (_debug)
                System.out.println("Memory address: "+addr);

            startIndex = _commands.elementAt(i).indexOf(EQUALS);

            String valueStr = _commands.elementAt(i).substring(startIndex +1).trim();
            Long value = Long.parseLong(valueStr);

            if (_debug)
                System.out.println("Value: "+value);

            char[] maskedAddress = applyMask(addr, theMask);
            Vector<String> addresses = generateAllAddresses(maskedAddress);

            if (_debug)
            {
                System.out.println("\nGenerated addresses:\n");

                for (int j = 0; j < addresses.size(); j++)
                    System.out.println(addresses.elementAt(j));
            }
        }
    }

    @Override
    public String toString ()
    {
        String str = _mask;

        for (int i = 0; i < _commands.size(); i++)
        {
            str += "\n" + _commands.elementAt(i);
        }

        return str;
    }

    private char[] applyMask (long address, String theMask)
    {
        char[] addrAsChar = convertToBinary(address);

        for (int i = 0; i < theMask.length(); i++)
        {
            switch (theMask.charAt(i))
            {
                case ONE_BIT:
                case FLOATING_BIT:
                {
                    /*
                     * If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
                     * If the bitmask bit is X, the corresponding memory address bit is floating.
                     */

                     addrAsChar[i] = theMask.charAt(i);
                }
                break;
                case ZERO_BIT:
                default:
                {
                    // If the bitmask bit is 0, the corresponding memory address bit is unchanged.
                }
                break;
            }
        }

        if (_debug)
            System.out.println("Masked address: "+new String(addrAsChar)+"\n");

        return addrAsChar;
    }

    private char[] convertToBinary (long value)
    {
        char[] tempBinary = Long.toBinaryString(value).toCharArray();
        char[] binary;

        if (tempBinary.length < INTEGER_SIZE)
        {
            binary = new char[INTEGER_SIZE];

            for (int i = 0; i < (INTEGER_SIZE - tempBinary.length); i++)
            {
                binary[i] = ZERO_BIT;
            }

            for (int j = 0; j < tempBinary.length; j++)
            {
                binary[(INTEGER_SIZE - tempBinary.length) +j] = tempBinary[j];
            }
        }
        else
            binary = tempBinary;

        return binary;
    }

    // 000000000000000000000000000000X1001X

    private Vector<String> generateAllAddresses (char[] address)
    {
        Vector<String> addresses = new Vector<String>();
        boolean hasFloatingBits = (new String(address).indexOf(FLOATING_BIT) == -1) ? false : true;

        if (!hasFloatingBits)  // no floating bits!
            addresses.add(new String(address));
        else
        {
            for (int i = 0; i < address.length; i++)
            {
                if (address[i] == FLOATING_BIT)
                {
                    generateAddress(address, i, ONE_BIT, addresses);
                    generateAddress(address, i, ZERO_BIT, addresses);
                }
            }
        }

        return addresses;
	}

    // 000000000000000000000000000000X1001X

    private void generateAddress (char[] address, int index, char replace, Vector<String> addresses)
    {
        char[] modified = Arrays.copyOf(address, address.length);

        System.out.println("Changing "+index+" to "+replace);

        System.out.println("Before "+new String(modified));

        modified[index] = replace;

        System.out.println("After  "+new String(modified));

        for (int i = index +1; i < modified.length; i++)
        {
            if (modified[i] == FLOATING_BIT)
            {
                generateAddress(modified, i, ONE_BIT, addresses);
                generateAddress(modified, i, ZERO_BIT, addresses);
            }
        }

        System.out.println("Now got  "+new String(modified));

        if (new String(modified).indexOf(FLOATING_BIT) == -1)
        {
            System.out.println("Adding "+new String(modified));

            addresses.add(new String(modified));
        }
        else
            System.out.println("Ignoring");
    }

    private String _mask;
    private Vector<String> _commands;
    private boolean _debug;
}
@@ -1675,219 +1675,285 @@
     
-if (_debug)%0A            %7B%0A                System.out.println(%22%5CnGenerated addresses:%5Cn%22);%0A%0A                for (int j = 0; j %3C addresses.size(); j++)%0A                    System.out.println(addresses.elementAt(j)
+for (int j = 0; j %3C addresses.size(); j++)%0A            %7B%0A                String theAddr = addresses.elementAt(j);%0A%0A                if (_debug)%0A                    System.out.println(%22Generated address: %22+theAddr);%0A%0A                mem.setValue(Long.parseLong(theAddr, 2), value
 );%0A 
@@ -3572,38 +3572,24 @@
 ngth); i++)%0A
-            %7B%0A
             
@@ -3613,30 +3613,16 @@
 ERO_BIT;
-%0A            %7D
 %0A%0A      
@@ -3667,38 +3667,24 @@
 ength; j++)%0A
-            %7B%0A
             
@@ -3750,30 +3750,16 @@
 ary%5Bj%5D;%0A
-            %7D%0A
         
@@ -4810,218 +4810,33 @@
     
-System.out.println(%22Changing %22+index+%22 to %22+replace);%0A%0A        System.out.println(%22Before %22+new String(modified));%0A%0A        modified%5Bindex%5D = replace;%0A%0A        System.out.println(%22After  %22+new String(modified))
+modified%5Bindex%5D = replace
 ;%0A%0A 
@@ -5130,313 +5130,108 @@
     
-System.out.println(%22Now got  %22+new String(modified));%0A%0A        if (new String(modified).indexOf(FLOATING_BIT) == -1)%0A        %7B%0A            System.out.println(%22Adding %22+new String(modified));%0A%0A            addresses.add(new String(modified));%0A        %7D%0A        else%0A            System.out.println(%22Ignoring%22
+if (new String(modified).indexOf(FLOATING_BIT) == -1)%0A            addresses.add(new String(modified)
 );%0A 

Update Command.java