Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 136.9MB ·虚拟内存 1437.2MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
Solidity 支持的逻辑运算符,如下表所示:
假设变量A的值为10,变量B的值为20。
| 序号 | 运算符与描述 |
|---|---|
| 1 | && (逻辑与)如果两个操作数都非零,则条件为真。例: (A && B) 为真 |
| 2 | || (逻辑或)如果这两个操作数中有一个非零,则条件为真。例: (A || B) 为真 |
| 3 | ! (逻辑非)反转操作数的逻辑状态。如果条件为真,则逻辑非操作将使其为假。例: ! (A && B) 为假 |
下面的代码展示了如何使用逻辑运算符。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SolidityTest {
uint storedData;
constructor() {
storedData = 10;
}
function getResult() public pure returns(string memory){
uint a = 2; // 局部变量
uint b = 2;
uint result = a & b; // 位与
return integerToString(result);
}
function integerToString(uint _i) internal pure
returns (string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (_i != 0) {
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);// 访问局部变量
}
}
运行上述程序,输出结果:
0: string: 3
Solidity 支持的位运算符,如下表所示:假设变量A的值为2,变量B的值为3。序号运算符与描述1& (位与)对其整数参数的每个位执行位与操作。例: (A & B) 为 2.2| (位或)对其整 ...