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.

I’m not going to provide examples for the other two operators, but I’ll tell you about them.<br />

^ is the XOR, or exclusive OR, operator. It works by comparing parallel bits, as with the first<br />

two operators. However, XOR is looking for differences. If the parallel bits are different,<br />

then the result is a one; otherwise the result is a zero.<br />

~ is the bitwise complement <strong>and</strong> is a unary operator, meaning it works like a binary minus<br />

sign on a single oper<strong>and</strong>, inverting zeros to ones <strong>and</strong> vice versa.<br />

Putting it all together<br />

Finally returning to the original bitwise color expression, float r = myColor >> 16 &<br />

0xFF;, you should now have some sense of what is happening in the expression. The last<br />

mystery to solve is the bit mask 0xFF. A bit mask is just a pattern of bits used to perform<br />

some type of operation on another binary value, using one of the bitwise operators you<br />

just looked at. The bit mask value itself (0xFF) hopefully looks familiar to you, as it’s just a<br />

number expressed in hexadecimal notation (base 16). Remember, colors are commonly<br />

represented as hexadecimal values. The 0x part of 0xFF tells the compiler that the characters<br />

FF should be evaluated in base 16. Since F is the highest value in base 16 (equal to 15<br />

in decimal), FF evaluates to 15 * 16 1 + 15 * 16 0 = 255, which you’ll remember is the same<br />

thing as 11111111 (in base 2).<br />

For those of you who actually like this bit-mathy stuff, break the binary value<br />

11111111 into two even groups of four bits <strong>and</strong> then evaluate each separately<br />

in base 2. Compare the results to the hexadecimal value 0xFF. Do you<br />

see the relationship between the binary <strong>and</strong> hexadecimal values?<br />

Finishing up this long discussion, you now have all the ingredients to underst<strong>and</strong> the bitwise<br />

color expression myColor >> 16 & 0xFF. Next is a description of how the expression<br />

actually returns the red component of a color.<br />

I’ll begin by creating a new color:<br />

color myColor = color(200, 190, 75, 255);<br />

This color looks like this in binary (please note that I divided the continuous 32-bit string<br />

into byte-sized components, for readability):<br />

11111111 11001000 10111110 01001011<br />

Performing the 16-bit right shift on the number yields the following:<br />

11111111111111111111111111001000<br />

Next I’ll use the bitwise AND operator <strong>and</strong> bit mask 0xFF (11111111 in binary). Remember,<br />

the & operator works by comparing the parallel bits. If the bits are both 1, then the result<br />

for that place is a one. All other results evaluate out to zero.<br />

MATH REFERENCE<br />

769<br />

B

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

Saved successfully!

Ooh no, something went wrong!