📜  Solidity – 运算符

📅  最后修改于: 2022-05-13 01:54:41.090000             🧑  作者: Mango

Solidity – 运算符

在任何编程语言中,运算符都扮演着至关重要的角色,即他们为编程奠定了基础。同样,如果不使用运算符,Solidity 的功能也是不完整的。运算符允许用户对操作数执行不同的操作。 Solidity 根据其功能支持以下类型的运算符。

  1. 算术运算符
  2. 关系运算符
  3. 逻辑运算符
  4. 位运算符
  5. 赋值运算符
  6. 条件运算符

算术运算符

这些运算符用于执行算术或数学运算。 Solidity 支持以下算术运算运算符:

OperatorDenotationDescription
Addition+Used to add two operands
SubtractionUsed to subtract the second operand from first
Multiplication*Used to multiply both operands
Division/Used to divide numerator by denominator
Modulus%Gives the remainder after integer division
Increment++Increases the integer value by one
DecrementDecreases the integer value by one

示例:在下面的示例中, SolidityTest 合约演示了上述不同类型的算术运算运算符。

Solidity
// Solidity contract to demonstrate
// Arithmetic Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
    // Initializing variables
    uint16 public a = 20;
    uint16 public b = 10;
 
    // Initializing a variable
    // with sum
    uint public sum = a + b;
 
    // Initializing a variable
    // with the difference
    uint public diff = a - b;
 
    // Initializing a variable
    // with product
    uint public mul = a * b;
 
    // Initializing a variable
    // with quotient
    uint public div = a / b;
 
    // Initializing a variable
    // with modulus
    uint public mod = a % b;
 
    // Initializing a variable
    // decrement value
    uint public dec = --b;
 
    // Initializing a variable
    // with increment value
    uint public inc = ++a;
     
}


Solidity
// Solidity program to demonstrate
// Relational Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
    // Declaring variables
    uint16 public a = 20;
    uint16 public b = 10;
 
    // Initializing a variable
    // with bool equal result
    bool public eq = a == b;
 
    // Initializing a variable
    // with bool not equal result
    bool public noteq = a != b;
    
    // Initializing a variable
    // with bool greater than result
    bool public gtr = a > b;
 
    // Initializing a variable
    // with bool less than result
    bool public les = a < b;
 
    // Initializing a variable
    // with bool greater than equal to result
    bool public gtreq = a >= b;
 
    // Initializing a variable
    // bool less than equal to result
    bool public leseq = a <= b;
     
}


Solidity
// Solidity program to demonstrate
// Logical Operators
pragma solidity ^0.5.0;
 
// Creating a contract
contract logicalOperator{
 
     // Defining function to demonstrate
     // Logical operator
     function Logic(
       bool a, bool b) public view returns(
       bool, bool, bool){
        
       // Logical AND operator 
       bool and = a&&b;
        
       // Logical OR operator 
       bool or = a||b;
        
       // Logical NOT operator
       bool not = !a;
       return (and, or, not);
 }
}


Solidity
// Solidity program to demonstrate
// Bitwise Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
    // Declaring variables
    uint16 public a = 20;
    uint16 public b = 10;
 
    // Initializing a variable
    // to '&' value
    uint16 public and = a & b;
 
    // Initializing a variable
    // to '|' value
    uint16 public or = a | b;
 
    // Initializing a variable
    // to '^' value
    uint16 public xor = a ^ b;
 
    // Initializing a variable
    // to '<<' value
    uint16 public leftshift = a << b;
 
    // Initializing a variable
    // to '>>' value
    uint16 public rightshift = a >> b;
   
    // Initializing a variable
    // to '~' value
    uint16 public not = ~a ;
     
}


Solidity
// Solidity program to demonstrate
// Assignment Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
        // Declaring variables
        uint16 public assignment = 20;
        uint public assignment_add = 50;
        uint public assign_sub = 50;
        uint public assign_mul = 10;
        uint public assign_div = 50;
        uint public assign_mod = 32;
     
        // Defining function to
        // demonstrate Assignment Operator
        function getResult() public{
           assignment_add += 10;
           assign_sub -= 20;
           assign_mul *= 10;
           assign_div /= 10;
           assign_mod %= 20;
           return ;
        }
}


Solidity
// Solidity program to demonstrate
// Conditional Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest{
 
     // Defining function to demonstrate
     // conditional operator
     function sub(
       uint a, uint b) public view returns(
       uint){
      uint result = (a > b? a-b : b-a);
      return result;
 }
}


输出 :

算术运算符

关系运算符

这些运算符用于比较两个值。 Solidity 支持以下关系运算符:

OperatorDenotationDescription
Equal==Checks if two values are equal or not, returns true if equals, and vice-versa
Not Equal!=Checks if two values are equal or not, returns true if not equals, and vice-versa
Greater than>Checks if left value is greater than right or not, returns true if greater, and vice-versa
Less than<Checks if left value is less than right or not, returns true if less, and vice-versa
Greater than or Equal to>=Checks if left value is greater and equal than right or not, returns true if greater and equal, and vice-versa
Less than or Equal to<=Checks if left value is less than right or not, returns true if less and equals, and vice-versa

