Trying to understand how Boolean 'and' & return work?

edited January 2017 in Questions about Code

Namaste all, I was trying something when I came across this code on the net:

final static color BG = #4080A0, FG = #FFFF00;
final static short BOLD = 3, FPS = 30;

void setup() {
  size(600, 400);
  frameRate(FPS);
  noLoop();
  smooth();

  background(BG);
  stroke(FG);
  strokeWeight(BOLD);
}

void draw() {
  println(mouseX + "\t" + mouseY);

  if ( (frameCount & 1) == 0 ) {
    background(BG);
    point(mouseX, mouseY);
    return;
  } 

  line(pmouseX, pmouseY, mouseX, mouseY);
  println();
}

void mouseClicked() {
  redraw();
}

The questions I have are: 1)how is the if statement working here.As far as I understand if the framecount and 1 becomes equal to 0 the code in the block will work, but 1 can never be equal to 0?

2) why was return used here? what does it actually do?

Thanks a lot.

Answers

  • edited January 2017 Answer ✓
    1. The expression (frameCount & 1) == 0 checks out whether frameCount is an even value.
      Read about bitwise operator & here: https://Processing.org/reference/bitwiseAND.html
    2. Keyword return abruptly quits any function: https://Processing.org/reference/return.html
      The effect there is to impede line() to be run when frameCount is even. >-)
  • Answer ✓

    1) The important thing to realize here is that there is a difference between & and &&. The first, &, is a binary and. The second, &&, is a logical and.

    Doing a binary and means that you are comparing the binary representations of things. Since framecount and 1 are both numbers, they are stored as binary. 1 is a bunch of 0's followed by a single 1 (00000...00001). If you binary and with that, you'll get all 0's except maybe for the last binary digit. That last digit will only be a 1 if there is a 1 in the last digit of framecount, or a 0 otherwise. If you get all 0's, then that does equal zero.

    In short, the check is the same as this: if( framecount %2 == 0 )

    It's checking to see if framecount is odd or even.

    2) The return keyword causes the function it's in to exit immediately, returning a value if one is specified.


    This code is old. And odd. It's essentially trying to use the framecount variable to track state, depending on if it is odd or even. Here's a more modern style version:

    final static color BG = #4080A0, FG = #FFFF00;
    final static short BOLD = 3;
    
    boolean show_line = true;
    int sx = -10;
    int sy = -10;
    int ex = -10;
    int ey = -10;
    
    void setup() {
      size(600, 400);
      smooth();
      stroke(FG);
      strokeWeight(BOLD);
    }
    
    void draw() {
      background(BG);
      if(show_line){
        line(sx,sy,ex,ey);
      } else {
        point(sx,sy);
      }
    }
    
    void mousePressed() {
      if (show_line) {
        sx = mouseX;
        sy = mouseY;
      } else {
        ex = mouseX;
        ey = mouseY;
      }
      show_line = !show_line;
    }
    
  • edited January 2017 Answer ✓

    This code is old. And odd.

    Actually I'm its author! [-( Indeed very old. Below its original forum threads: :bz

    1. https://forum.Processing.org/one/topic/drawing-lines-with-user-defined-coordinates.html
    2. https://forum.Processing.org/one/topic/mouse-click.html

    And watch it running online at the link below: O:-)
    http://studio.SketchPad.cc/sp/pad/view/ro.9c9DCUznTwot6/latest

    essentially trying to use the frameCount variable to track state, depending on if it is odd or even.

    Actually it tries to rely on as many Processing's variables as possible, rather than declaring its own.
    Besides frameCount, there's also mouseX & mouseY and pmouseX & pmouseY coordinate pairs. *-:)

    If you pay attention, apart from the global constants, not a single variable was declared at all! \m/

  • Thanks for the help guys!!. also was there any particular reason why the binary and was used in place of if( framecount %2 == 0 ). I mean it dosen't come naturally to think in that way. How do one decide when to use bitwise operators(I read about them after going through your comments)?

  • Answer ✓

    Bitwise operations are the fastest a CPU can do. \m/
    Although the diff. between the modulo (division remainder) and the AND & operator is almost nothing, in a long tight loop, it can be gauged. :P

  • Answer ✓

    I mean it dosen't come naturally to think in that way.

    This depends. If you grew up writing 8 bit assembly level programming where bitwise operators are available but division and modulo aren't then this is perfectly natural. Especially when the modulo is a power of two.

    You see bitwise operators quite often when people are passing flags around. You can store as many flags as there are bits in a long and check and set individual flags with bitwise operators rather than defining an array of booleans.

  • edited January 2017 Answer ✓

    Yeah well, if you'd like to take so much pain to do that in something that has graphics, which won't even improve performance by a visible amount inside loops that check like a million times (try it if you don't believe me, most CPUs have super fast units for division also), then you're most welcome.

    But, when coming to the code being discussed, yes it is very strange unless it was some sort of assignment. And we aren't talking about some slow, old, 8-bit computer with 256 bytes of RAM. Even today's microcontrollers like Arduino UNO (actually should be saying Atmega 328P) have fairly fast division, and it won't make a considerable impact within a few thousand iterations.

    So yes, in today's world, where you have immense calculating power fitting in a package smaller than your hand, it won't come naturally.

    And also understand that gauged as GoToLoop says is (slightly)different from felt, which is what matters (most of the time).

  • edited January 2017

    The method on the left is incorrect because it returns true if even also you can reduce it to a single statement

    public boolean isOdd (int value) {
      return value % 2 != 0;
    } 
    
  • edited January 2017

    Yes, exactly. Rewriting the code on the right in the same way, you have this-

    public boolean isOdd(int value){
      if((value & 1) == 1){
        return true;
      }else{
        return false;
      }
    }
    
  • edited January 2017

    we doing dank memes now?

  • Ha ha, hilarious.
    Stop flooding the OP's post with irrelevant memes.
    GoToLoop posted a relevant one. Keep yours for someplace where it might make more sense.
    This is entirely a point of personal choice.

  • Meh, the question has been answered, and threads should be allowed to derail every now and then.

Sign In or Register to comment.