I have this code :
int flags = some integer value;
compte.actif = !Convert.ToBoolean(flags & 0x0002);
It is working very well the problem is I don’t really understand how it s working..
The & operation is a bitwise AND I assume so Imagine 110110 & 000010 I assume it will result 001011 (maybe i’m wrong from here). The goal is to check if the 2’s bit in the first term is filled. So in this case it is true.
I don’t really understand How it can be converted in boolean..
Thanks for help
Bitwise and of 110110 & 000010 is 000010.
The
ToBooleanlooks for a non-zero value, so basically, this code checks thatflagshas the 2nd bit set, then negates it (!). So it is checking “is the 2nd bit clear”.A more traditional test there might be: