We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Please explain "fill(239|i&16);"
Page Index Toggle Pages: 1
Please explain "fill(239|i&16);" (Read 962 times)
Please explain "fill(239|i&16);"
Dec 10th, 2009, 2:27pm
 
Hello all,

I am studying a wonderfuly written Sketch, by dotlassie, called HexEdit openprocessing dot org/visuals/?visualID=3613. I find his/her code facinating.

I am trying to work out how fill(239|i&16); works in the code below.

I understand that i is being incremented by 16 as the script iterates, but I have never seen the syntax before.

Thank you.

int i,y,x,h=16;
void draw()
{
 textFont(createFont("",h));
 x=i%h*h;
 if(x<1)
 {
   noStroke();
   fill(239|i&16);
   rect(90,y+2,999,h);
   fill(0);
   text(hex(i),0,y=i+h);
 }
 text(hex(i,2),99+x*2,y);
 text(char(i),620+x,y);
 i++;
}
Re: Please explain "fill(239|i&16);"
Reply #1 - Dec 10th, 2009, 3:06pm
 
Looks like '&' is a bitwise operator.  Arcane; but usually quicker than the more human-readable method...

Actually '|' (pipe) is too.

So that would read as something like (239 OR (i AND 16)) where (i AND 16) presumably gives you the increment by 16...
Re: Please explain "fill(239|i&16);"
Reply #2 - Dec 10th, 2009, 3:19pm
 
Hi Blindfish,

Thanks for your feedback. I am aware of | being a pipe, but never seen it used as OR in a language, usually its ||. I never thought of & being a bitwise operator either. Very intersting.

dotlassie likes to keep their code as short and sweet as possible so presumably that is why tat method seems 'arcane'.

Thanks for your help.
Re: Please explain "fill(239|i&16);"
Reply #3 - Dec 10th, 2009, 3:31pm
 
Arcane, because they're doing things at a much lower level than I can get my head round; but that often also results in better efficiency.  If I've understood correctly OR has a slightly different meaning with bitwise operation than simply "do this or that" - presumably why it has a slightly different syntax.  IIRC it's comparing the value at binary level and if either of the two inputs is a 1 it returns a 1.  With an AND both have to be identical to return a 1.  I'd have to look it up to be sure.  I know these operations are particularly useful/quick when handling colour values...  but you might have gathered that I haven't used it much.
Re: Please explain "fill(239|i&16);"
Reply #4 - Dec 10th, 2009, 11:52pm
 
yes, bitwise operations

239 =
11101111

i & 16 =
00000000 if i is between 0 and 16
00010000 if i is between 17 and 32
00000000 if i is between 33 and 64
etc

and
11101111 | 00010000 = 11111111
11101111 | 00000000 = 11101111

so it's flipping the colour between 239 (light grey) and 255 (white) once every 16 iterations.

don't they teach this in schools anymore? 8)
Re: Please explain "fill(239|i&16);"
Reply #5 - Dec 11th, 2009, 3:55am
 
koogy wrote on Dec 10th, 2009, 11:52pm:
don't they teach this in schools anymore 8)


Schools as in universities  I'm sure they do if you're doing a Computer Science related degree, but not all of us have had the pleasure...  I know I should learn about this stuff at some point, but I keep putting it off.  It scares me, just like regular expressions   :o

I remember learning the basics of binary numbers at secondary school, but definitely not bitwise operations...
Re: Please explain "fill(239|i&16);"
Reply #6 - Dec 11th, 2009, 4:44am
 
yeah, i was being facetious and showing my age 8)

but also curious.

(yeah, we did binary as a maths concept when i was about 10 but the application of it for this stuff was a few years later after sinclair had popularised computers here in england (from '81) and i was writing z80 for fun and generally reading everything i could find. and later doing C code and noticing all those #defines for 0x1, 0x2, 0x4, 0x8 etc (using bits as flags)

kids these days know stuff at a much higher level and generally program rings around me. but if they want binary numbers (or regular expressions, or vi keystrokes) i am still useful)

so, anyway, the (239|i&16) is perfectly cromulent, and much more concise than the equivalent condition, but it's crying out for an explanatory comment.
Re: Please explain "fill(239|i&16);"
Reply #7 - Dec 11th, 2009, 5:23am
 
Sadly I'm not as young as I would like to be (though they had at least progressed to the ZX Spectrum 48K by around the time I hit 12 Tongue ), but I didn't get into programming until relatively recently.

I can just about handle the odd regex, but like bitwise operators that's not always easily readable by untrained humans.  And looking at the context of the original code they were clearly going for brevity rather than legibility...
Page Index Toggle Pages: 1