binary shifting

edited October 2013 in How To...

I have seen alot of cases where binary shifting is done on a number especially color

and I have a hard time understanding it :^o

even the processing website says the bit shifting is faster

this is what I have tried to gain an understanding sadly no luck

especially when anding a number with 0xFF would a kind please explain them to me

PS: new here no idea which category this question so I posted in How To so please bear with me :o3

void setup(){
      size(600,600);
      noLoop();
    }
    color c=color(255,255,255);
    void draw(){
      strokeWeight(3);

      for(int i=0;i<width/3;i++){
        stroke(c);
        line(i*3,0,i*3,width);
        println("orig c="+binary(c));
        int a = (c >> 24);
        println("alpha o= "+binary(a));
            a = (c >> 24) & 0xFF;
        println("alpha &="+binary(a));

        int r = (c >> 16);
        println("red o= "+binary(r));

         r = (c >> 16) & 0xFF;
        println("red &= "+binary(r));
        int g = (c >> 8) & 0xFF;
        println("green= "+binary(g));
        int b = c & 0xFF;
        println("blue=  "+binary(b)); 
        //c=c << 24;
        //println("red= "+r+" green="+ g+" blue="+b);
      }
    }

    void mousePressed(){
      redraw();
    }
Tagged:

Answers

  • edited October 2013 Answer ✓

    I believe you already know Processing stores ARGB color values as data-type int.
    Each 1 of the ARGB elements uses up 1 byte (8 bits); totaling 4 bytes (32 bits).
    I guess you wanna know the logic behind the process of extracting each color element?

    Let's start w/ blue (B). That occupies the least significant byte to the far right.
    So, to isolate the B from the others, we simply need to apply an And (&) mask.
    That mask is exactly a full byte. That is 255, 0xFF, 0377 or 0xb11111111.
    Doing c & 0xFF, we zero all the other bits from left, while leaving the right-most byte intact!

    And what about the other color channels like A, R, and G?
    Trick is to right-shift a chosen element a # of bits to the far-right. Exactly where B would be!

    This time we're gonna choose red (R). R is 2 bytes (16 bits) apart from where B is.
    So we need an extra step of bit-shifting it 16 bits (0x10, 020 or 0xb1111111111111111) to right:
    c >> 16 or c >> 0x10 or c >> 020.

    Afterwards, just apply the And mask as we did w/ B, since it's located exactly there now. That's it!

  • thanks very much I was actually very confused after I looked at your answer I looked on the explanation of & on the processing site and they gave a very good example what I was confused about was I that I though that oxFF was equal to 11111111111111111111111111111111 instead of
    00000000000000000000000011111111

    when u said the mask was a full byte I though huh :-? I read about bitwise operations on wikipedia my first source should have been processing website

    sos for troubling u X_X

  • one hex digit (0 to f) is 4 bits (0000 to 1111). so 0x0f is 8 bits, one byte

Sign In or Register to comment.