# 5 Bit Barker Code

**URL:** <https://discourse.processing.org/t/5-bit-barker-code/31141>\
**Category:** Coding Questions\
**Created:** [July 9, 2021, 4:34pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141 "2021-07-09T16:34:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 9, 2021, 4:34pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/1 "2021-07-09T16:34:14Z")

</div>

hi all

I want to perform multiplication and addition for this array so that the result is as follows  
thanks in advance

```auto
int [] intArray = new int[] {1, 1, 1,-1, 1};
println("Original Array:");
for (int i=0; i<intArray.length; i++)
print(intArray[i] + " ");
println();
println(" reverse order:");
for (int i=intArray.length-1; i>=0; i--)
print(intArray[i] + " ");

------------------------------------------------------
i want to reach this result 

{1, 1, 1,-1, 1} orginal * reverse {1,-1, 1, 1, 1}

 1 1 1 -1 1
  -1 -1 -1 1 -1
      1 1 1 -1 1
         1 1 1 -1 1
           1 1 1 -1 1
            
 ------------------------------------------------------           
 1 0 1 0 5 0 1 0 1
  

```

 ![array](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/74396e403a30bc8545cc1737137e0407c17ac50c.jpeg)

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [July 10, 2021, 9:50am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/2 "2021-07-10T09:50:36Z")

</div>

You can change your array to a [IntList](https://processing.org/reference/IntList.html) how have reverse method implement

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 10, 2021, 10:22am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/3 "2021-07-10T10:22:19Z")

</div>

thank you for this hint

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 11, 2021, 10:25pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/4 "2021-07-11T22:25:21Z")

</div>

hi all

i made a code but its very long how i can use for loop to make it shorter

```auto

int[] numbers = new int[5];

void setup() {
  size(200, 200);
  numbers[0] = 1;  
  numbers[1] = 1; 
  numbers[2] = 1;  
  numbers[3] = -1; 
  numbers[4] = 1;  
  int a = numbers[4] *numbers[0]; 
  int b = numbers[4] * numbers[1]; 
  int c = numbers[4] * numbers[2]; 
  int d = numbers[4] * numbers[3]; 
  int e = numbers[4] * numbers[4]; 
  println(a, b, c, d, e);

  int a1 = numbers[3] *numbers[0]; 
  int b1 = numbers[3] * numbers[1]; 
  int c1 = numbers[3] * numbers[2]; 
  int d1 = numbers[3] * numbers[3]; 
  int e1= numbers[3] * numbers[4];  
  println(a1, b1, c1, d1, e1);

  int a2 = numbers[2] *numbers[0]; 
  int b2 = numbers[2] * numbers[1]; 
  int c2 = numbers[2] * numbers[2]; 
  int d2 = numbers[2] * numbers[3]; 
  int e2= numbers[2] * numbers[4];  
  println(a2, b2, c2, d2, e2);
  int a3 = numbers[1] *numbers[0]; 
  int b3 = numbers[1] * numbers[1]; 
  int c3 = numbers[1] * numbers[2]; 
  int d3 = numbers[1] * numbers[3]; 
  int e3= numbers[1] * numbers[4];
  println(a3, b3, c3, d3, e3);
  int a4 = numbers[0] *numbers[0]; 
  int b4 = numbers[0] * numbers[1]; 
  int c4 = numbers[0] * numbers[2]; 
  int d4 = numbers[0] * numbers[3]; 
  int e4= numbers[0] * numbers[4];
  println(a4, b4, c4, d4, e4);
  int x1 = a;
  int x2 = b+a1;
  int x3 = c+b1+a2;
  int x4 = d+c1+b2+a3;
  int x5 = e+d1+c2+e3+a4;
  int x6 = e1+d2+c3+b4;
  int x7 = e2+d3+c4;
  int x8 = e3+d4;
  int x9 = e4;
  println(x1, x2, x3, x4, x5, x6, x7, x8, x9);
}

void draw() {
}

```

 ![Untitled 203](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/80c9e73724bb0d8ce25ea5021e9a9475803f7f80.jpeg)

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [July 12, 2021, 10:21am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/5 "2021-07-12T10:21:32Z")

</div>

Hey  
you can do the upper half with a nested for loop but I don’t know if you can do the lower half with a for loop. Unfortunately, I didn’t manage to do it 😟

I hope I could help you 🙂

Here is my code:

