When you work on image pixel manipulation, you may use bit shift operators such as below:
// assume a,r,g,b is max 8 bits (0-255) value int pixel = (a << 24 )| (r << 16) | (g << 8) | b;Now I give you a simple question - what will you do for recovering original a, r, g, b value from the pixel int?
You may think the code below:
int aa = (v & 0xff000000) >> 24; int rr = (v & 0xff0000) >> 16; int gg = (v & 0xff00) >> 8; int bb = (v & 0xff);Actually the above code is wrong. Do you know where it is?
The wrong part is right shift operator used for getting value of "aa".
In Java, the most left bit is used as a signed bit.
If you use the right shift operator to the int which has 1 on the most left bit, the most left bit of the right shifted int remains in 1 to save sign.
In the above question case, you should use unsigned right shift operator (>>>) which fills all 0 to bits after right shifted.
So the answer should be....
int aa = (v & 0xff000000) >>> 24; // unsigned right shift!! int rr = (v & 0xff0000) >> 16; int gg = (v & 0xff00) >> 8; int bb = (v & 0xff);
コメント