Binary numbers with length and charAt function

Hello,
I have to get the binary numbers from 0-32. The tutors just gave us the solution by trying, but I didn’t really understand what is happening. Could someone explain this code to me?
How am I able to do a loop until 32 without writing all numbers into print?

 String DualZahl(int a){
  String dual = "";
  int i = a;
  while(i > 0){
    if(i % 2 !=0){
      dual += "1";
      
    }
    else{
      dual += "0";
    }
    i /= 2;
  }
  String dual1 = "";
  for(int c = dual.length()-1; c >= 0; c--){
    dual1 += dual.charAt(c);
  }
  return dual1;
}

void setup() {
  println(DualZahl(2));
}

You can do a for-loop i from 0 to 32 in setup() and use println (i + ": " + DualZahl(i) );

1 Like

Thank you! Why isn’t it working with System.out.printf?
I have to format the code

    for (int z = 0; z<=32; z++) {
      //println(z+ "->" +DualZahl(z));
    System.out.printf(z+ "%2d ->\n" +DualZahl(z));
    }
}

I think, println is a complex function that does a lot of work behind the scenes to be able to work with all different kinds of input (data types)

You have to make yourself familiar with what binary numbers are.

  • There are only 0 and 1.

4 decimal is: 100 binary etc.

because in our system in every “slot” we count to 9, in binary to 1.

Here is the list

0: 
1: 1
2: 10
3: 11
4: 100
5: 101
6: 110
7: 111
8: 1000
9: 1001
10: 1010
11: 1011
12: 1100
13: 1101
14: 1110
15: 1111
16: 10000
17: 10001
18: 10010
19: 10011
20: 10100
21: 10101
22: 10110
23: 10111
24: 11000
25: 11001
26: 11010
27: 11011
28: 11100
29: 11101
30: 11110
31: 11111
32: 100000

(it doesn’t really work for 0, should give 0)

Now, let’s look at the function.

first half it builds dual, 2nd half it just copies dual into dual1 backwards.

Which is a bit unnecessary, see here:



String DualZahl(int a) {
  String dual = "";
  int i = a;

  while (i > 0) {
    if (i % 2 !=0) {
      dual = "1" + dual ;
    } else {
      dual = "0" + dual ;
    }
    i /= 2;
  }
  return dual;
}

void setup() {
  for (int i=0; i <=32; i++)
    println (i + ": " + DualZahl(i) );
  //println(DualZahl(2));
}

Anyway. The 1st half loops over the input a (or i, it’s not necessary to copy a to i).



String DualZahl(int i) {  // renamed to i, deleted a 

  if (i==0)
    return "0";  // result for 0: 0 

  String dual = "";

  while (i > 0) {
    if (i % 2 != 0) {
      dual = "1" + dual ; // appending 1 on the LEFT 
    } else {
      dual = "0" + dual ; // appending 0 on the LEFT 
    }
    i /= 2;
  }
  return dual;
}

void setup() {
  for (int i=0; i <=32; i++)
    println (i + ": " + DualZahl(i) );
  //println(DualZahl(2));
}

It checks if the remainder (% 2) is != 0 (it can be only 1 then).

If so, we put 1 into dual (this is on the very right of dual!). It is not even, so the very right slot of the result number must be 1.

We split i by 2: i /= 2; but this an integer division so for example i = 15 / 2 gives 7 and not 7.5.

Now we do the same for the next number. It’s in a while loop so this goes on until i is 0.

I don’t get fully myself. :wink:

See how the numbers build up:

for 15:

15 1
7 11
3 111
1 1111

result 
15: 1111

for 16:

16 0
8 00
4 000
2 0000
1 10000

result 
16: 10000

Chrisir

1 Like

Thank you Chrisir!! :grinning:

1 Like

Hello @Ellie98

Consider bit manipulation:

A very simple example:

void setup() 
  {
  String binStr = "";
  char c = ' ';

  int a = 3;                  //Try different values from 0 to 3 or 0b00 to 0b11
  
  c = ((a&1)== 1) ? '1':'0';  // & (bitwise AND) operator
  binStr = c + binStr;
  a = a>>1;                   //  >>  (right shift) operator
  
  c = ((a&1)== 1) ? '1':'0'; 
  binStr = c + binStr;
  a = a>>1;  
  
  println (a, binStr);
  }

Note:
I have a nice small and elegant function for this.
I intentionally minimized this and unrolled the loop (partially) and removed the function.
It is not complete but can be easily made into a function.
Use println() statements to see what each step is doing.

Se bitwise operators in the references:
image

This is just one example; I also shifted the mask bit in other efforts.

Bit manipulation can be a challenge at first. Have fun!

I am assuming you need to write the code that does this and not use existing methods\functions.

The Processing binary() function is also available for use:
https://processing.org/reference/binary_.html
You can see what this does here:
https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L10304 Google Chrome takes about 6 seconds to find that line! Be patient.

:)