how to get binary value in java

Solutions on MaxInterview for how to get binary value in java by the best coders in the world

showing results for - "how to get binary value in java"
Elijah
20 May 2019
1public static String makeBinaryString(int n) {
2	StringBuilder sb = new StringBuilder();
3	while (n > 0) {
4    sb.append(n % 2);
5		n /= 2;
6	}
7  	sb.reverse();  	
8  	return sb.toString();
9}
Bautista
03 Feb 2019
1import java.util.*;
2
3class Bin {
4
5    //fields
6    private Double number;
7
8    //constructors
9    public Bin(){
10        number = 0.0;
11    }
12
13    public Bin(Double num){
14        number = num;
15    }
16
17    //methods
18    public ArrayList<String> reverseArrayList(ArrayList<String> alist) {
19        
20        ArrayList<String> array = new ArrayList<String>();
21
22        int x = 0;
23        int i = alist.size() - 1;
24
25        while(x < alist.size()){
26            array.add(alist.get(i));
27            i -= 1;
28            x += 1;
29        }
30
31        return array;
32    }
33
34	//getter methods
35    public ArrayList<String> getBinVal(){
36        ArrayList<String> binaryVal = new ArrayList<String>();
37        ArrayList<String> array = new ArrayList<String>();
38        Double num = number;
39        Double sol1, check; 
40
41        while( num > 0 ) {
42
43            sol1 = num/2;
44            check = sol1 - Math.floor(sol1);
45
46            if(check > 0) {
47                binaryVal.add("1");
48                num = sol1 - 0.5;
49            }
50
51            else {
52                binaryVal.add("0");
53                num = sol1;
54            }
55        }
56
57        while(binaryVal.size() < 4) {
58            binaryVal.add("0");
59        }
60
61        array = reverseArrayList(binaryVal);
62        return array;
63    }
64
65}
66
67class MainClass {
68    
69    public static void main(String[] args) {
70		
71      	//add the number in as a command line argument
72        Double num = Double.valueOf(args[0]);
73		
74      	//create an instance of Bin
75        Bin value = new Bin(num);
76		
77      	//print the binary value
78        System.out.println(value.getBinVal());
79    }
80}
queries leading to this page
decimal to binary java codehow to convert decimal to binary javajava convert integer to binary stringhow to print out a binary number in javaconverting int to binaryjava program to convert integer to binaryto binary in javaprint int in binary javahow to convert a number to binary in javaconvert number to binary in javajava show binary valuejava print binarybinarie output in javabinary data in javajava how to represent something in binaryjava convert int to binaryconvert integer to binary in javajava compute binary 28 29integer to binary stringint to binary java formatjava get binary stringhow to get java se binaryprint number in binary in javajava int to binary arrayjava read int as binaryshow binary string of javadecimal to binary conversion java programinteger to binary conversion javajava convert number from decimal to binaryconvert to binary javahow to convert to binary in javashow binary numbers java8how to find binary number in javaconvert int to binary javahow to convert integer to binary string in javprint binary string for int javaconvert number into its binary equivalent in javahow to convert integer to binary string in javaget binary value in javaintger to binary stringbinary to javajava int to binaryjava integer to binaryarray to binary number javajava code to convert int to binaryjava number to binaryjava integer to binary stringjava get binary value of inthow to convert int to binary in javahow to convert the integer number into binary representation in javahow to print binary value in javahow to get binary representation of a number in javaget binary number in javajava program to convert integer value into binaryconverting int to binary in javahow to get binary value of a string in java with a clasprint as binary javahow to get binary value of a number in javabest way to find binary value of a number code in javahow to get binary value of an integer in javaint to binary string javahow to print binary of a number in javause binary in javasimple number to binary in javabinary in javabinary representation of a number in javanumber to binary javaconvert number to binary javahow to convert decimal to binary in javaconvert decimal to binary in javainteger binary representation javajava convert decimal to binaryhow to convert an integer object to binary string in javaconvert an integer to binary javahow to get the binary value of a number in javato find binary numbers javaint to binary javahow to get the get the binary value of an integer in javaprint binary number in javahow to get ascii value in pythonbinarystring to number java inbuilt functionconverting int to binary java 2f part 28a 29 2c use size 2f 2f hint 29 use integer tobinarystring 28 29 methoddecimal to binary in javajava get bit representation of intstore binary value javaint to bits javaconvert int to binary int in javajava int to binary digitshow to convert number to binary string javajava library to convert integer to binarybinary value in javaint to binary string in javastring integer to binary number javahow to convert decimal into binary in javahow to store binary value in javainteger to binary string in javajava int get binaryhow to use binary in javaget binary of a number javajava int to binary stringhow to print a binary in javaconvert decimal to binary javaconvert int to binary stringgetting int binary value in javaintegere to binary in javaconvert integer into binary javaconverting integer to binary in javahow to store int as binary in javato binary string javajava decimal to binary conversioninteger to binary javajava find a binary number of a intjava print integer as bitsdecimal to binary converter in javabinary in four bits javagetting a binary number in javahow to get the binary vale of a number in javafrom int to binaryhow to get binary number in javaint to binaray in javaint to binary stringinteger to binary string javajava recursively turn int to binary string methodconvert int to binary in javahow to store binary number in javahow to get binary value in javajava convert number to binarybinary javainteger to binary stringjava binary valuebianry string in javahow to print 4 binary value in pythonhow to do binary in javafrom integer to binarycode to convert decimal to binary in javamake binary tree from number arrayget binary representation javaconvert number into binary 2b javadecimal to binary javaconvert int to binray injavaconvert to binary integer javahow to convert integer into binary string format javahow to get 4 digit binary number in javahow to print binary number in javainteger to binary in javajava print number in binaryjava print int as binarydecimal number to binary number in javaprint binary value in javajava integer to binary string docnumber to binary converter javato binary javaint to binary in javahow to convert a number into binary in javahow to get binary value in java