calculation tricks

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

calculation tricks

Post by brandonb »

by putting this in tips tricks section was hoping to learn better coding myself and make it easier to find for others in future if others will add to these

Code: Select all

 variable=variable|1<<7//set bit 7 in variable 

Code: Select all

 variable=variable&~(1<<7)//clear bit 7 in variable 

Code: Select all

 variable=variable^1<<7//toggle bit 7 in variable 

Code: Select all

 output=!output // toggles boolean variable

Code: Select all

 if(  (a>b&&b<c)||(d!=c&&e<5)  ) //if left statment true or right statment true condition is true

Code: Select all

 if(  a  ) //if the a  variable is above 0 the if statement is true, not limited by boolean

Code: Select all

 a=~a//flips all bits,if a=10100000 a would then = 01011111 

Code: Select all

**************32BIT TO 4 BYTES / 4 BYTES TO 32 BIT ******************
(recall_4 = msB  recall_1 = lsB)   (d = msB  a = lsB)

if shifting bytes into a 32 bit number this is the minimum data types
recall_1 is a byte
recall_2 is a byte
recall_3 is a unsigned int
recall_4 is a unsigned int
--> being paranoid recall_4 i use ulong 

input_number = 65535

a = input_number & 0xff // byte
b = (input_number >> 8) & 0xff // byte
c = (input_number >> 16) & 0xff // byte
d = (input_number >> 24) & 0xff // byte

output_number = recall_1 + (recall_2 << 8) + (recall_3 << 16) + (recall_4 << 24)
MINIMUM DATA TYPES TO BIT SHIFT 32 BIT NUMBERS.fcf
(15.5 KiB) Downloaded 398 times
Last edited by brandonb on Wed May 22, 2013 3:19 am, edited 4 times in total.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: calculation tricks

Post by medelec35 »

Thanks for your very useful post Brandon.

It's much better all the calculations being in one place, rather than hunting around for them.


Martin
Martin

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: calculation tricks

Post by kersing »

Brandon,

I'm afraid I noticed a couple of issues:

Code: Select all

    if(  a  ) //if the a  variable is above 0 the if statement is true, not limited by boolean
This is true for any 'a' that is unequal to 0. So for signed data types (integers) it is true for negative numbers as well as positive numbers.

Code: Select all

    if(  ((a&&b&&c&&d)>10)  )//if all variables greater than 10 if statement true 
What you are doing is using the logical and operator on a, b, c and d. The logical and operator is true if both left and right operand are not false. For numbers in flowcode (and C) this means the number is not equal to zero. So your expression is (a&&b&&c&&d) > 10, this means if all 4 variables are unequal to zero the left side of the expression evaluates to true. Depending on the representation of true (is it -1, 1, 255 or any other value) the result of the total expression may be true or false. Basically, the result of the expression may be true if all of the variables are non zero or never be true (depending on the C compiler used)

Best regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: calculation tricks

Post by brandonb »

I'm afraid I noticed a couple of issues:
thanks kersing, i see what you mean on the (a&&b&&c)>10, it doesnt work the way i thought, when i tested it to see if my understanding was correct on it, i must have tested it in a way that just happened to give me what i thought could be interperted as this statement

Code: Select all

if(  (a>10)&&(b>10)&&(c>10)   )
yep, two different things, thanks
please feel free to add and correct

Post Reply