sub bin2gray
{
    return $_[0] ^ ($_[0] >> 1);
}

sub gray2bin
{
    my ($num)= @_;
    my $bin= $num;
    while( $num >>= 1 ) {
        
        $bin ^= $num;   
    }                   
    return $bin;
}

for (0..31) {
    my $gr= bin2gray($_);
    printf "%d\t%b\t%b\t%b\n", $_, $_, $gr, gray2bin($gr);
}