Page 1 of 1

how to set or clear individual bits in a veriable??

Posted: Wed Feb 03, 2021 10:28 am
by kg300
in the C compiler get this code working but how to do it in flowcode, any idae pls..

unsigned int32 code;

for(i = 0; i < 32; i++)

if( count > 20)
bit_set(code, (31 - i));

else

bit_clear(code, (31 - i));

Re: how to set or clear individual bits in a veriable??

Posted: Wed Feb 03, 2021 12:49 pm
by mnf
One way to do it is with '|' (or operator) or to clear bits '&' (and operator)

Code: Select all

x = x | (1 << 4)
will set bit 4

Code: Select all

x = x & ~(1 << 4)
will clear bit 4

To set a 'variable' bit

Code: Select all

x = x | (1 << n)
To set or clear multiple bits you can use a mask

Code: Select all

x = x | 0b11110000
will set bits 7..4

Martin

Re: how to set or clear individual bits in a veriable??

Posted: Wed Feb 03, 2021 2:24 pm
by kg300
Thanks martin :D

Re: how to set or clear individual bits in a veriable??

Posted: Wed Feb 03, 2021 3:47 pm
by AbhijitR
Hello! Martin

Wonderful explanation, thank you.

Abhi