What does a single vertical bar mean in C [closed]

2020-03-26 18:54发布

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.

标签: c
7条回答
家丑人穷心不美
2楼-- · 2020-03-26 19:12

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.

查看更多
成全新的幸福
3楼-- · 2020-03-26 19:12

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 like

#define FBMap(x) something_else(abs(0 x 0))

thus trying to emulate the mathematical "absolute value" operator | |. Your call will expand into

pFCT->something_else(abs(0 | ( VBQNum - 1 ) / 8 | 0))

thus 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.

查看更多
戒情不戒烟
4楼-- · 2020-03-26 19:16

It is a bitwise OR. Essentially it takes the two values and ORs each of the corresponding bits in their binary representations:

10010001
01001010
--------
11011011

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.

查看更多
叼着烟拽天下
5楼-- · 2020-03-26 19:18
if (!pFCT->FBMap(| ( VBQNum - 1 ) / 8 |) & (1 << (7 - ( ( VBQNum - 1 ) % 8))))) { stuff }
/*              ^^^   syntax error   ^^^                               */

I guess whoever showed you the line in question meant absolute value

if (!pFCT->FBMap(abs( ( VBQNum - 1 ) / 8 )) & (1 << (7 - ( ( VBQNum - 1 ) % 8))))) { stuff }
/*              ^^^^^^                  ^^^                            */

Oh! A single vertical bar means bitwise or.

查看更多
The star\"
6楼-- · 2020-03-26 19:21

Bitwise inclusive or:

The bitwise-inclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Source.

查看更多
Animai°情兽
7楼-- · 2020-03-26 19:35

One bar by itself means "bitwise OR" (as opposed to the double bar which means "Logical OR")

 1 | 1 == 1
 0 | 1 == 1
 1 | 0 == 1
 0 | 0 == 0
 true || true  == true
 false || true == true


 01 | 10 == 11
 01 || 10 == true

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.

查看更多
登录 后发表回答