It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
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.
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.
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.
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.
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.
It is a bitwise OR operator.
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 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.
Related
for (i in 1:length(data$name)){
if (!is.na(data$years[i]) >= 34 & !is.na(data$gender[i]) == "male" & !is.na(data$classification[i]) == "mid"){
print(data$name)
}
}
There's a few problems in your code. I am assuming this is a class exercise or similar, so I'll add a bit extra detail to illustrate where you've missed a step.
First of all you loop works fine, but your if condition is not completely correct.
!is.na(data$years[i]) >= 34
All of your conditions look (somewhat) like this. The idea is obvious, you want to "check that data$years[i] is not null, and above 34". But in R (and most languages) you have to check these seperately.
!is.na(data$years[i]) && data$years[i] >= 34
Similar for the rest of your conditions.
Next your print statement is printing out everything, because this is what you're asking it to:
print(data$name)
is "ignorant" of anything else you've done up till now. It seems you want to print the specific record, eg:
print(data$name[i])
And this is the way to go about it.
Now R has a thing called "vectorization", so we could wrap this entire loop up in one go:
data$name[!is.na(data$years) & !is.na(data$gender) & !is.na(data$classification) & data$year > 34 & data$gender == "male" & data$classification == "mid"]
But I am assuming that is not part of your current exercise. Note the slight (but important) difference that for vectorized (eg. more than 1) condition I use single & but for single conditions I use 2 &&. The latter is optimized to be "lazy" for single inputs (thus faster).
Perhaps you can try subset + complete.cases like below
subset(
data,
years >= 34 & gender == "male" & classification == "mid" & complete.cases(data[c("years", "gender", "classification")])
)$name
This question already has answers here:
Using Exclamation Marks '!' in C
(5 answers)
Closed 3 years ago.
I was reading a code and I found these "!" all over the place what is it supposed to do ? this is a part from the code I found it in :
if (!piocherMot(motSecret))
exit(0);
piocherMot is a function in another file while "motsecret" is a variable in my main code.
This
!
is logical NOT operator & its a unary operator i.e it takes only one operand, result of this operator is either true(1) or false(0). Truth table of logical NOT operator is
A !A
----------
| 0 | 1 |
| 1 | 0 |
----------
Hence if piocherMot(motSecret) results in true i.e !1 which is 0 then if block will not get executes, in reverse case it gets executes.
if(!1) { /* 0 i.e if block won't executes */
}
And
if(!0) { /* 1 i.e if blocks executes */
}
I have seen something like this in some of my coworkers code today :
I2C1ADB1= (slave_read_address | 0x01);
What does this | 0x01 part do? Does it end 1 at the end of the bits?
Let's say I2C1ADB1=0b00000000. If I use above line, will the new I2C1ADB1 be 0b000000001? Will it also increase the bit count from 8 to 9?
'|' is bit-wise OR operator in C. It does bit-wise OR between two values and return the final value.
I2C1ADB1= (slave_read_address | 0x01);
Assume slave_read_address in binary is 0bxxxxxxxx where each x is bit value 1 or 0. Similarly, 0x01 in binary is 0x00000001.
As you know OR will return true (1) if at least one of the value is true (1). Otherwise returns false (0).
So After the above C line, I2C1ADB1 will have 0bxxxxxxx1.
The operator will not ADD bits. Usually '|' (OR) operator is used to set a particular set of bits without altering other bits.
The statement I2C1ADB1 = (slave_read_address | 0x01); stores the value of slave_read_address into I2C1ADB1, forcing the low order bit to 1.
Your interpretation is incorrect, the value is not shifted, no extra bit is appended. The lowest bit is set to 1:
0 becomes 1,
1 is unchanged,
2 becomes 3,
3 does not change,
4 becomes 5,
etc.
Because on the left you have a variable and on the right a constant the result is to set all the corresponding 1 bits from the constant in the variable. In this case you’re right: it sets the last bit. No bit count increase occur!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I recently corrected a bug in a C program where I had:
if (foobar != FOO | BAR | BAZ)
The correct code is
if (foobar != (FOO | BAR | BAZ))
According to the C operator precedence it is clear that != has precedence over |.
My question is why it is like this and not the other way around? From my experience I will often use either a == b || a == c or d == (a | b | c), but never a == b | c == d.
What is the logic behind this choice?
It has historical reasons, quote from Dennis Ritchie:
“Early C had no separate operators for & and && or | and ||. Instead
it used the notion (inherited from B and BCPL) of ‘truth-value
context': where a Boolean value was expected, after ‘if‘ and ‘while‘
and so forth; the & and | operators were interpreted as && and || are
now; in ordinary expressions, the bit-wise interpretations were used.
It worked out pretty well, but was hard to explain. (There was the
notion of ‘top-level operators’ in a truth-value context.) “The
precedence of & and | were as they are now.
Primarily at the urging of
Alan Snyder, the && and || operators were added. This successfully
separated the concepts of bit-wise operations and short-circuit
Boolean evaluation. However, I had cold feet about the precedence
problems. For example, there were lots of programs with things like:
if (a==b & c==d) …
“In retrospect it would have been better to go
ahead and change the precedence of & to higher than ==, but it seemed
safer just to split & and && without moving & past an existing
operator.”
Dennis Ritchie's Development of the C Language covers this and other curious historical artifacts.
Basically, originally the language did not have the && and || operators -- just & and |, so you would write things like a == b | c == d. The precedence rules were designed based on that.
Later, the short ciruiting operators were added, but the precedence of the old operators was not revised.
I found this code on ioccc and I have trouble even beginning to understand how it works!
void main(int riguing, char** acters) {
puts(1[acters-~!(*(int*)1[acters]%4796%275%riguing)]);
}
An explanation of how this is valid code and how it actually works would be fantastic!
First, in C (and C++) k[pointer] and pointer[k] mean exactly the same thing, which is *(k + pointer) and *(pointer + k), respectively. Code obfuscators often seem to like to use the first version because many people find it unusual. But it's reasonably obvious that pointer + k and k + pointer are the same computation.
The other little diversion in the snippet is the use of
pointer-~!(something)
which is exactly the same as
pointer + (something == 0 ? 2 : 1)
How this works:
The ! operator turns any true (non-zero) value into 0 and the false (0) value into a boolean true (1):
!something: something == 0 ? 1 : 0
The ~ operator is bitwise inverse, so it turns 0 into the number consisting of all 1 bits, which is -1 and 1 into the number consisting of all 1 bits except the last bit, which is -2. See Wikipedia article on two's complement.
~!something: something == 0 ? -2 : -1
Subtracting that from something is the same as adding the negative (a - -b == a + b)
a-~!something: something == 0 ? a + 2 : a + 1
Finally
1[a-~!something]: something == 0 ? a[3] : a[2]
So it selects either the second or third command line argument based on whether some computation is zero or not.
So now we need to decipher "some computation". We start with the type-punning operator *(T *)(pointer), in this case *(int*)(char*), reads out whatever the pointer points to as though it were a T. So in this case, it reads the first sizeof(int) characters from 1[acters] -- that is, from the first command-line argument (argv[1]) -- as though they were the internal representation of an integer. That will code every president as an integer based on the first four characters of their surname.
While there have been several repeated presidential surnames in US history, it's not a problem as long as the two presidents with the same name shared a political party.
One such pair, the father and son John Adams, Jr. (a Federalist), and John Quincy Adams (elected to the senate as a Federalist and to the presidency as a Democratic-Republican), are eliminated as being prior to the first valid president (Franklin Pierce), as is the elder Harrison (William Henry, a Whig) whose grandson Benjamin was elected as a Republican. The father and son George H.W. and George W. Bush are both Republicans. And the two Johnsons, Andrew and Lyndon Baines (as far as I know, not related to each other), were both Democrats.
So that leaves only the two Roosevelts, Theodore (Republican) and Franklin Delano (Democrat). The great-great-great-great-grandfathers of the two Roosevelt presidents were the brothers Johannes and Jacobus, sons of Nicholas Roosevelt (or Nicholas van Rosenvelt) (1658-1742) and grandsons of the Dutch immigrant Claes Maartenszen Van Rosenvelt, making them fifth cousins. However, the presidents were more closely related to each other through Eleanor Roosevelt, Theodore's niece and FDR's wife. In order for the IOCCC entry to work, it's necessary to represent the younger Roosevelt as "fdr", as he was commonly known.
So that only leaves (integer)%4796%275%riguing, or (integer)%4796%275%4, since riguing (aka argc) is 4. That's a simple hash function, which I imagine was discovered by trial and error using the list of presidential surnames and their affiliations.