# Converting a number to binary and back as a test in Java

This is one of my first takes on Java, coming mainly from Matlab I found I had many things to learn. The code will try to convert a number into an array of ones and zeros and then do the opposite returning you the original number (or at least something very close to it, as it is hard to return the exact number). It is not tested very well, so I can only tell that it is symmetric and not that it is correct.

It would be nice to know any errors I have made, or things that I could improve. An idea I had was using BigInt, I have read about it but not yet used it.

If you could focus on the language specific issues and kindly point them out I would be grateful.

package baseconversion;

import java.lang.Math;
import java.util.Arrays;

public class FloatToBin{

public static double pow2(double a, double b) {
return a * (java.lang.Math.pow(2, b));
}

public static double rem(double a, double b) {
return a % b;
}

public static double fix(double val) {
if (val < 0) {
return Math.ceil(val);
}
return Math.floor(val);
}

public static double vConv(int[] a, double[] b) {
int aRows = a.length;
int bRows = b.length;
double c = 0;
for (int i = 0; i < aRows; i++) {
c += a[i] * b[i];
}
return c;
}

public static void main(String []args){
double a = 340.3562;
int n = 20;
int m = 20;

int count = 0;

int[] out = new int[n + m];

for (int i = -(n-1); i <= m; i++) {
out[count] = (int)rem(a * pow2(1, (double)i), 2);
count++;
}

double[] temp = new double[n + m];
double origVal;
count--;
int revCount = 0;

for (int ii = n - 1; ii >= -m; ii--) {
temp[revCount] = pow2(1, (double)ii);
count--;
revCount++;
}

origVal = vConv(out, temp);

System.out.println("Original number is:");
System.out.println(a);
System.out.println("Binary representation is:");
System.out.println(Arrays.toString(out));
System.out.println("Inverting we get:");
System.out.println(origVal);
}
}

• Welcome to CodeReview, green_leaf. I hope you get some fine answers! Apr 25, 2015 at 15:11
• Are you sure the code works? An example run is: Original number is: 41.52 Binary representation is: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1] Inverting we get: 0.5199995040893555 Apr 25, 2015 at 16:17
• Maybe you only care about the decimal part? Apr 25, 2015 at 16:19
• @Caridorc I am sorry for that, I was struggling with java and did not notice the error. Please do try the updated version. Apr 25, 2015 at 16:48
• @green_leaf thanks for the fast fixing :) Apr 25, 2015 at 16:50

## Cryptic names

int n = 10;
int m = 20;


Please give longer and more descriptive names to n and m

## Overworked main

I would define outside of main

public static double FloatToBin(float n) {

}

public static double BinToFloat(bin n) {

}


So that main can contain the IO only.

• @green_leaf - It's ok for you to c9e w2h w5r s3m you l2e but you c4t a4e the r4r w2l u7d. J2t so l2g as you get ok r4s. Apr 25, 2015 at 17:21
• I'm just "trusting" that @green_leaf knows the theory behind my naming convention... right? - in case you were not aware, I have put the first letter, and last letter of each word, and then, if there are more than 1 letter in between, I have indicated how many there are... Apr 25, 2015 at 17:32
• @rolfl yup, that was exactly the idea :) Let whoever has to deal with my code figure everything out, I don't have time to deal with that, plus I have the numbers that I wanted to and its really late...let me just write a comment wishing good luck to whoever might need this in the future and I'm done. Apr 25, 2015 at 17:41
• In a more serious tone though, I do hope that by hanging out here I will learn how to do things properly, all constructive criticism is welcomed and expected. Apr 25, 2015 at 17:42
• In which case, your intentional laziness and the professor who suggests it are a liability to the industry. Code is written once, and read many times. Making the code hard to read is egotistical, arrogant, and a disservice to yourself and others. Apr 25, 2015 at 17:43

Use imports consistently. You've fully qualified java.lang.Math.pow but imported java.lang.Math and called Math.floor. Do one or the other (the second being better). You can also statically import methods, e.g. import static java.lang.Math.pow and then just call pow(2, b).

The rem function seems a bit pointless to me. I'd just inline it.

The pow2 function is always called with a == 1. Ged rid of the argument. Then get rid of the function and just call pow(2, x) directly.

• Thank you for the import consistency comment, I scrolled up at my code the moment I read it, and it makes no sense using both ways. pow2 is the way it is in order to remain consistent with the Matlab version. rem indeed should not be there, again I just copied it from matlab. Apr 26, 2015 at 6:30