Reading Individual variable bits.

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
lungchard
Flowcode v5 User
Posts: 19
Joined: Wed Aug 14, 2013 3:53 pm
Location: Thailand
Has thanked: 1 time
Been thanked: 3 times
Contact:

Reading Individual variable bits.

Post by lungchard »

Hello Forum Members,

I am trying to edit some code published by Medelc35 and am having difficulty in working out what to do.
Bear with me as my programming knowledge is very limited.
I want to test 4 individual bits in a variable and carry out some action based on their individual values.
I have tried the decision block as found on another forum page but still without success.
If anyone can help me resolve how to do this I would be most greatfull.
Thanks in advance.

Richard Hogan.
Attachments
decision.png
(7 KiB) Downloaded 394 times

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: Reading Individual variable bits.

Post by medelec35 »

Hi Richard,
This is what I do to test for bits:

Code: Select all

(Variable & (1 << bit)) <> 0 // to Check if bit of variable is set
or

Code: Select all

(Variable & (1 << bit)) = 0 // to Check if bit of variable is Clear
So if you want to see if bit 2 is Clear:

Code: Select all

If (Variable & (1 << 2)) = 0 
If you want to see if bit 2 is Set:

Code: Select all

If (Variable & (1 << 2)) <> 0 
you can use & 4 instead but its easier doing the above as you dont need to work out bit 0 is 1, bit 1 is 2, bit 2 is 4, bit 3 is 8 etc

Hope that helps?
Martin

lungchard
Flowcode v5 User
Posts: 19
Joined: Wed Aug 14, 2013 3:53 pm
Location: Thailand
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: Reading Individual variable bits.

Post by lungchard »

Hello Martin,

Tnx for the reply, especially on a Sunday!
Do i just enter your suggested text into the decision block or is there more to it?


Best Regards

Richard Hogan.

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: Reading Individual variable bits.

Post by medelec35 »

You're welcome.
Within the decision macro, Just replace the the name

Code: Select all

Variable 
with the actual variable you are interested in.
Also replace the

Code: Select all

bit
with a number that represents the bit you are interested in.
For example if you want to test bit 4 of a variable called temp3 you either use:

Code: Select all

(temp3 & (1 << 4)) <> 0
or

Code: Select all

temp3 & 16
You need to remember for 8 bits, lowest bit is 0 and highest bit is 7
If you get stuck just upload your flowchart.
Martin

lungchard
Flowcode v5 User
Posts: 19
Joined: Wed Aug 14, 2013 3:53 pm
Location: Thailand
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: Reading Individual variable bits.

Post by lungchard »

Hello Martin,

Just the ticket!
Does what i need but rest of my code needs some minor tweeks to get the displayed temperature to show correct value when the 4 digits have values of 0001. This corresponds to a temperature of 0.0625 but LCD currently shows 0.625.
Need to figure out how to get a leading zero on this value only.
All other combinations of the bits result in a 4 digit value that displays correctly.
May have to force a 0 to the LCD if only bit 0 has a value of 1.
Same applies to bit values of 0000. This shows 0.0 on the LCD. I would prefer to see 0.0000 but that is for another day.

Many thanks again for your help.

Best Regards from Thailand,

Richard Hogan.

Post Reply