-5
private static final byte[] BitPMC1 = { 56, 48, 40, 32, 24, 16, 8, 0, 57,
        49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51,
        43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13,
        5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 };

 byte[] outData= new byte[] { 0, 0, 0, 0, 0, 0, 0 };

 for (int i = 0; i < 56; i++) {
     if ((inData[BitPMC1[i] >> 3] & (1 << (7 - (BitPMC1[i] & 0x07)))) != 0) {
    outData[i >> 3] |= (1 << (7 - (i & 0x07)));
     }
 }

java  outData[i >> 3]  and c#  different
RamonBoza
  • 8,898
  • 6
  • 36
  • 48

3 Answers3

1

Your input values are all in the range 0..127 so there's no problem there.

On the output, but only after all the calculations are done, you need to convert the byte values into int values, whilst also converting the twos-complement negative values to positive:

int unsigned_value = ((int)byte_value) & 0xff;

If you output each element of the array per the above, you should find that the values you get out match your C# code.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

You don't describe what your code should do (e.g. giving some example numbers) thus I can only guess.

Having unsigned data, it does not make sense to use an operation taking care of the sign bit (>>), probably the operation you need is a logical shift (>>>)

More details here: Difference between >>> and >>

Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • none of the right shifts are on "negative" numbers therefore there's no need to use `>>>` instead of `>>`. – Alnitak Oct 24 '13 at 10:13
  • @Alnitak: You are right, for the given input values there is no difference. I assumed the code should be fine for the full byte range. – Daniel Oct 24 '13 at 10:52
  • Whilst _in general_ it makes sense not to use `>>` when manipulating "unsigned" data, in the OP's case this would not resolve his problem. – Alnitak Oct 24 '13 at 11:35
-1

I think you need to use the operator >>> instead of >>, you can have problems when your number is higher than 127, it would become something negative. Well, this might help you: Can we make unsigned byte in Java

Cheers

Community
  • 1
  • 1
Juan
  • 1,754
  • 1
  • 14
  • 22
  • per my comment on one of the other answers - none of the numbers on which the right shift operator are used are outside the range `0 .. 127` so there's no need to use the unsigned shift operator. – Alnitak Oct 24 '13 at 10:37
  • But it will be after applying the operator >> 3 times. If the number is >=32, after moving three bits it becomes 128, which is already a negative number. If it were unsigned byte, it will work well, but if 7 bits are used instead of 8, the result would be different. But this just depends on what the function should do. – Juan Oct 24 '13 at 10:56
  • No, look again. There are only two right shifts - `BitPMC1[i] >> 3` and `i >> 3`. The `BitPMC1` array is constant and only contains values `0..127`. `i` is the loop counter and goes from `0 .. 55`. – Alnitak Oct 24 '13 at 11:33
  • Which is higher than 32 – Juan Oct 25 '13 at 17:45
  • huh? It's only "signed" values with bit 7 set (i.e. negative numbers, or "unsigned" values > 127) that would be affected. 32 doesn't come into it. – Alnitak Oct 25 '13 at 19:18