/** * Converter illustrates a sample usage of the << and >> operators. They * are used to convert between binary and decimal. * * @author Mark Roth */ public class Converter { /** * Execution starts here */ public static void main( String[] args ) { System.out.println( "66 in binary is: " + decToBinary( 66 ) ); System.out.println( "10110 in decimal is: " + binaryToDec( "10110" ) ); } /** * Converts decimal to binary * * @param dec The decimal number to convert * @return A string containing the binary number. */ public static String decToBinary( int dec ) { String result = ""; while( dec > 0 ) { result = (dec & 1) + result; dec >>= 1; } return result; } /** * Converts binary to decimal * * @param bin The binary number to convert * @return An integer containing the decimal number. */ public static int binaryToDec( String bin ) { int i, result = 0; for( i = 0; i < bin.length(); i++ ) { result <<= 1; if( bin.charAt( i ) == '1' ) result++; } return result; } }