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.

Here’s the output:<br />

c1 in binary = 10000000111111111111111111111111<br />

c1 in decimal = -2130706433<br />

----------------------------------------------------------------------c2<br />

in binary = 1111111111111111111111111111111<br />

c2 in decimal = 2147483647<br />

Look at the output closely. Notice that c1’s binary bit string (32 digits) is actually 1 digit<br />

longer than c2’s (31 digits). (If you don’t feel like counting all the ones, just take my word<br />

for it.) Also, <strong>and</strong> more bewildering, c1’s decimal value is negative while c2’s is positive.<br />

Why?<br />

Both c1 <strong>and</strong> c2 have identical RGB values. However, their alpha values are 128 <strong>and</strong> 127,<br />

respectively. So all the variations between their binary bit strings <strong>and</strong> decimal values are<br />

somehow caused by this slight variation (probably not even visibly noticeable) in their<br />

alpha values. Why again? The long answer is not simple, so I’ll give a “good enough”<br />

answer (<strong>and</strong> of course provide a link to a more detailed explanation).<br />

<strong>Processing</strong>’s (<strong>and</strong> Java’s) 32-bit integer type needs to account for positive <strong>and</strong> negative values<br />

(<strong>and</strong> 0). There are various ways of coding these values as a binary bit string. The most<br />

efficient <strong>and</strong> common approach is using what’s referred to as a two’s complement system.<br />

The very basic way the two’s complement system works is as follows: to create a positive<br />

number, you simply encode the value using st<strong>and</strong>ard binary conversion, as you looked at<br />

earlier. So for example, the number 8 in decimal becomes 2 3 , or 1000 in binary. To create<br />

a negative number, you need to first create a positive binary equivalent of the number,<br />

<strong>and</strong> then invert all the bits (zeros become ones <strong>and</strong> ones become zeros), <strong>and</strong> then add 1.<br />

So to express –8 in a two’s complement system, first you need to specify 8 as a positive<br />

binary number, which you know is 1000. However, 1000 is not the full 32-bit representation<br />

of 8, as <strong>Processing</strong> discarded the 28 zeros to the left of the number. Normally this<br />

would be fine, as these zeros on the left side of the number don’t change its actual value.<br />

But to underst<strong>and</strong> the two’s complement system, you need to account for these bits.<br />

Here’s the full 32-bit binary representation of 8 (with the 28 zeros added on):<br />

00000000000000000000000000001000<br />

So to calculate –8, first you invert all the bits:<br />

11111111111111111111111111110111<br />

And then you add 1:<br />

11111111111111111111111111111000<br />

Simple, right? OK, I know this stuff is pretty confusing. If the two’s complement stuff isn’t<br />

doing it for you, just remember one final rule about it: if the leftmost digit of the entire bit<br />

string (referred to as the signed bit ) is a zero, the value will be positive, <strong>and</strong> if the signed<br />

bit is a one, the number will be negative. To confirm this, try looking at the binary equivalent<br />

of some positive <strong>and</strong> negative numbers in <strong>Processing</strong> (or just take my word for it).<br />

If this exciting two’s complement discussion really did it for you, check out http://<br />

en.wikipedia.org/wiki/Two's_complement.<br />

MATH REFERENCE<br />

765<br />

B

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

Saved successfully!

Ooh no, something went wrong!