Visual basic: Variable assignment with OR

.net, active-directory, bit, bit-manipulation, vb.net

Solution

or is a bitwise operation. `C = A or B` ensures that all bits in C are set which are set in A or in B.

Example:

   01001001
or 00011000
   --------
   01011001

`AccountOptionsEnum.UF_ACCOUNTDISABLE` is probably a value of the form `2^x`, which means that only a single bit is set. Let's assume that it is the fourth bit from the right:

00001000   = UF_ACCOUNTDISABLE

The operation `X = X or UF_ACCOUNTDISABLE` ensures that this fourth bit is set in X. If it has been set before, nothing changes:

   00011100 old X
or 00001000 UF_ACCOUNTDISABLE
   --------
   00011100 new X

If it has not been set, it will be set:

   00010100 old X
or 00001000 UF_ACCOUNTDISABLE
   --------
   00011100 new X

Basically, the following bitwise operations are commonly used:

X = X or FLAG         ' sets FLAG in X
X = X and not FLAG    ' removes FLAG from X
X = X xor FLAG        ' toggles FLAG in X
if (X and FLAG) <> 0  ' checks if FLAG is set in X

Problem

I don't understand what the OR statement do in this sample of code. ``` DE.Properties("UserAccountControl").Value = CInt(DE.Properties("UserAccountControl").Value) Or AccountOptionsEnum.UF_ACCOUNTDISABLE ``` DE.Properties("UserAccountControl").Value represents a decimal. The enum is integer. I know that this is code is used to desactivate a user in an Active Directory but i don't understand how the "OR" works in here. Thanks

Original source