Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I was going through the source code of Intel's deep learning framework Caffe, when I came across |=. I've never seen that before in any code. In fact, I found it twice in the code. Line 188:
need_backward |= blob_need_backward_[blob_id];
and line 254:
need_backward |= param_need_backward;
I realize that they both are housed in a for loop which might signify some kind of relation. I'm just assuming.
Thats the 'bitwise OR assignment' compund assignment operator.
x |= y;
is equivalent to:
x = x | y;
There are a number of similar operators: +=, -=, *=, etc.
See: operator_assignment
|= is a compound assignment.
<var> |= <expr> means <var> = <var> | <expr>
It is the bitwise OR equivalent of += for incrementing. You can do this with most mathematical operators in C++.
| is bitwise OR, so you're reassigning a variable to its OR'd result.
it is shorthand for
need_backward = need_backward | param_need_backward;
you are performing bitwise or operation
Related
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 5 years ago.
Improve this question
Recently I have started looking into C++ from the basics and got to know (to my surprise) that I can give series of expressions in a single line separated by commas in some cases as below
//it'll execute all the expressions mentioned after condition seperated by comma
for(int i=0;condition;++i,++x,cout<<"in for loop"<<endl,z = z*2);
(x>y)? ++z,z1 = z*2, cout<<"printing statement"<<endl:cout<<"condition failed"<<endl,z = z/2;
Here, I have a confusion after this is working. Is it safe to code in that way or is there any problem coding in such a way?
Please clarify!!!
Correct me if i'm wrong anywhere, I'm just curious to know why most of the programmers don't use this way (I haven't seen such kind of lines anywhere)
The comma operator , evaluates each of its operands in sequence. In standardese, there is a sequence point between the evaluation of the left operand and the right operand.
In a expression which contains a comma operator, the value of the left operand is discarded and the expression takes on the value of the right operand. In both of the examples above, the comma operator is used in a void context, so none of the values are used.
So a statement like this where the value of the comma operator is not used:
exp1, exp2, exp3, exp4;
Is equivalent to the following sequence of statements:
exp1; exp2; exp3; exp4;
The first example is equivalent to the following:
for(int i=0;condition;) {
++i;
++x;
cout<<"in for loop"<<endl;
z = z*2;
}
And the second example:
if (x>y) {
++z;
z1 = z*2;
cout<<"printing statement"<<endl;
} else {
cout<<"condition failed"<<endl;
z = z/2;
}
Note that this is considerably more readable that the one-line versions. It's also easier to debug. Since debuggers typically step through code a line at a time, it breaks up the flow and is more granular.
Not indenting and spacing your code is not less costly regarding performance. It is unreadable, confusing and a pain to understand for you and for anyone who'd have to work with it.
Lot of people will prefer a well-syntaxed, beautifully and efficiently-indented code than a top-performance one. You can modify, debug and refract a code which might not work but has the advantage to be understandable.
On the other hand, very few codes remain unchanged and stay unread. There will always be a time when someone, may be you, will have to read it again and if it looks like the one if your OP, it will be very time costly to do.
It is allowed. In my opinion and i say without a reference that in general other programmers do not find your 'for' loop very readable. Sometimes in a for loop you want to do other things then just for (int i = 0; i < 10; ++i){"do something"}For example increment 'i' in every loop with two. Reading code should be like reading a text. If you are reading a book you do not want it to be unnecessary difficult.
Your other question was about the safety of the statement. The biggest problem with the code is that you might get confused about what you are doing exactly. Bugs are caused by human errors (computers are deterministic and are executing machine code which ultimately has been written by a human) and the question about safety mainly depends on how you define it.
To give you some tips. When i just started programming C++ i looked a lot on CPP reference. I will give you a link where you can read about the syntax and what is allowed/possible. On this website there are quite a lot of examples on all kinds of statements. They will in general not put 5 or 6 operations within in a single line. If there are more variables that you want to change then you might want to do that in the scope of the for loop so it will be more readable instead of inside the for loop.
http://en.cppreference.com/w/cpp/language/for
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently trying to build a very simple compiler. I have created a function that converts a mathematical equation in infix notation to RPN using the shunting-yard algorithm however I have encountered a problem. I did not include error checking in my conversion function so I was wondering if there was a simple way to check if a function in infix notation is in correct infix notation syntax. This would enable me to keep my current conversion function without obscuring it with error checking.
If your expressions only consist of parentheses, values (constants and/or identifiers), and prefix, postfix and infix operators, then there are two error conditions you need to check:
The parentheses must match. It's hard not to notice this with the shunting yard algorithm, because there is a point in the algorithm where an open parenthesis is popped off the stack when a close parenthesis is encountered in the input. If you overpop the stack or you don't pop the entire stack at end of input, then the parentheses didn't balance.
The tokens must conform to the following simple regular expression:
PRE* VAL POST* ( INFIX PRE* VAL POST* )*
where
PRE is a prefix operator or an (
POST is a postfix operator or a )
VAL is a value: a constant or an identifier
That actually reduces to a two-state state machine: the initial state (state 0) could be called "expecting value" and the other state (state 1) could be called "expecting operator". Only state 1 is accepting, and the transitions are as follows:
State 0:
PRE → State 0
VAL → State 1
State 1:
POST → State 1
INFIX → State 0
All other transitions are to an error.
It's often necessary to implement this state machine in order to properly deal with unary minus (and other operators which could be prefix or infix), and it is, in any event, very simple to integrate into your input handling.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What is the mathematical equivalent equation of following Macro
#define SQ(a) (a*a )
int answer SQ(2 + 3 );
Output is 11 for this case
and for
int answer SQ(2 + 4);
is 14 I can't figure out the equation from outputs.
The macro you defined lacks brackets to keep the arithmetic working as you want. Remember preprocessor macros are doing text replacement solely. So what you'll get from calling it as you shown expands to
int answer (2 + 4 * 2 + 4);
and according operator precedence the result is 14.
Write your macro as
#define SQ(a) ((a)*(a))
to get the result you expected.
SQ(2 + 4) expands to 2+4*2+4 = 14 because you have not used brackets in your macro. It is a generic macro pitfall for newcomers as macros are not quite safe in this respect as they are just processed by the preprocessor as raw string.
You should write something like this:
#define SQ(a) ((a)*(a))
and that will expand to: (2+4)*(2+4) = 36.
The same logic holds true If you replace 4 with 3, you will get to the 11, and with the corrected macro 25.
That being said, you really should not initialize an integer like that. The general way is to use explicit assignment.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
oops, I am not talking about Object Operator, some call it arrow, some call it thingy...
today, while studying DISCRETE STRUCTURES, teacher told us,
if p then q , this is a conditional statement and its written as p -> q ( p implies q),
my question was, what this sign is called, teacher says its if and then sign, then say, its implies sign, but I dont feel it right...
can somebody tell me what this sign is called?? can somebody explain it ? as I was caught in the sign only, I wasnt even able to listen what the teacher was telling about this conditional statement...( teacher sent me out of class saying YOU ARE ASKING FOOLISH QUESTIONSS :( )
One Request..I dont know where to Put this question...as discrete structures relates to programming, so putting my question right here, Forgive me for this if I am at wrong place ( no down-voting , rather please shift this question to approperiate place )
Wikipedia titles it the Material Conditional operator, though I've usually called it the implication operator. In my discrete structures class, we generally read it as either "if p then q" or "p implies q".
For completeness sake, here's the truth table:
p | q | p -> q
--------------
T | T | T
T | F | F
F | T | T
F | F | T
Maybe you could call it simply the arrow sign. We used to say it like "p arrow q".
you can understand it by this way,
p -> q // p derives q. You can reach to q if you are given p.
// q is obtainable from p.
This question already has answers here:
Bitwise OR of constants
(5 answers)
Closed 9 years ago.
In this answer, the answerer uses the | operator: Yes/No message box using QMessageBox
According to the documentation, that argument is supposed to be StandardButtons.
So the | operator merges two StandardButton into one StandardButtons?
I've tried looking through the Qt documentation, but there is none on operators. So it must be a C++ operator?
| is the bitwise OR operator, which is commonly used to combine enum values (assuming that the integers backing the enum values are powers of two). In this case, the code specifies that it wishes to have both a Yes and a No button. (Note that this is standard C++ syntax and nothing Qt specific.)