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:
assign
redvalue ofpixelshifted 16 bitsassign
redvalue ofpixelnegated bitwise. clobbers value ofredcalculated in previous step.
if want negate value calculated in step 1, need this:
red = ~red;
Comments
Post a Comment