2

I am declearing in Java

public byte[] orbits = new byte[38];

Now if I am doing

orbits[24] = (byte)0xFF;

orbits[24] should get populated by 11111111 i.e FF(in hexadecimal) but instead its getting populated with -1.

This operation in C++ working perfectly

char orbits[38]
orbits[24] = (char)0xFF;

How to replicate the similar situation in Java using byte? Thanks

JavaBits
  • 2,005
  • 11
  • 36
  • 40

1 Answers1

6

Well, it just happens that -1 is 0xFF. Everything is correct. byte stores values from -128 to 127 using two's complement.

In Java there are no unsigned types. If you want to use bit patterns, then use byte. 0xFF and -1 are the same thing in this situation. If you want to use numbers, that is, 0xFF is actually 255 and not -1, then you need to use a bigger type, like short.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • SO are you telling that if we compare them bit by bit then it will be equal in Java by lets say a for loop. – JavaBits Jun 30 '11 at 15:08