Someone showed me the following code snippet and asked what it meant:
if (!pFCT->FBMap(| ( VBQNum - 1 ) / 8 |) & (1 << (7 - ( ( VBQNum - 1 ) % 8)))))
{
stuff
}
And I got stuck on the stand alone vertical bars. I know two together mean "or" but just one, what does that mean.
Dale said he knew it meant "or" with two operands. His question is what it means when used as a unary operator. Short answer is that it doesn't mean anything in C/C++.
In some languages (like Verilog for coding hardware) this is an or-reduction, which result in a 1 if any bit is one.
So I think this is a syntax error unless the is something funny going on overloading the parentheses that I can't get my head around.
Your code is not valid C, unless put in a specific (and quite artificial) context. Operator
|
is a binary operator. It is a bitwise-or, as you seem to know already. By itself, it cannot be used the way it is used in your code.If one wanted to force this code to compile as C code, one'd probably have to define
FBMap
as a macro. Something likethus trying to emulate the mathematical "absolute value" operator
| |
. Your call will expand intothus making the application of
|
operator valid.But even after that you'd need
something_else
to be a function pointer in that*pFCT
struct, since the call looks awfully as a C++ method call. Your question is tagged C, so the only way to make it work in C is to introduce a function pointer member into the struct.It is a bitwise OR. Essentially it takes the two values and ORs each of the corresponding bits in their binary representations:
If either operand's bit is a 1, the answer's bit in that place is a one. If neither are 1s, the answer has a 0 there.
I guess whoever showed you the line in question meant absolute value
Oh! A single vertical bar means bitwise or.
Bitwise inclusive or:
Source.
One bar by itself means "bitwise OR" (as opposed to the double bar which means "Logical OR")
However, the vertical bars in your sample, as far as I can tell, are syntax errors. It looks like the author is going for "absolute value", which would use vertical bars – in writing or pseudo-code – but not in any computer language I know of.