Page 1 of 1
Reading Individual variable bits.
Posted: Sun Apr 12, 2020 1:33 pm
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.
Re: Reading Individual variable bits.
Posted: Sun Apr 12, 2020 1:59 pm
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:
If you want to see if bit 2 is Set:
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?
Re: Reading Individual variable bits.
Posted: Sun Apr 12, 2020 2:34 pm
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.
Re: Reading Individual variable bits.
Posted: Sun Apr 12, 2020 2:53 pm
by medelec35
You're welcome.
Within the decision macro, Just replace the the name
with the actual variable you are interested in.
Also replace the
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:
or
You need to remember for 8 bits, lowest bit is 0 and highest bit is 7
If you get stuck just upload your flowchart.
Re: Reading Individual variable bits.
Posted: Sun Apr 12, 2020 3:59 pm
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.