Decimal into binary numbers

Hey,
i’m trying to convert the decimal numerbs into binary numbers but I get a lot of zeros in front of the binary numbers. How do I cut of the zeros?
Thanks in advance!

void setup() {
  //System.out.printf("Dez -> Dualzahl|\n");
  for (int a = 0, b = 32, c = 64, d=96, e=128, f=160, g=192, h=224; a <=31; a++, b++, c++, d++, e++, f++, g++, h++) {
    System.out.printf("%3d ->\n", a, b, c, d, e, f, g, h);
    System.out.printf("" + binary(a));
  }
}

Hi,

If you look at the binary() function, it’s returning the binary representation as a String.

In order to get a portion of a String in Java, use the subtring() method which takes to indices and return the corresponding substring.

You can write a method to cut the first n characters of a String :

/*
Cut the first n characters from a source String
*/
String trimCharacters(String source, int n) {
  return source.substring(n, source.length());
}

That you can use like this :

println(trimCharacters(binary(a), 20));

Note that it’s more coherent to use the pre defined print() and println() functions in Processing rather than classic Java System.out.print...

If you want to remove all the zeros, you need to compute the maximum exponent p where 2 to the power of p is less than or equal to your number a, which represents the length of your binary number without the zeros.

You can use another function to do this :

/*
Return the highest exponent p where 2**p < n
*/
int getHighestExponentPowerOfTwo(int n) {
  int p = 0;
  while (pow(2, p) < n) p++;
  return p;
}

It increase a value p and stops when 2**p > n.

Since the binary() function returns the 32 bit representation of the value, we need to cut 32 - binary size of a zeros from the string.

So finally it goes like this :

// We add one because the smaller power start with 0
int binaryLength = getHighestExponentPowerOfTwo(a) + 1;
int zerosToTrim = 32 - binaryLength;
println(trimCharacters(binary(a), zerosToTrim));

And you get :

0
1
10
011
100
0101
0110
0111
1000
01001
01010
01011
01100
01101
01110
01111
10000
010001
...

Note, this is a bit over complicated but it’s a nice exercise and it introduce you to functions if you are not used to :wink:

A better way would be to use the indexOf() method to get the first position of a 1 in the binary representation and cut from that index. I let you do this part!

3 Likes

Function binary() has a 2nd parameter where we can specify how many digits we need:

We can combine that w/ Integer.numberOfLeadingZeros() in order to know how many digits to remove outta total max of 32 bits:

https://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html#numberOfLeadingZeros(int)

for (int i = 0; i < 05000; i += 32) {
  final int digits = max(1, Integer.SIZE - Integer.numberOfLeadingZeros(i));
  println(i, TAB, binary(i, digits));
}
exit();
0 	  	 0
32 	  	 100000
64 	  	 1000000
96 	  	 1100000
128 	 10000000
160 	 10100000
192 	 11000000
224 	 11100000
256 	 100000000
288 	 100100000
320 	 101000000
352 	 101100000
384 	 110000000
416 	 110100000
448 	 111000000
480 	 111100000
512 	 1000000000
544 	 1000100000
576 	 1001000000
608 	 1001100000
640 	 1010000000
672 	 1010100000
704 	 1011000000
736 	 1011100000
768 	 1100000000
800 	 1100100000
832 	 1101000000
864 	 1101100000
896 	 1110000000
928 	 1110100000
960 	 1111000000
992 	 1111100000
1024 	 10000000000
1056 	 10000100000
1088 	 10001000000
1120 	 10001100000
1152 	 10010000000
1184 	 10010100000
1216 	 10011000000
1248 	 10011100000
1280 	 10100000000
1312 	 10100100000
1344 	 10101000000
1376 	 10101100000
1408 	 10110000000
1440 	 10110100000
1472 	 10111000000
1504 	 10111100000
1536 	 11000000000
1568 	 11000100000
1600 	 11001000000
1632 	 11001100000
1664 	 11010000000
1696 	 11010100000
1728 	 11011000000
1760 	 11011100000
1792 	 11100000000
1824 	 11100100000
1856 	 11101000000
1888 	 11101100000
1920 	 11110000000
1952 	 11110100000
1984 	 11111000000
2016 	 11111100000
2048 	 100000000000
2080 	 100000100000
2112 	 100001000000
2144 	 100001100000
2176 	 100010000000
2208 	 100010100000
2240 	 100011000000
2272 	 100011100000
2304 	 100100000000
2336 	 100100100000
2368 	 100101000000
2400 	 100101100000
2432 	 100110000000
2464 	 100110100000
2496 	 100111000000
2528 	 100111100000
3 Likes