Original Post
Hey all!
Just had a quick question that I've been trying to debug on my own to no avail. I'm currently following a tutorial on how to manipulate an image's pixels and colors through bitwise operations but I seem to have reached a part that confounds me.
At one part he writes this line of code
pixels & 0xff;
and says that this line of code will "get rid of the alpha channel" from the colors.
***********************************************************************************************************************************************************************************
Now what I understand is that this "&" operator goes through the bits and performs the logical "and" operation. So if you and'ed 8 and 2 you would get:
8 ------- 1000
2 ------- 0010
5 ------- 0101
as it returns 1 if the parallel bits are the same, and 0 if they're not.
***********************************************************************************************************************************************************************************
So as a test I and'ed pure opaque blue [0xFF0000FF(dec value 255)] with the supposed alpha channel [0xFFFF(dec value 255)] and continually lowered the blue all the way to zero;
The results I got were simply the color value (255,254,253,......,0) but if I increased the color value above 255 I would simply get a zero back.
[source lang="java"]for(int i = 255; i >0; i--)
{
System.out.println(i & 255);
}
[/source]
255,254,253,252.....0
From my results I then assumed that this operation then just returned all values less than or equal to the second operand(in this case 0xFF) and returned 0 for all values greater than the second operand. However, I tried to replicate this with other numbers like 16&16/0x10&0x10 but the results did not match. With those numbers, any number that was not equal to the second operand yielded 0, and if equal yielded the second operand(16,0,0,0,0.....)
[source lang="java"] public static void loop()
{
for (int i = 16; i > 0; i--)
{
System.out.println(i&16);
}
}[/source]
16,0,0,0,0,0,0.......0
I kept checking numbers to see when I would return to the original behavior that I observed, and noted that it doesn't return to the pattern I saw before but some numbers will give sequences like (128,128,128,128,128,0,0,0,0,0,64,64,64,0,0,0,) and such.
Thus I am completely baffled and ask for your help in this. I understand what the & operator does, but I don't understand how it's being "used" here. I will include the video link to the tutorial I'm following. The code is very similar to the code I saw Notch use on Minicraft for Ludum Dare which I'm guessing is where the person in the tutorial learned from. The rest of his videos continue on in this fashion using a lot of bitwise operations so it's crucial for me to grasp on to how he's using it here. He didn't explain very well how he uses what he uses.
[media]
Thank you again for your help!
-Adrian
Just had a quick question that I've been trying to debug on my own to no avail. I'm currently following a tutorial on how to manipulate an image's pixels and colors through bitwise operations but I seem to have reached a part that confounds me.
At one part he writes this line of code
pixels & 0xff;
and says that this line of code will "get rid of the alpha channel" from the colors.
***********************************************************************************************************************************************************************************
Now what I understand is that this "&" operator goes through the bits and performs the logical "and" operation. So if you and'ed 8 and 2 you would get:
8 ------- 1000
2 ------- 0010
5 ------- 0101
as it returns 1 if the parallel bits are the same, and 0 if they're not.
***********************************************************************************************************************************************************************************
So as a test I and'ed pure opaque blue [0xFF0000FF(dec value 255)] with the supposed alpha channel [0xFFFF(dec value 255)] and continually lowered the blue all the way to zero;
The results I got were simply the color value (255,254,253,......,0) but if I increased the color value above 255 I would simply get a zero back.
[source lang="java"]for(int i = 255; i >0; i--)
{
System.out.println(i & 255);
}
[/source]
255,254,253,252.....0
From my results I then assumed that this operation then just returned all values less than or equal to the second operand(in this case 0xFF) and returned 0 for all values greater than the second operand. However, I tried to replicate this with other numbers like 16&16/0x10&0x10 but the results did not match. With those numbers, any number that was not equal to the second operand yielded 0, and if equal yielded the second operand(16,0,0,0,0.....)
[source lang="java"] public static void loop()
{
for (int i = 16; i > 0; i--)
{
System.out.println(i&16);
}
}[/source]
16,0,0,0,0,0,0.......0
I kept checking numbers to see when I would return to the original behavior that I observed, and noted that it doesn't return to the pattern I saw before but some numbers will give sequences like (128,128,128,128,128,0,0,0,0,0,64,64,64,0,0,0,) and such.
Thus I am completely baffled and ask for your help in this. I understand what the & operator does, but I don't understand how it's being "used" here. I will include the video link to the tutorial I'm following. The code is very similar to the code I saw Notch use on Minicraft for Ludum Dare which I'm guessing is where the person in the tutorial learned from. The rest of his videos continue on in this fashion using a lot of bitwise operations so it's crucial for me to grasp on to how he's using it here. He didn't explain very well how he uses what he uses.
[media]
[/media]
Thank you again for your help!
-Adrian