Use logical OR || to combine two integers?
-
09-03-2021 - |
Вопрос
In this MSDN article on file sharing mode with std::ofstream, Microsoft writes:
To combine the
filebuf::sh_read
andfilebuf::sh_write
modes, use the logical OR (||
) operator.
Both constants are of type int
, as far as I can see, so I don't understand why we should use the logical OR instead of the bitwise OR (|
). I always thought that the logical OR produces a Boolean value, so there is no way of interpreting the result?
Решение
It is a documentation error. In later versions, they have restructured the documentation, delegating the explanation of bitmask types to the following page:
A bitmask type can be implemented as either an integer type or an enumeration. In either case, you can perform bitwise operations (such as AND and OR) on values of the same bitmask type. The elements A and B of a bitmask type are nonzero values such that A & B is zero.
Get there via
- http://msdn.microsoft.com/en-us/library/5785s5ts(v=vs.71).aspx
- http://msdn.microsoft.com/en-us/library/7z434859(v=vs.71).aspx
- http://msdn.microsoft.com/en-us/library/t60aakye(v=VS.71).aspx
- http://msdn.microsoft.com/en-us/library/y1et11xw(v=VS.71).aspx
- http://msdn.microsoft.com/en-us/library/5kb732k7(v=VS.71).aspx
Yay! for MSDN navigation. Also, VS2010 documentation has been altered again: the newest page doesn't even describe the semantics of the flags fields anymore (allthough, you could take one mention of _Mode | ios_base::out
to imply that the params are bitmask combinations)