示例:在下面的示例中, SolidityTest 合约演示了上述不同类型的关系运算符。

坚固性

// Solidity program to demonstrate
// Relational Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
    // Declaring variables
    uint16 public a = 20;
    uint16 public b = 10;
 
    // Initializing a variable
    // with bool equal result
    bool public eq = a == b;
 
    // Initializing a variable
    // with bool not equal result
    bool public noteq = a != b;
    
    // Initializing a variable
    // with bool greater than result
    bool public gtr = a > b;
 
    // Initializing a variable
    // with bool less than result
    bool public les = a < b;
 
    // Initializing a variable
    // with bool greater than equal to result
    bool public gtreq = a >= b;
 
    // Initializing a variable
    // bool less than equal to result
    bool public leseq = a <= b;
     
}

输出 :

关系运算符

逻辑运算符

这些运算符用于组合两个或多个条件。 Solidity 支持以下算术运算运算符:

OperatorDenotationDescription
Logical AND&&Returns true if both conditions are true and false if one or both conditions are false
Logical OR||Returns true if one or both conditions are true and false when both are false
Logical NOT!Returns true if the condition is not satisfied else false

示例:在下面的示例中,合约logicalOperator演示了上述不同类型的逻辑运算符。

坚固性

// Solidity program to demonstrate
// Logical Operators
pragma solidity ^0.5.0;
 
// Creating a contract
contract logicalOperator{
 
     // Defining function to demonstrate
     // Logical operator
     function Logic(
       bool a, bool b) public view returns(
       bool, bool, bool){
        
       // Logical AND operator 
       bool and = a&&b;
        
       // Logical OR operator 
       bool or = a||b;
        
       // Logical NOT operator
       bool not = !a;
       return (and, or, not);
 }
}

输出 :

逻辑运算符

位运算符

这些运算符在用于执行位级操作的位级上工作。 Solidity 支持以下算术运算运算符:

OperatorDenotationDescription
Bitwise AND&Performs boolean AND operation on each bit of integer argument
BitWise OR|Performs boolean OR operation on each bit of integer argument
Bitwise XOR^Performs boolean exclusive OR operation on each bit of integer argument
Bitwise Not~Performs boolean NOT operation on each bit of integer argument 
Left Shift<<Moves all bits of the first operand to the left by the number of places specified by the second operand
Right Shift>>Moves all bits of the first operand to the right by the number of places specified by the second operand

示例:在下面的示例中, SolidityTest 合约演示了上述不同类型的位运算运算符。

坚固性

// Solidity program to demonstrate
// Bitwise Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
    // Declaring variables
    uint16 public a = 20;
    uint16 public b = 10;
 
    // Initializing a variable
    // to '&' value
    uint16 public and = a & b;
 
    // Initializing a variable
    // to '|' value
    uint16 public or = a | b;
 
    // Initializing a variable
    // to '^' value
    uint16 public xor = a ^ b;
 
    // Initializing a variable
    // to '<<' value
    uint16 public leftshift = a << b;
 
    // Initializing a variable
    // to '>>' value
    uint16 public rightshift = a >> b;
   
    // Initializing a variable
    // to '~' value
    uint16 public not = ~a ;
     
}

输出 :

位运算符

赋值运算符

这些运算符用于将值分配给变量。左侧的操作数是可变的,而右侧的操作数是值。 Solidity 支持以下算术运算运算符:

OperatorDenotationDescription
Simple Assignment =Simply assigns the value at the right side to the operand at the left side
Add Assignment+=Adds operand at the right side to operand at the left side and assigns the value to left operand
Subtract Assignment-=Subtracts operand at the right side from operand at the left side and assigns the value to left operand
Multiply Assignment*=Multiplies both the operands and assign the value to left operand
Divide Assignment/=Divides operand at the left side by operand at the right side and assign the value to left operand
Modulus Assignment%=Divides operand at the left side by operand at the right side and assign the remainder to left operand

示例:在下面的示例中, SolidityTest 合约演示了上述不同类型的赋值运算符。

坚固性

// Solidity program to demonstrate
// Assignment Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest {
 
        // Declaring variables
        uint16 public assignment = 20;
        uint public assignment_add = 50;
        uint public assign_sub = 50;
        uint public assign_mul = 10;
        uint public assign_div = 50;
        uint public assign_mod = 32;
     
        // Defining function to
        // demonstrate Assignment Operator
        function getResult() public{
           assignment_add += 10;
           assign_sub -= 20;
           assign_mul *= 10;
           assign_div /= 10;
           assign_mod %= 20;
           return ;
        }
}

输出 :

赋值运算符

条件运算符

它是一个三元运算符,首先计算表达式,然后检查条件以获取对应于真或假的返回值。

句法:

if condition true ? then A: else B

示例:在下面的示例中,合约 SolidityTest演示了条件运算符。

坚固性

// Solidity program to demonstrate
// Conditional Operator
pragma solidity ^0.5.0;
 
// Creating a contract
contract SolidityTest{
 
     // Defining function to demonstrate
     // conditional operator
     function sub(
       uint a, uint b) public view returns(
       uint){
      uint result = (a > b? a-b : b-a);
      return result;
 }
}

输出 :

条件运算符