Hi, everyone! This article will introduce some mathematical and logical manipulation called operators. Actually we have learnt some operators in code through previous chapters, like the assignment signal "=" , addictive signal "+", subtraction signal "-" multiplication signal "*" etc. This chapter we will learn the operators more detailed.
The operators are include arithmetic operators, relational operators,logical operators, bit wise operators, all these operators are taught in our primary school ,high school or university .So let us just list them on types
0) Arithmetic operators
Operator | Means | example |
+ | add two operands | 1+2 |
- | the first operands subtract the second operand | 2-1 |
* | multiples two operands | 1*2 |
/ | divides numerator by sub-numerator | 1/2 |
These basic arithmetic operators are enough for us to code lately, in the summary chapter I will list them all. Now, we just need to get these.
1) Relational operators
Oprater | means | example |
> | the left operand is more than the right one if right return true ,otherwise return false | 1 > 2 false |
< | the left operand is less than the right one if right return true, otherwise return false | 1<2 true |
== | if the two operands equal, if right return true if not equal ,return false | 1==2 false |
!= | if the two operands are not equal, if right return true, else return false | 1!=2 true |
>= | the left operand is more than or equals the right one , if right return true, otherwise return false | 1>=2 false |
<= | the left operands is less than or equal the right one, if right return true, otherwise return false | 1<=2 true |
These relational operators are the basic relationship.
2) Logical operators
Now let us declare some variables " int a = 1; int b = 2; int c = 3; int d = 4; "
Logical sign | means | example |
&& | AND | (a>b)&&(c<d) |
|| | OR | (a>b)||(c<d) |
! | NOT | !(a>b) |
In math, the logical true table shows below
AND:
x | 1 | 0 | 1 | 0 |
y | 1 | 1 | 0 | 0 |
1 | 0 | 0 | 0 |
x | 1 | 0 | 1 | 0 |
y | 1 | 1 | 0 | 0 |
1 | 1 | 1 | 0 |
x | 1 | 0 |
0 | 1 |
When you want to design a program depends on the different situation ,you can use these logical operators in your program . The judgement related program will be introduced later.
3)Bitwise Operators
Here we just talk about two bitwise operators. As the time goes on, we'll learn all of them in our Code.
Operator | Means | Example |
& | Bit AND if both bits are 1 ,the result is 1, otherwise the result is 0. | 0b01&0b11* |
| | Bit OR only one bit is 1 ,the result is 1, otherwise the result is 0 | 0b01|0b00 |
*0b represents the binary
Ok, we have list a lot of operators , next, let us use some operators to solve the problems which we met in daily life.
0) I want to buy a bottle of coca cola which priced $2 , I give the cashier $5, and how much the cashier should give change?
1) My friend Jen. will have the birthday, i really want to give her a present, but it really depends on the time and the money, if i have free time i can prepare a home made present, but if i don't have enough time , so just buy a present. How can i handle this situation?
2) Last year, I bought a new bicycle on $200, now today i want to sell it on 40 percent off, how much is the second hand bicycle now?
3) A switch can represent two state on or off , when turn it on , the light lights. When turn it off ,the light off. But now I have two switches series connection , then join the light.Please detail the state of two switches and the light.
All these question are a logical or arithmetic problem, so think over and write down our own answers, next chapter we will learn the program base on conditions called Decision making, after get the decision making , we will be able to solve these problems in code.
Have a nice weekend everyone!