tags:

views:

408

answers:

11

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.

+6  A: 

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.

Dominic Rodger
+2  A: 

one bar is a bit-wise or

Itay
+6  A: 

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.

James Curran
You meant "OR" right :-)
drewk
@drewk -- damn your fast -- I had that fixed in a few seconds....
James Curran
You meant "you're" right? (Sorry, I couldn't resist)... going back to english.stackexchange.com now...
Atømix
I'd be startled if there were *no* languages that used vertical bars to mean modulus – it's been standard mathematical notation for a long time, and computing borrows loads from math – but it's sure not common.
Donal Fellows
+3  A: 

It is a bitwise OR operator.

Reed Copsey
+2  A: 

It means bit wise OR

Howard May
+1  A: 

Im going to nominate that conditional for a coding hall of shame.

Derek
+1 if it is inside of a loop getting eval'd 1M time
Derek
+1  A: 

| is the bitwise OR operator in C

Josh
+13  A: 
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.

pmg
+2  A: 

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.

Chris Cooper
+2  A: 

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.

JeffW
+1  A: 

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.

AndreyT
Fine. The code does compile. I did make a typo when I entered it. It should have been:
Dale
Dale
This sure seems like it's trying to do an ABS on the value of VBQNum - 1 / 8. FYI, I was just told this is SAS C.
Dale
"SAS C" ≠ "C" it seems.
Donal Fellows
Evidently it is not.
Dale
At the first sight, SAS C docs don't seem to suggest anything as weird.
AndreyT