Reading individual bits

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
Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Reading individual bits

Post by Dave S »

Hi all,

Is there any way to read individual bits from a Flowcode variable without resorting to C code please?

Something like a decision block with: "If Variable,3 = 1" with yes or no outputs.

I can put : "If variable = 23" and it works fine but I need to test each of the 8 bits and act on the value of each one.

You can read in individual bits from ports.............

Thanks,

Dave.

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

Re: Reading individual bits

Post by medelec35 »

Hi Dave,
If reading from a variable e.g variable is = 23
I believe you can test each individual bit by the following method:
Bit testing of a byte.jpg
Bit testing of a byte.jpg (52.4 KiB) Viewed 6103 times
So if you wanted to test bit 3 use:
(Variable & (1<<3))
Etc.

Hope this Helps

Martin
Martin

Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Re: Reading individual bits

Post by Dave S »

Thanks Martin that's very useful. I'll try it out first thing in the morning.

These statements appear to be derived from C? How do you find out what you can put in a decision box and what has to go in a C box?

I find the Flowcode help files very limited. Is there a document somewhere that details all of the syntax etc. for each function?

Thanks again for you help.

Dave.

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

Re: Reading individual bits

Post by medelec35 »

Hi Dave.
(Variable & (1<<3)) may be a command that's found in C programs, but we are not not using any C boxes as such.
This type of calculation can be also used in the normal decision branches and non C calculation boxes.
Since this is the case it should work with the Flowcode simulator.
To be honest I'm not an expert with these type of commands but the way It works is ANDing with the bit you want to compare with and masking out all the other bits

Bit 3 = 2 to the power of 3 = 8
(2y3)

so its ANDing 23 with 8

Code: Select all

        16  8  4  2  1  
23 =    1   0  1  1  1
Note: the << just means shift bits to the left = double the previous value. Likewise >> is shift bits to the right = half the previous value.
1<<1 = 1y2 = 2
1<<2 = 2y2 = 4
1<<3 = 3y2 = 8
1<<4 = 4y2 = 16
1<<5 = 5y2 = 32 etc.

You will only get a 1 if bit 3 is 1 since all other bits are masked off.
As you can See, Bit 3 is 0, so the result is 0 so bit 3 of 23 is off

Take a look at this great article on Simple Masking Technique:
http://www.matrixmultimedia.com/article.php?a=366
Which is where I remembered the information from.
These articles do come in very handy for learning with Flowcode.

Martin
Martin

Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Re: Reading individual bits

Post by Dave S »

Thanks Martin,

There seems to be a lot of information on line. Problem is in finding it!

Dave.

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

Re: Reading individual bits

Post by medelec35 »

Dave S wrote:Thanks Martin,

There seems to be a lot of information on line. Problem is in finding it!

Dave.
I couldn't agree more, for that reason I posted this:
http://www.matrixmultimedia.com/mmforum ... 451#p26400
Which is asking for more information on different types of bit/byte manipulation to be added to that article (with examples given)

Perhaps even a contents or index like a book will help?

Martin
Martin

Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Re: Reading individual bits

Post by Dave S »

medelec35 wrote:
Dave S wrote:Thanks Martin,

There seems to be a lot of information on line. Problem is in finding it!

Dave.
I couldn't agree more, for that reason I posted this:
http://www.matrixmultimedia.com/mmforum ... 451#p26400
Which is asking for more information on different types of bit/byte manipulation to be added to that article (with examples given)

Perhaps even a contents or index like a book will help?

Martin
I've been using Flowcode for about a month now after previously using asm. Flowcode is an amazing program let down by its lack of documentation. I have learned a lot in a month, mainly from ploughing through examples and asking questions on here to helpful people like yourself. Why can't we just have a manual?
Another example, I've just bought a Flowkit. It came with a nice glossy booklet telling me about Eblocks in many different languages. You have to search on the web for instructions for Flowkit.

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

Re: Reading individual bits

Post by medelec35 »

Dave S wrote: Another example, I've just bought a Flowkit. It came with a nice glossy booklet telling me about Eblocks in many different languages. You have to search on the web for instructions for Flowkit.
Have you looked here?:
http://www.matrixmultimedia.com/mmforum ... =26&t=7138

One thing that’s got better is the tutorials.
Have a look in the leaning centre:
http://www.matrixmultimedia.com/lc_index.php

Martin.
Martin

Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Re: Reading individual bits

Post by Dave S »

I saw the flowkit document thanks. By tutorials do you mean the video's?

The "in circuit debugging" one just shows the simulator :roll:

Dave

Post Reply