how to get binary value in java
Java
public static String makeBinaryString(int n) {
StringBuilder sb = new StringBuilder();
while (n > 0) {
sb.append(n % 2);
n /= 2;
}
sb.reverse();
return sb.toString();
}import java.util.*;
class Bin {
//fields
private Double number;
//constructors
public Bin(){
number = 0.0;
}
public Bin(Double num){
number = num;
}
//methods
public ArrayList<String> reverseArrayList(ArrayList<String> alist) {
ArrayList<String> array = new ArrayList<String>();
int x = 0;
int i = alist.size() - 1;
while(x < alist.size()){
array.add(alist.get(i));
i -= 1;
x += 1;
}
return array;
}
//getter methods
public ArrayList<String> getBinVal(){
ArrayList<String> binaryVal = new ArrayList<String>();
ArrayList<String> array = new ArrayList<String>();
Double num = number;
Double sol1, check;
while( num > 0 ) {
sol1 = num/2;
check = sol1 - Math.floor(sol1);
if(check > 0) {
binaryVal.add("1");
num = sol1 - 0.5;
}
else {
binaryVal.add("0");
num = sol1;
}
}
while(binaryVal.size() < 4) {
binaryVal.add("0");
}
array = reverseArrayList(binaryVal);
return array;
}
}
class MainClass {
public static void main(String[] args) {
//add the number in as a command line argument
Double num = Double.valueOf(args[0]);
//create an instance of Bin
Bin value = new Bin(num);
//print the binary value
System.out.println(value.getBinVal());
}
}
Also in Java:
- Title
- java get current milliseconds
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- declare String in java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- how to get a user input in java
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- java try catch
- Category
- Java
- Title
- jbutton remove selection border
- Category
- Java
- Title
- kotlin add value to MutableLiveData
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- java taking console input
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- how to check if a string contains only alphabets and space in java
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- generate random number java
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- for loop in java stack overflow
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- how to test for legit email in java
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- java 8 function supplier consumer
- Category
- Java
- Title
- remove action bar android studio
- Category
- Java
- Title
- how to separate no and text in java
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- java merge sort
- Category
- Java
- Title
- check if list contains new line in java
- Category
- Java
- Title
- java read each lines in file
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- java 8 find min value in array
- Category
- Java
- Title
- arraylist in java
- Category
- Java
- Title
- java 8 iterating and manipulating list
- Category
- Java
- Title
- listview get selected java
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- java random seed
- Category
- Java
- Title
- expression régulière seulement un espace java
- Category
- Java
- Title
- detect tv remote keys andoid studio
- Category
- Java
- Title
- java stream filter
- Category
- Java
- Title
- How to make a class in Java?
- Category
- Java
- Title
- how to get witdth of window android
- Category
- Java
- Title
- creating array java
- Category
- Java
- Title
- sort a map based on keys and values using java 8
- Category
- Java
- Title
- how to initialize array in java
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- how to make an object move with arrow keys in java
- Category
- Java
- Title
- arraylist get last
- Category
- Java
- Title
- how to declare string array in java
- Category
- Java
- Title
- faire un timer en java
- Category
- Java
- Title
- java time code
- Category
- Java
- Title
- java int to hex fixed length
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- comparable on a generic class java
- Category
- Java
- Title
- filter and map multiple fields from java stream
- Category
- Java
- Title
- stack overflow recyclerview
- Category
- Java
- Title
- check if user has internet connection in kotlin
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- java function without return
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- java bubble sort short circuit
- Category
- Java
- Title
- java obtain list string from list object
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- armstrong number in java
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- [ERROR] Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /usr/lib/jvm/java-1.8.0-openjdk-amd64
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- dictionary in java
- Category
- Java
- Title
- hibernate Unknown integral data type for ids : java.lang.String
- Category
- Java
- Title
- java loop through map
- Category
- Java
- Title
- how to get individual words from a string in java
- Category
- Java
- Title
- displaying an arraylist in java
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- full shutdown windows 10 cmd
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- get drawable with string android java
- Category
- Java
- Title
- java get an int from a char
- Category
- Java
- Title
- import java.util.*;
- Category
- Java
- Title
- spigot kill entity
- Category
- Java
- Title
- copy array in java
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- how to make a minecraft hacked client drawing the hud malicious intent
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- how to count an replace string in java
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- java script using navigator.useragent to detect the browse
- Category
- Java
- Title
- how to write a perfect shuffle method in java
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java