```auto
int[] numbers = new int[]{1, 1, 1, -1, 1};

ArrayList<Integer> abc;

void setup() {
  size(200, 200);

  surface.setVisible(false);//so that the canvas is not displayed

  abc = new ArrayList<Integer>();

  //index 0 = 4 * 0
  //index 1 = 4 * 1
  //index 2 = 4 * 2
  //index 3 = 4 * 3
  //index 4 = 4 * 4

  //index 5 = 3 * 0
  //index 6 = 3 * 1
  //index 7 = 3 * 2
  //index 8 = 3 * 3
  //index 9 = 3 * 4

  //index 10 = 2 * 0
  //index 11 = 2 * 1
  //index 12 = 2 * 2
  //index 13 = 2 * 3
  //index 14 = 2 * 4

  //index 15 = 1 * 0
  //index 16 = 1 * 1
  //index 17 = 1 * 2
  //index 18 = 1 * 3
  //index 19 = 1 * 4

  //index 20 = 0 * 0
  //index 21 = 0 * 1
  //index 22 = 0 * 2
  //index 23 = 0 * 3
  //index 24 = 0 * 4

  //int a = 0; 
  //int b = 1; 
  //int c = 2; 
  //int d = 3; 
  //int e = 4; 
  //println(a, b, c, d, e);

  //int a1 = 5; 
  //int b1 = 6; 
  //int c1 = 7; 
  //int d1 = 8; 
  //int e1 = 9;  
  //println(a1, b1, c1, d1, e1);

  //int a2 = 10; 
  //int b2 = 11; 
  //int c2 = 12; 
  //int d2 = 13; 
  //int e2 = 14;  
  //println(a2, b2, c2, d2, e2);

  //int a3 = 15; 
  //int b3 = 16; 
  //int c3 = 17; 
  //int d3 = 18; 
  //int e3 = 19;
  //println(a3, b3, c3, d3, e3);

  //int a4 = 20; 
  //int b4 = 21; 
  //int c4 = 22; 
  //int d4 = 23; 
  //int e4 = 24;
  //println(a4, b4, c4, d4, e4);

  for (int i = numbers.length-1; i >= 0; i--) {
    for (int j = 0; j < numbers.length; j++) {
      int a = numbers[i] * numbers[j];
      print(a + " ");
      abc.add(a);
    }
    println("");
  }

  int x1 = abc.get(0);
  int x2 = abc.get(1) + abc.get(5);
  int x3 = abc.get(2) + abc.get(6) + abc.get(10);
  int x4 = abc.get(3) + abc.get(7) + abc.get(11) + abc.get(15);
  int x5 = abc.get(4) + abc.get(8) + abc.get(12) + abc.get(19) + abc.get(20);
  int x6 = abc.get(9) + abc.get(13) + abc.get(17) + abc.get(21);
  int x7 = abc.get(14) + abc.get(18) + abc.get(22);
  int x8 = abc.get(19) + abc.get(23);
  int x9 = abc.get(24);
  println(x1, x2, x3, x4, x5, x6, x7, x8, x9);
}

void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 12, 2021, 2:44pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/6 "2021-07-12T14:44:44Z")

</div>

[Flolo](https://discourse.processing.org/u/Flolo)

hi  
nice code for 5 elements but for 16 or 32 it is going to be much longer i have tried to use for loop but failed until now thanks for your time

---

<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:** [July 12, 2021, 3:02pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/7 "2021-07-12T15:02:37Z")

</div>

What is the purpose of the arrays?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 12, 2021, 3:21pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/8 "2021-07-12T15:21:53Z")

</div>

@Chrisir

thanks for response …actually it is Barker code 5 bits calculation and it might up to length codes of _N_ \< 10^22 and calculation 13 bit length as i did with the 5 bit length with out for loop is going to be very long code

this is the idea and you can see what i calculated i take length 5 which is {1, 1, 1,-1, 1};

 ![Untitled 204](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/add9ec17a04db456fe59eea5039593291f0ade6e.jpeg)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 12, 2021, 3:28pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/9 "2021-07-12T15:28:04Z")

</div>

@Chrisir the idea of solving is simple

[![](https://img.youtube.com/vi/qek4-ET1d9Q/maxresdefault.jpg "ECE3084 Lecture 12.5: Autocorrelation, Pulse Compression, and Barker Codes (Signals & Systems)") ](https://www.youtube.com/watch?v=qek4-ET1d9Q)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 12, 2021, 3:42pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/10 "2021-07-12T15:42:23Z")

</div>

@Chrisir

this is some information with c

> <https://stackoverflow.com/questions/42278458/implementing-iterative-autocorrelation-process-in-c-using-for-loops>

---

<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:** [July 12, 2021, 6:00pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/11 "2021-07-12T18:00:50Z")

</div>

Sorry, I am on a journey now and can’t really help you

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 12, 2021, 6:03pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/12 "2021-07-12T18:03:46Z")

</div>

it is fine when you can i am not in hurry have fun

---

<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:** [July 12, 2021, 7:15pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/13 "2021-07-12T19:15:30Z")

</div>

Okay, when you only have 16 or 32 items specifically then write a code that use println in a for loop

Let it println the lines of code you need for the main Sketch such as

> [@Flolo](#):
>
> ```auto
> int x7 = abc.get(14) + abc.get(18) + abc.get(22);
> int x8 = abc.get(19) + abc.get(23);
> 
> ```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 12, 2021, 7:26pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/14 "2021-07-12T19:26:30Z")

</div>

@Chrisir you are true but if my array like this

```auto
int [] intArray = new int[] {1, 1, 1,-1, 1,+1 +1 +1 +1 +1 −1 −1 +1 +1 −1 +1 −1 +1};

