// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/*
赋值运算符
+=
-=
*=
/=
%= 将左边的操作数除以右边的操作数后的余数赋给左边的操作数
&=
|=
^=
<<=
=
/
contract OperatorsRelational {
//init
uint public a=10;
uint public b= 20;
uint public c = 30;
uint public d = 40;
uint public e = 50;
bytes1 public h1 = 0xa0;
bytes1 public h2 = 0xb0;
bytes1 public h3 = 0xc0;
bytes1 public h4 = 0xd0;
bytes1 public h5 = 0xe0;
//hanshu
function getResult()public {
a+=10;//20
b-=10;//10
c=10;//300
d/=10;//4
e%=10;//0
h1&=0x10;//10100000 & 00010000 00000000 0x00
h2|=0x10;//10110000 | 00010000 10110000 0xb0
h3^=0x10;//11000000 ^ 00010000 11010000 0xd0
h4<<=2;// 11010000 01000000 0x40
h5>>=2;// 11100000 00111000 0x38
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/*
赋值运算符
=
+=
-=
*=
/=
%= 将左边的操作数除以右边的操作数后的余数赋给左边的操作数
&=
|=
^=
<<=
>>=
*/
contract OperatorsRelational {
//init
uint public a=10;
uint public b= 20;
uint public c = 30;
uint public d = 40;
uint public e = 50;
bytes1 public h1 = 0xa0;
bytes1 public h2 = 0xb0;
bytes1 public h3 = 0xc0;
bytes1 public h4 = 0xd0;
bytes1 public h5 = 0xe0;
//hanshu
function getResult()public {
a+=10;//20
b-=10;//10
c*=10;//300
d/=10;//4
e%=10;//0
h1&=0x10;//10100000 & 00010000 00000000 0x00
h2|=0x10;//10110000 | 00010000 10110000 0xb0
h3^=0x10;//11000000 ^ 00010000 11010000 0xd0
h4<<=2;// 11010000 01000000 0x40
h5>>=2;// 11100000 00111000 0x38
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/*
条件运算符,三元运算符,它首先计算表达式,然后检查对应于true,false的返回条件
语法
if condition true ? then A:else B
*/
contract OperatorsRelational {
//hanshu
function test(uint a,uint b)public pure returns(uint) {
uint result =a>b?a-b:a+b;
return result;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/*
条件运算符,三元运算符,它首先计算表达式,然后检查对应于true,false的返回条件
语法
if condition true ? then A:else B
*/
contract OperatorsRelational {
//hanshu
function test(uint a,uint b)public pure returns(uint) {
uint result =a>b?a-b:a+b;
return result;
}
}
/*
delete a
delete 除适用于值类型变量外,也适用于数组和结构体,但对映射无效,
以上语句的结果是将变量a类型的初始值赋值给a
对于整数变量 相当于a=0;
对于动态数组,相当于重置为数组长度为零的数组
对于静态数组,相当于将数组中的所有值重置为初始值
对于数组,delete a[x]仅删除数组索引x处的元素,其他的元素和长度不变
对于结构体,相当于将结构体中的所有属性(成员)重置。
运算符优先级
1 a++ b-- New 数组下标 对象成员 函数调用 圆括号
2 ++a --b - delete ! ~
3 **
4 * / %
5 + -
6 << >>
7 &
8 ^
9 |
10 < > >= <=
11 == !=
12 &&
13 ||
14 ? : = |= ^= &= <<= >>= += -= *= /= %=
15 ,
*/
/*
delete a
delete 除适用于值类型变量外,也适用于数组和结构体,但对映射无效,
以上语句的结果是将变量a类型的初始值赋值给a
对于整数变量 相当于a=0;
对于动态数组,相当于重置为数组长度为零的数组
对于静态数组,相当于将数组中的所有值重置为初始值
对于数组,delete a[x]仅删除数组索引x处的元素,其他的元素和长度不变
对于结构体,相当于将结构体中的所有属性(成员)重置。
运算符优先级
1 a++ b-- New 数组下标 对象成员 函数调用 圆括号
2 ++a --b - delete ! ~
3 **
4 * / %
5 + -
6 << >>
7 &
8 ^
9 |
10 < > >= <=
11 == !=
12 &&
13 ||
14 ? : = |= ^= &= <<= >>= += -= *= /= %=
15 ,
*/