04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the number, as shown in the following equations—remember that any number raised to<br />

the 0 power is equal to 1—that is, n 0 = 1):<br />

Now let’s go back to the alpha setting in base 2:<br />

The 255 probably rings a bell, as it’s the highest value any of the individual RGBA components<br />

can have.<br />

Bitwise operations to the rescue<br />

Referring back to the <strong>Processing</strong> reference, if you call the function float r =<br />

red(myColor); by passing in a color argument (which I’m calling myColor), the red value is<br />

pulled out of the color <strong>and</strong> returned as a float value, which I’m assigning to the float<br />

variable r. Well, a faster way to get the red value is to perform a bitwise operation on the<br />

color. Now you may say, big deal, computers are fast. This is way too confusing to warrant<br />

the little savings in time. However, imagine now you have an image that is 1,500 pixels<br />

times 1,000 pixels (or a total of 1,500,000 pixels) <strong>and</strong> you want to do a series of manipulations<br />

to all the pixels in the image, <strong>and</strong> maybe you even want to animate it—well, processing<br />

time is now a huge deal, <strong>and</strong> the bitwise operators may very well save the day.<br />

Again, here’s the expression float r = red(myColor); h<strong>and</strong>led as a bitwise operation:<br />

float r = myColor >> 16 & 0xFF;<br />

I know this expression must look odd (<strong>and</strong> maybe even scary) to many of you—it totally<br />

did to me. There are two distinct processes occurring on the integer in this little line of<br />

code. The first process involves the bits of the integer being shifted 16 places to the right<br />

(using >> 16), <strong>and</strong> after that, a bitwise AND operation (using & 0xFF) is performed, yielding<br />

the isolated red value. I’ll look at each process separately.<br />

Shifting bits<br />

Shifting bits 16 places to the right literally means moving all the bits over 16 places. There<br />

are two different bitwise shift operators in <strong>Processing</strong>: >> <strong>and</strong> ) shifts bits<br />

to the right <strong>and</strong> the second (

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!