implementing << and >> with &, |, ^, ~ only? - bit-manipulation

Can bitwise left-shift (<<) and right-shift (>>) operators be implemented using only AND (&), OR (|), XOR (^), NOT (~)?

Related

Why can't I write cout<<a==b; but can write cout<<(a==b);

I know () has higher precedence than <<, and << has higher precedence than ==, but I want to know why I can't write cout<<a==b; yet can write cout<<(a==b); in C++.
How the compiler translates cout<<a==b; and then shows error?
<< has higher precedence than ==
as you can see here.
The statement
cout<<a==b
is equivalent to
(cout<<a)==b
The expression
cout<<a
returns a stream. This stream is compared to b. If there is no left shift operator for a stream and a or no comparing operator for a stream and b this causes a compiler error
cout<<a==b is similar to (cout<<a) == b as << has higher precedence over ==. Now cout<<a will be syntactically incorrect if the type of a is not supported for <<. Next, if a has an overload for the << operator, it will again be syntactically wrong as the == operator can't operate with std::stream and type of b unless b overloads this compare operator.
But in case of cout<<(a==b), a==b will result in a boolean value. As the << operator support boolean value it is a valid operation.
<< priority is higher than == so it's interpreted as (cout<<a)==b
but = has lower so you can do :
bool t = a == b

Use of <<++ and >>++ operators in C++ [duplicate]

This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 4 years ago.
I have encountered <<++ and >>++ operators many time in`C++, but I don't understand what they are. What is the specific meaning and use of these operators, and how are they different from right shift and left shift operator?
C++ compilers ignore whitespace unless in certain situations such as string literals.
<<++ and >>++ is really just a bit-shift operatior << or >>, followed by an increment operator ++.
Consider this code:
a <<++ b is equivalent to
a<<++b because the spaces are ignored in this context, and then equivalent to
a << ++b (a left shifted by a pre-incremented b)
a << (++b) due to operator precedence. Bit shift operators have lower precedence than incrementation.
There are two separate operators in both cases: left shift (<<), right shift (>>) and increment operator (++).
You can rewrite the following:
a >>++ b
as:
a >> (++b)

"|=" what does this mean and what is this called? (c++)

void show_node_names() { display_flags |= ShowNodeNames; } // what is "|="?
I'm not sure what "|=" does or what it's called.
Any help?
That statement is a bitwise or assignment.
It is equivalent to doing display_flags = display_flags | ShowNodeNames.
In particular, it will set every bit in display_flags to 1 if the corresponding bit in ShowNodeNames is 1.
The |= operator is a compound assignment operator like += or *=, but using the bitwise OR operator. The line
display_flags |= ShowNodeNames;
is equivalent to
display_flags = display_flags | ShowNodeNames;
If you haven't seen the bitwise OR operator, you should read up on it for more details. If you're familiar with it, then you can think of display_flags |= ShowNodeNames; as a way of saying "make all the bits set in ShowNodeNames also set in display_flags."
Hope this helps!
| (which can also be spelt bitor) is the bitwise or operator. It combines the bits of each operand so that each bit of the output is set if the corresponding bit of either operand is set. Compare this with the bitwise and operator, & or bitand, where each bit is set of the corresponding bit of both operands is set.
|= (or or_eq) is the corresponding assignment operator. As with all compound assignment operators, a |= b is equivalent to a = a | b, except that a is only evaluated once. Its effect is to set each bit in a that's set in b, and leave the other bits unchanged.

C++ Converting Postfix to infix

So i'm programming a cmd-based calculator in C++. I finished it, but i was wondering, after converting the infix to postfix, i have a queue called the postfix queue containing the operators/operands in correct order. How do I convert a postfix expression back to infix?
If you don't mind producing some extra parentheses, it should be pretty easy. You basically "evaluate" the postfix data about like usual, except that when you get to an operator, instead of evaluating that operator and pushing the result on the stack, you print out an open-paren, the first operand, the operator, the second operand, and finally a close-paren.
If you didn't mind changing the order, it would also be pretty easy to avoid the extraneous parentheses. Walk the expression backwards, rearranging things from operator operand operand into operand operator operand. If you encounter an operator where you need an operand, you have a sub-expression to print out similarly. You need to enclose that sub-expression in parentheses if and only if its operator is of lower precedence than the operator you encountered previously.
For example, consider: a b + c *. Walking this backwards, we get *, then c, so we start by printing out c *. Then we need another operand, but we have a +, so we have a sub-expression. Since + is lower precedence than *, we need to enclose that sub-expression in parentheses, so we get c * (b + a).
Conversely, if we had: a b * c +, we'd start similarly, producing c +, but then since * is higher precedence that +, we can/could print out the a * b (or b * a) without parens.
Note that with - or / (or anything else that isn't commutative) you'd have to be a bit more careful about getting the order of operands correct. Even so, you're not going to get the original expression back, only an expression that should be logically equivalent to it.

What is "operator<<" called?

I know the names of most of the operators but not sure what operator<< and operator>> are called.
i.e.
operator=() // the assignment operator
operator==() // the equality of comparison operator
operator++() // the increment operator
operator--() // decrement operator etc.
operator<() // the less-than operator
and so forth...
<< is both the insertion operator and the left-shift operator.
>> is the extraction operator and the right-shift operator.
In the context of iostreams, they are considered to be stream insertion/extraction. In the context of bit-shifting, they are left-shift and right-shift.
In C++ Streams,
<< is insertion operator.
>> is extraction operator.
In Binary Operations,
Right shift (>>)
Left shift (<<)
<< left shift
>> right shift
<< = Bitwise left shift
>> = Bitwise right shift
Bit Shift Operators
The original names were left shift operator (<<) and right shift operator (>>), but with their meanings perverted by streams into insertion and extraction, you could argue that even in bitwise operations << inserts bits on the right while >> extracts them. Consequently, I almost always refer to them as the insertion and extraction operators.
<< is the 'left-shift' operator. It shifts its first operand left by the number of bits specified by its second operand.
They are called the Guillemet Left and Guillemet Right symbols :)