```

solving it in this way going to take very long lines

```auto
 int x1 = abc.get(0);
  int x2 = abc.get(1) + abc.get(5);
  int x3 = abc.get(2) + abc.get(6) + abc.get(10);

```

---

<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:** [July 12, 2021, 7:35pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/15 "2021-07-12T19:35:49Z")

</div>

When you write the code lines automatically by another Sketch B it doesn’t matter

In fact you can let B insert “\n” after each „+“ automatically

Just use ctrl-t in your main Sketch A after pasting the result from B into A

---

<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:** [July 12, 2021, 7:38pm UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/16 "2021-07-12T19:38:21Z")

</div>

Here is an example for a Sketch that writes code

It generates for loop, your Sketch B generates code with + etc.

> [@Tools for you: Search file name, Join pde-files and others](https://discourse.processing.org/t/tools-for-you-search-file-name-and-others/10517/8):
>
> Did you ever had to write 9 nested for-loops? for (int i0 = 0; i0 \< 2; i0++) { for (int i1 = 0; i1 \< 2; i1++) { for (int i2 = 0; i2 \< 2; i2++) { .... ...... I wrote another Sketch to write these 9 lines with for-loops for me, so I don’t have to do it. You can copy paste the auto-generated code from the direct window in processing: void setup() { size(800, 600); int numberOfDesiredForLoops = 9; for (int i = 0; i \< numberOfDesiredForLoops; i++) { …

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 14, 2021, 1:33am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/17 "2021-07-14T01:33:36Z")

</div>

```auto
int[] a = new int[] {1,2,3,4,5};
int[] ar = new int [a.length];
int[][] b = new int[a.length][a.length*2-1];
int counter = 0;
int[] c = new int[a.length*2-1];
int[][]d = new int [a.length][a.length];
for (int i=0; i<a.length; i++)ar[i] = a[a.length-1-i];
for (int i=0; i<a.length; i++) {

  println("ar",ar[i],"a",a[i]);
  for (int j=0; j<a.length; j++) {
    b[i][j+i] = ar[i]*a[j];
    
    println("output",b[i][j+i],ar[i],a[j]);
  }
  println("index", i, a[i]);
}
for (int i=0; i<b.length; i++) {
  String s = "";
  for (int j=0; j<b[i].length; j++) {
    s+= b[i][j];
     
  }
  println(s);
}
for (int i=0; i<b[0].length; i++) {

  counter = 0;
  for (int j=0; j<b.length; j++) {
    int pos = j;
    counter+= b[j][i];
  }
  c[i] = counter;
  counter++;
}
println("",counter);
for (int i=0; i<c.length; i++) {
print(c[i]);
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 14, 2021, 2:25am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/18 "2021-07-14T02:25:58Z")

</div>

@paulgoux

thank you it is creative idea

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 14, 2021, 2:26am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/19 "2021-07-14T02:26:46Z")

</div>

It probably can be simplified but for loop and array logic isn’t my forte

Good little exercise though

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 14, 2021, 2:30am UTC](https://discourse.processing.org/t/5-bit-barker-code/31141/20 "2021-07-14T02:30:05Z")

</div>

@paulgoux

the way you used is very nice

[Next page](https://discourse.processing.org/t/5-bit-barker-code/31141.md?page=2)
