简体   繁体   English

如何在 Java/Groovy 中将 int 数组和字节相乘?

[英]How to multiply int array and byte in Java/Groovy?

I have a function which accepts byte array.我有一个接受字节数组的 function。 I want to multiply each element of byte array to int array of size 8. How can i multiply element-by-element.我想将字节数组的每个元素乘以大小为 8 的 int 数组。如何逐个元素相乘。

Here is the function:这是 function:

  //  modulation function
  private float[] bytes2signal(byte[] buf) {
    float[] signal = new float[buf.length * 8 * SAMPLES_PER_SYMBOL * 2]
    int num_stations = 8
    
    float [] channel_sequence = new float[buf.length * 8 * SAMPLES_PER_SYMBOL * 2];

    SimpleCdma channel = new SimpleCdma();
    channel.setUp(signal, num_stations);

    int[][] wtable = channel.getWalshTable(num_stations)

    for (int i = 0; i < num_stations; i++) {
      // I want to multiply only 2nd row with each element of byte array 
      channel_sequence[i] = (float) ((int) (buf[i] * wtable[1][i]));
    }}

Here is the example of the walsh table matrix/array:这是 walsh 表矩阵/数组的示例:

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 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 1 -1 

Let's say if my byte array is [00000001, 00000100, 00000110] and my 2nd row of walsh table is 1 -1 1 -1 1 -1 1 -1 then I want to multiply [0 0 0 0 0 0 0 1]*[1 -1 1 -1 1 -1 1 -1] and expecting [0 0 0 0 0 0 0 -1] this.假设我的字节数组是[00000001, 00000100, 00000110]并且我的 walsh 表的第二行是1 -1 1 -1 1 -1 1 -1那么我想乘以[0 0 0 0 0 0 0 1]*[1 -1 1 -1 1 -1 1 -1]并期待[0 0 0 0 0 0 0 -1]这个。

How can i perform this multiplication?我怎样才能执行这个乘法?

Using the getBit function from here , you can easily access a bit from a byte, so you only need to use some loops:使用这里getBit function ,您可以轻松地从一个字节访问一个位,因此您只需要使用一些循环:

int[][] results = new int[mask.length][mask[0].length]

for (i = 0; i < mask.length; i++) { 
    for (j = 0; j < mask.length; j++) { 
        results[i][j] = mask[i][j] * getBit(our_bytes[i], j)
    } 
} 

getBit function in case the link dies: getBit function 以防链接失效:

private static int getBit(byte[] data, int pos) {
      int posByte = pos/8;
      int posBit = pos%8;
      byte valByte = data[posByte];
      int valInt = valByte>>(8-(posBit+1)) & 0x0001;
      return valInt;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM