Decimal ti binary conversion.......

For general Flowcode discussion that does not belong in the other sections.
Post Reply
solozerouno
Posts: 113
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 4:36 am
Has thanked: 2 times
Been thanked: 2 times

Decimal ti binary conversion.......

Post by solozerouno »

if I receive a given byte from a component in decimal and load it in a variable and I want to have it in binary so as to have a bit mask to compare with a setting that activates a component, how do I do?
Thanks Solozerouno

Steve-Matrix
Matrix Staff
Posts: 1249
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Decimal ti binary conversion.......

Post by Steve-Matrix »

You shouldn't need to convert the variable's format. That is only used to display the value to the (human) user.

If you have a byte variable "var" and want to see if a particular set of bits are set, then you can use bitwise operators like AND (&) and OR (|). For example, "var AND 0b0010" will be true if the second bit of "var" is set. You can use expressions like this within a decision or loop icon, or to set another variable.

solozerouno
Posts: 113
Joined: Tue Dec 08, 2020 4:36 am
Has thanked: 2 times
Been thanked: 2 times

Re: Decimal ti binary conversion.......

Post by solozerouno »

But is it possible to calculate an xor on a byte of 201 and a byte of 189 in decimal and then translate the result on binary and then for example turn on the led of the 8 bit port one by one ?
There is a function to convert a binary byte from an 8-bit gate use a logic function always in binary to create a 'mask' and then on my display I write its number in decimal ...... Maybe so it is more easy to understand .......
Thanks a lot Solozerouno.

Steve-Matrix
Matrix Staff
Posts: 1249
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Decimal ti binary conversion.......

Post by Steve-Matrix »

Result = var1 XOR var2. This translates as 201 XOR 189 = 0xC9 XOR 0xBD = 0b01110100 (I think).

You can then write the whole "result" variable to the port. Or if you wanted to do something more specific on individual bits, you would do something like "if (result AND 0b00100000)" in a decision.

In Flowcode you can write integer values in decimal, hexadecimal or binary (201 == 0xC9 == 0b11001001). And you can display them in the variable watch window in any of these formats too (by using the suffix ",h" or ",b").

Any integer within Flowcode has no specific format as far as your program is concerned so there is never a need to "convert" from a decimal to a binary value. The only time you need to convert is when displaying or writing the values so you as a user can see them in your preferred format (e.g. to display a value as a binary string on a display).

Post Reply