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.

This sketch outputs the following:<br />

c1 = 2147483392<br />

binary(c1) = 1111111111111111111111100000000<br />

show 32 bits = 01111111111111111111111100000000<br />

binary(c1>>16) = 111111111111111<br />

show 32 bits = 00000000000000000111111111111111<br />

Notice that I explicitly added zeros to the left of two of the binary outputs (labeled show<br />

32 bits) just to reveal the actual 32 bits of data. Again, the zeros on the left of the number<br />

don’t change the number’s value (<strong>and</strong> thus normally aren’t output when you call<br />

println()), but seeing them does make it easier to visually compare the different binary<br />

bit strings. The color I began with, c1, had an alpha of 127, giving the color integer a<br />

positive value (2147483392) since the signed bit was a zero. Thus, when I shifted all the bits<br />

16 places to the right, the 16 places emptied on the left side of the number were filled<br />

with zeros—again following the rule that the signed bit controls what these values<br />

should be.<br />

Bitwise operators<br />

Please note, when shifting to the left, places emptied on the<br />

right (due to the shifting) are always filled with zeros.<br />

In addition to bit shifting, the original expression float r = myColor >> 16 & 0xFF; contains<br />

a bitwise operator (&) as well as what’s referred to as a bit mask (0xFF).<br />

Bitwise operators are similar to other operators you know about (+, *, -, etc.), only bitwise<br />

operators work directly on binary numbers. There are four bitwise logical operators in<br />

Java, two of which are included in <strong>Processing</strong>. However, as <strong>Processing</strong> really is Java, you can<br />

use all four in <strong>Processing</strong>, even though the <strong>Processing</strong> language reference only covers two<br />

of them. As I mentioned, these operators work on binary values (directly with the bits). So<br />

if I write 4 | 6, using the bitwise OR operator (which I’ll explain shortly), the numbers 4<br />

<strong>and</strong> 6 will be evaluated as their binary equivalents (100 <strong>and</strong> 110, respectively). This is great<br />

for the computer, which loves (assuming it could actually feel) dealing at the bit level, but<br />

kind of lousy for us squishy bio-forms, who are not used to doing fast base 2 calculations.<br />

It helps to see the bitwise operators in action to get a better sense of how they work.<br />

The four logical bitwise operators are &, |, ^, <strong>and</strong> ~.<br />

& is the bitwise AND operator; it works by evaluating the parallel bits of two binary numbers.<br />

If the two parallel bits are both 1, then the result for that place is a 1. All other results<br />

evaluate out to 0 for that bit position.<br />

The following example compares the parallel bits of the binary values of 4 <strong>and</strong> 6 (which<br />

are listed next) using the bitwise AND operator:<br />

MATH REFERENCE<br />

767<br />

B

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

Saved successfully!

Ooh no, something went wrong!