vendredi 11 septembre 2015

Decimal to Binary Java

I want convert just 256 numbers to binary without using if, while, etc., just by using the ? operator and four binary operators.

My programs works well for numbers 1 to 64, but after 64 it does not work! How can I do this? I must store all results in the variable b.

public class NaarBinair {
    public static int g=1,b=0;

    public static void main(String[] args){
        int r1 = g % 2 ;        
        int q1 = g / 2 ;
        int r2 = q1 % 2 ;
        int q2 = q1 / 2 ;
        int r3 = q2 % 2 ;       
        int q3 = q2 / 2 ;
        int r4 = q3 % 2 ;       
        int q4 = q3 / 2 ;
        int r5 = q4 % 2 ;       
        int q5 = q4 / 2 ;
        int r6 = q5 % 2 ;       
        int q6 = r5 / 2 ;
        int r7 = q6 % 2 ;       
        int q7 = r6 / 2 ;

        String s1 =  String.valueOf(r1) ;           
        String s2 =  String.valueOf(r2) ;
        String s3 =  String.valueOf(r3) ;
        String s4 =  String.valueOf(r4) ;       
        String s5 =  String.valueOf(r5) ;
        String s6 =  String.valueOf(r6) ;
        String s7 =  String.valueOf(r7) ;

        b = Integer.parseInt( s7 + s6 + s5 + s4 + s3 + s2 + s1 ) ;
        System.out.println(b);
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire