I have an array of bytes, byte bytes[], representing a greyscale image.
I want to invert the colours of this image - so figured I would just flip the bits (bitwise not).
I have attempted this as seen below (have included an extra loop to populate a dummy byte array to allow quick and easy re-creation of my problem)
Random rand = new Random();
byte bytes[] = new byte[500];
// populate a dummy byte array
for (int i = 0; i < bytes.length; ++i)
{
new Random().nextBytes(bytes);
System.out.println(bytes[i]);
}
for (int i = 0; i < bytes.length; ++i)
{
System.out.print(bytes[i] + " ");
//bytes[i] = ~bytes[i]; This throws an error as apparantly java treats a byte array as an integer array?!
bytes[i] = (byte)~bytes[i]; // This compiles but output not a bitwise not, results displayed below
System.out.println(bytes[i]);
}
The results I am getting are:
116 -117
48 -49
70 -71
What I'm looking for is: (I have added the binary manually to fuly illustrate what my understanding of bitwise not (please correct if wrong)
116 (01110100) = (10001011) 139
48 (00110000) = (11001111) 207
70 (01000110) = (10111001) 185
Thanks in advance for any advice
~that makes it an int (and yes you can just cast back)