问题
I have an assignment that I'm supposed to implement the MIPS processor in C++ and one of the MIPS instructions is "AND" and "OR" the MIPS instruction is represented as and $s1,$s2,$s3
which means that $s1=$s2(and)$s3
the $s2 and $s3
registers are represented into bits ,,, how can I perform the "AND" and "OR" operations using C++?
回答1:
There are both binary and logical and and or operators in C++.
int a, b = 1;
int x = a | b; // binary OR
int x = a & b; // binary AND
bool x = a || b; // boolean OR
bool x = a && b; // boolean AND
回答2:
A boolean comparison will return true or false depending on the value of the two operands. Logical "and" will return true if both operands are non-zero. Logical "or" will return false only if both operands are false.
Bitwise operators are different and operate on the bits of the operands. A bit wise "and" will set a bit to true only if both corresponding bits are true:
101 & 110 = 100
Bitwise "or" sets a bit to zero only if both corresponding bits are zero:
010 | 001 = 011
The two bitwise comparison operators are more closely related to the shift operators (<< and >>) and the one's complement operator (~) in that they are low level operations.
来源:https://stackoverflow.com/questions/6246819/how-can-i-implement-and-and-or-operations-in-c