0

Look the code below:

image.getRGB(x,y) & 0x000000FF

When the result image.getRGB(x,y) is -16777216, the AND operation result is 0 (BLACK COLOR)

When the result image.getRGB(x,y) is -1, the AND operation result is 255 (WHITE COLOR)

My question is, if you look to my related article in Java - Understanding about image.getRGB(x,y) output in Binary term you can see the result of image.getRGB(x,y) in binary term include Alpha, Red, Green, and Blue (32 bit) but 0x000000FF is only 8-bit; for example:

image.getRGB : 1111 1111 0000 0000 0000 0000 0000 0000
0x000000FF : 1111 1111
image.getRGB & 0x000000FF : 0000 0000

Second Example :

image.getRGB : 1111 1111 1111 1111 1111 1111 1111 1111
0x000000FF : 1111 1111
image.getRGB & 0x000000FF : 1111 1111

How they compare between 8 bit of 0x00000FF with 32 bit image.getRGB so that they get result 0 or 255 like my case above?

Community
  • 1
  • 1
Mr. Mike
  • 453
  • 5
  • 23
  • Are you sure you made no mistake. This code outputs: `System.out.println(0xFF<<24 & 0xFF); → 0` `System.out.println(Color.black.getRGB()); → -16777216` `System.out.println(Color.black.getRGB() & 0xFF); → 0` – Maljam Mar 27 '16 at 06:21
  • @maljam : sorry, i has been edited my question. you can check again above. thanks – Mr. Mike Mar 27 '16 at 06:22
  • `-16777216` == `0xFF00000` – Jim Garrison Mar 27 '16 at 06:32

1 Answers1

4

Simply think of them as numbers, and regardless of the number of bits, you should align the numbers with the bits representing the same power of 2 together and then do the binary operation:

im.getRGB :       1111 1111 0000 0000 0000 0000 0000 0000
0x000000FF :      0000 0000 0000 0000 0000 0000 1111 1111
                  ^^^^ ^^^^                     ^^^^ ^^^^
im.getRGB & 0xFF: 0000 0000 0000 0000 0000 0000 0000 0000 = 0

Same thing with the other example:

im.getRGB :       1111 1111 1111 1111 1111 1111 1111 1111
0x000000FF :      0000 0000 0000 0000 0000 0000 1111 1111
                  ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
im.getRGB & 0xFF: 0000 0000 0000 0000 0000 0000 1111 1111 = 255
Maljam
  • 6,244
  • 3
  • 17
  • 30