3. 数组
1字节(Byte) = 8位 (bit), bytes32 = 256位,bytes1 实质上就等于 int8
3.1 固定长度的字节数组
固定长度的数组一旦被定义就无法再更改,并且长度在一开始就会被显式定义
我们再来创建一个新的文件用来编写代码
pragma solidity ^0.4.0;
contract BytesArray {
bytes2 num = 125;
//如下返回值为2
function getLength() view public returns(uint)
{
return num.length;
}
}
字节数组无法进行基本运算,但是可以比较
bytes1 public a = 0x7a; //0111 1010 218
bytes1 public b = 0x68; //0110 1000 184
//false
function compare() returns(bool) {
return a==b;
}
//true
function compare2() returns(bool) {
return a!=b;
}
//true
function compare3() returns(bool) {
return a>b;
}
字节数组还支持其他一些逻辑运算,具体计算结果如下,读者也可以自行运算比较文章来源:https://www.toymoban.com/news/detail-405136.html
bytes1 public a = 0x7a; //0111 1010 218
bytes1 public b = 0x68; //0110 1000 184
--
//0x68
function weiyu() view public returns(bytes1) {
return a & b;
}
//0x7a
function weihuo() view public returns(bytes1) {
return a | b;
}
//0x85
function weifan() view public returns(bytes1) {
return ~a;
}
//0x12
function weiyihuo() view public returns(bytes1) {
return a ^ b;
}
//0xf4
function zuoyi() view public returns(bytes1) {
return a << 1;
}
//0x3d
function youyi() view public returns(bytes1) {
return a >> 1;
}
3.2 动态字节数组
动态数组的具体操作如下,读者可手动编译尝试体验文章来源地址https://www.toymoban.com/news/detail-405136.html
pragma solidity ^0.4.0;
contract Dynamicbytes {
//定义一个空数组 :0x0000
bytes public name = new bytes(2);
//给空数组中填充变量 :0x7a68
fun
到了这里,关于以太坊智能合约语言Solidity - 3 数组的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!