bit manipulation - Shifting bits java -


to insert value, start using mask clear out 8-bits of pixel corresponding given color channel. example, in case of red, shift 8-bit mask of ones left 16 bits, invert (using ~ operator), , “and” (&) mask rgb value, clears out 8 bits of red , leaves other bits unchanged. next, shift parameter value (red, in case) left same number of bits (16, in case of red) , “or” (|) shifted value pixel value.

int getred(){     red = (pixel>>16);     red = ~pixel;     red = pixel<<16 | pixel;     return red; } 

what doing wrong according directions?

the problem here seems fundamental problem in understanding how assignment works (in java ... , every imperative programming language!). example:

red = (pixel>>16); red = ~pixel; 

that says:

  1. assign red value of pixel shifted 16 bits

  2. assign red value of pixel negated bitwise. clobbers value of red calculated in previous step.

if want negate value calculated in step 1, need this:

red = ~red; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -