public class printBits2{
public static void main( String args[] ){
printBits2 pb = new printBits2( args[ 0 ] );
}
public printBits2( String message ){
//System.out.print( message + "\n\n" );
char chA[] = new char[ message.length() ];
chA = message.toCharArray();
System.out.print( chA[ 0 ] + " -> is your input char\n" );
printCharBits( chA[ 0 ] );
System.out.print( " -> is what computer sees\n\n" );
toHamming( chA[ 0 ] );
}
private void toHamming( char c ){
char displayMask = 1 << 0;
char n[]; n = new char[ 4 ]; n[ 0 ] = 91; n[ 1 ] = 109; n[ 2 ] = 7; n[ 3 ] = 7;
int j = 0;
boolean b = true;
char HC = 0 << 15;
for( int i = 16; i > 5; i-- ){
if( 16 % ( 17 - i ) == 0 ){
//insert the appropriate parity bits
n[ j ] = (char) ( c & n[ j ] );
for( int k = 0; k < 7; k++ ){
if( (int) ( n[ j ] & displayMask ) == 1 ){ b = !b; }
n[ j ] >>=1;
}
if( ! b ){
int helpMask = 1 << (int) ( Math.pow( 2, j ) - 1 );
HC = (char) ( helpMask | (int) HC );
}
j++;
b = true;
}
else{
if( ( c & displayMask ) == displayMask ){
int helpMask = 1 << ( 16 - i );
HC = (char) ( helpMask | (int) HC );
}
c >>=1;
}
}
printCharBits( HC ); System.out.print( " -> hamming code computer sees\n\n" );
}
private void printCharBits( char c ){
char displayMask = 1 << 15;
for ( int bit = 1; bit < 17; bit++ ){
if( ( c & displayMask ) == 0 ){
System.out.print( "0" );
if( bit % 8 == 0 ){ System.out.print( " " ); }
}
else{
System.out.print( "1" );
if( bit % 8 == 0 ){ System.out.print( " " ); }
}
c <<= 1;
}
}
}