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

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
kg300
Posts: 31
Joined: Thu May 31, 2018 6:25 am
Has thanked: 5 times
Been thanked: 1 time
Contact:

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

Post 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));

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

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

Post 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

kg300
Posts: 31
Joined: Thu May 31, 2018 6:25 am
Has thanked: 5 times
Been thanked: 1 time
Contact:

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

Post by kg300 »

Thanks martin :D

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

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

Post by AbhijitR »

Hello! Martin

Wonderful explanation, thank you.

Abhi

Post Reply