function    gish = grayish_code(i, pi_B, NB)
%Called by approx_phi.m
%i= integer in 0:(2^NB-1)
%pi_B= permutation of row vector (1:NB)
%NB= integer, number of bits
%gish= integer, decimal representation of grayish code for i
if (NB<1)
	error("NB is less than 1");
end
if (i>=2^NB|  i<0) 
	error("i is out of range");
end
if (length(pi_B)!=NB)
	error("pi_B has wrong length");
end
x=dec_to_bin(i, NB);
y=zeros(1, NB);
for alp=0:(NB-2)
	index = NB - alp;
	if (x(index-1)==1)
		y(index)=1-x(index);
	else
		y(index)=x(index);
	end
end
y(1)=x(1);
z=zeros(1, NB);
for index=1:NB
	z(index) = y(pi_B(index));
end
gish   =  bin_to_dec(z); 