Page 1 of 1

Decimal ti binary conversion.......

Posted: Mon Aug 23, 2021 10:24 am
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

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

Posted: Mon Aug 23, 2021 10:32 am
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.

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

Posted: Wed Aug 25, 2021 1:53 am
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.

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

Posted: Wed Aug 25, 2021 9:47 am
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).