# Binary numbers with length and charAt function

**URL:** <https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 29, 2020, 6:24pm UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817 "2020-11-29T18:24:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [November 29, 2020, 6:24pm UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817/1 "2020-11-29T18:24:29Z")

</div>

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?

```auto
 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));
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 29, 2020, 7:57pm UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817/2 "2020-11-29T19:57:28Z")

</div>

> [@Ellie98](#):
>
> am I able to do a loop until 32 without writing all numbers into print?

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

---

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [November 29, 2020, 9:12pm UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817/3 "2020-11-29T21:12:51Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 29, 2020, 11:52pm UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817/4 "2020-11-29T23:52:38Z")

</div>

> [@Ellie98](#):
>
> Why isn’t it working with System.out.printf?

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)

> [@Ellie98](#):
>
> Could someone explain this code

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

```auto
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:

```auto

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).

```auto

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. 😉

See how the numbers build up:

for 15:

```auto
15 1
7 11
3 111
1 1111

result 
15: 1111

```

for 16:

```auto
16 0
8 00
4 000
2 0000
1 10000

result 
16: 10000

```

Chrisir

---

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [November 30, 2020, 12:41am UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817/5 "2020-11-30T00:41:57Z")

</div>

Thank you Chrisir!! 😀

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 30, 2020, 12:56pm UTC](https://discourse.processing.org/t/binary-numbers-with-length-and-charat-function/25817/6 "2020-11-30T12:56:55Z")

</div>

Hello @Ellie98

Consider bit manipulation:

- [https://en.wikipedia.org/wiki/Bit\_manipulation](https://en.wikipedia.org/wiki/Bit_manipulation)
- [https://processing.org/reference/rightshift.html](https://processing.org/reference/rightshift.html)

A very simple example:

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5de61158bd9d02dddd35d16cb5d512d2a815be24.png)

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](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](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.

`:)`
