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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

766<br />

Let’s now return to the initial expression listed in the <strong>Processing</strong> reference that sparked<br />

this discussion:<br />

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

You’ll remember that this expression is a way of getting just the red component of the<br />

color <strong>and</strong> a faster alternative to <strong>Processing</strong>’s red() function. Let’s assume myColor is<br />

#FF00FF (purple) <strong>and</strong> 100 percent alpha (which you’ll remember is the default). The bit<br />

string of this color integer is as follows:<br />

11111111 11111111 00000000 11111111<br />

(alpha) (red) (green) (blue)<br />

Again, I divided the 32-bit string into 4 bytes, just to show how the packed integer represents<br />

the different color components. As I specified, the four 8-bit groups, from left to<br />

right, represent the alpha, red, green, <strong>and</strong> blue components, respectively. Remembering<br />

what I discussed a few paragraphs back, can you guess whether this integer would evaluate<br />

to a positive or negative value? The answer is negative, since the signed (leftmost) bit<br />

is a one. To test this, run println(#FF00FF );, which should output -65281. This specific<br />

value is not terribly useful. (I remember being pretty confused the first time I ran a<br />

println() on one of my colors <strong>and</strong> it came up with this huge negative value.) What is<br />

worth remembering is that the sign of the number is based on the leftmost bit (1 is negative<br />

<strong>and</strong> 0 is positive).<br />

Next, let’s shift the bits of the original purple color 16 places to the right. Here is the original<br />

bit string:<br />

11111111 11111111 00000000 11111111<br />

Shifting the bits 16 places to the right, it becomes the following:<br />

11111111 11111111 11111111 11111111<br />

The 16 bits on the left side of the number move to the right 16 places. The original 16 bits<br />

on the right side move over as well, but since there are no places to their right, they are<br />

discarded. You may be wondering then why the original 16 places on the left, after the<br />

shifting takes place, remain ones instead of becoming zeros. This is a good question, which<br />

originally confused me as well. The reason for this relates to the signed bit (the leftmost<br />

bit), which you’ll remember was a one in the original purple color. When bits are shifted to<br />

the right in a two’s complement system, any empty positions on the left side, where the<br />

bits were shifted away from, as in this example, are filled with the value of the signed bit.<br />

That’s why in the last example, the left 16 bits were replaced with ones instead of zeros.<br />

Here’s one more example in <strong>Processing</strong>:<br />

color c1 = color(255, 255, 0, 127);<br />

println("c1 = " + c1);<br />

println("binary(c1) = " + binary(c1));<br />

println("show 32 bits = 0" + binary(c1));<br />

println("binary(c1>>16) = " + binary(c1>>16));<br />

println("show 32 bits = 00000000000000000" + binary(c1>>16));

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

Saved successfully!

Ooh no, something went wrong!