Usage of Logical AND Bitwise AND

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Usage of Logical AND Bitwise AND

Post by Jay Dee »

Hi all,
As soon as I start to play with logical and bitwise operators my brain fries and I would like to make sure I dont pick up bad habbits!
I would like to be able to get the logical state ( 0 or 1 ) of a specific bit within a byte, is this the best way;

Result = (Byte AND Mask)&&1

Am I correct in the following assumptions,
Bitwise AND can be implemented in a calculation component using either AND or &
Logical AND is implemented only by using &&

Also I wanted to ensure that I avoided any divide by zero situations, is there a simple technique to use?
Thanks,
John.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Usage of Logical AND Bitwise AND

Post by JonnyW »

Hello. I would not do it this way - logical operators may implement branches or logic that is unnecessary for this specific use, or may result in odd behaviour when the code is optimised.

For bitwise operations you could do any of the following to achieve the same result:

Code: Select all

Result = (Byte >> Shift) AND 1          // Where Mask = (1 << Shift)
Result = (Byte AND Mask) >> Shift       // Where Mask = (1 << Shift)
Result = !!(Byte AND Mask)
Result = ((Byte AND Mask) != 0)
I would recommend the first line if you know the specific bit you're after, or the last line if you don't.

You are correct in your assumptions. I think in v4 either of the &&, & operators can be used wherever the other is - this is certainly true in v5.

For divide by zero, it depends what you are doing and to your results as to how to avoid divide by zero. A simple technique is to do:

Code: Select all

  result = numerator / (denominator + (denominator == 0))
Which will add 1 to the denominator if it is zero, but this may not be what you want. Usually it is better for clarity's sake to perform a specific check with a decision icon. This way you can handle the div-zero error much better.

Cheers,

Jonny

Post Reply