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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

760<br />

Figure B-6. Sine Console sketch<br />

Bitwise Operations<br />

If you look at what the <strong>Processing</strong> language reference has to say about the red() function,<br />

there’s a mysterious blurb under the description section:<br />

“The red() function is easy to use <strong>and</strong> underst<strong>and</strong>, but is slower than another technique.<br />

To achieve the same results when working in colorMode(RGB, 255), but with greater<br />

speed, use the >> (right shift) operator with a bit mask.”<br />

The terms right shift operator <strong>and</strong> bit mask can be generally grouped under the heading<br />

of bitwise operations. I briefly mentioned bitwise operations in Chapter 10 <strong>and</strong> in<br />

Appendix A, but next I’ll provide a detailed account of how they work <strong>and</strong> why you might<br />

want to learn them.<br />

A word of caution: Bitwise operations can be daunting for beginners (actually even<br />

experienced coders) <strong>and</strong> are an advanced topic. Though they can be difficult to get<br />

your head around, bitwise operators are also powerful <strong>and</strong> highly efficient processing<br />

tools <strong>and</strong> certainly worth (eventually) learning about.<br />

As part of my research for the book, I did a web search on bitwise operations, <strong>and</strong> was surprised<br />

at how poorly they were explained—including in my trusted Wikipedia. There are<br />

plenty of bitwise operation examples, but I had trouble finding a really clear elucidation—<br />

especially for new programmers. Thus, I have taken this challenge on, as a personal mission,<br />

to demystify the mass confusion <strong>and</strong> escalating anxiety over bitwise operations (at<br />

least in their relation to <strong>Processing</strong> <strong>and</strong> color).<br />

First of all, what the heck are bitwise operations, <strong>and</strong> why are they in <strong>Processing</strong> in the first<br />

place, especially if they’re not easy to use? Bitwise operations are super-low-level mathematical<br />

operations, down at the binary or bit level. Rather than dividing 4 by 2 like 4/2, I<br />

could get the same answer (2) using a bitwise operation, which would look like 4>>1, or<br />

even 4^6. Yikes, why would I want to do that?! Well, most of the time I wouldn’t. However,<br />

there are some good reasons to break out bitwise operations (as well as some really geeky<br />

low-level reasons that most of us <strong>Processing</strong> types are not gonna want/need to do). Color

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

Saved successfully!

Ooh no, something went wrong!