// Arup Guha
// 10/4/2019
// Solution to CIS 3362 Homework #4 Question #10

import java.util.*;

public class multbytes {
	public static void main(String[] args) {
	
		// Set byte and loop 256 times for each row.
		byte a = -128;
		for (int i=0; i<256; i++) {
			
			// Do same for b and columns.
			byte b = -128;
			for (int j=0; j<256; j++) {
				System.out.printf("%02x ",mult(a,b));
				b++;
			}
			
			// Go to the next row.
			System.out.println();
			a++;
		}
	}
	
	// Pretty self-explanatory.
	public static byte add(byte a, byte b) {
		return (byte)(a ^ b);
	}


	// Returns b * 2, in the field.
	public static byte mult2(byte b) {
		if (b < 0)
			return (byte)( ((0x7f & b) << 1) ^ 0x1b );
		return (byte)(b << 1);
	}	
	
	public static byte mult(byte a, byte b) {

		byte pow2 = b;
		byte ans = 0;

		// Loop until we've shifted all bits of a over.
		while (a != 0) {

			// Add in the contribution for this bit of a, if necessary.
			if (a%2 != 0)
		        ans = add(ans, pow2);


			a = (byte)((a >> 1) & 127);

			
		    // Multiply pow2 by 2, in the field, accordingly.
		    pow2 = mult2(pow2);
			

		}

		return ans;
	}	
}