溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

solidity智能合約[9]-字節(jié)數(shù)組與運算

發(fā)布時間:2020-06-15 02:34:47 來源:網(wǎng)絡 閱讀:348 作者:jonson_jackson 欄目:開發(fā)技術

byte類型

有byte bytes1 bytes2 … bytes32
特殊的有byte == bytes1

后面的數(shù)字代表占了多少字節(jié)。1個字節(jié)在內(nèi)存中占了8位

性質(zhì)

固定字節(jié)數(shù)組不能修改長度和內(nèi)容

字節(jié)一般用16進制來存儲

16進制中的1個數(shù)字代表占了4位。

1
2
3
4
5
6
7
bytes1 public num1 = 0x6a;        //轉換為10進制:106
bytes2 public num2 = 0x6a6f;     //轉換為10進制:27247

bytes6 public num3 = 0x6a6f6e736f6e;

bytes1 public a = 0x6a;//轉換為2進制:0110   1010    
bytes1 public b = 0x6f;//轉換為2進制:0110   1111

字節(jié)可以有長度屬性

1
2
3
function getlength() view public returns(uint,uint,uint){
   return (num1.length,num2.length,num3.length);
}

字節(jié)可以比較大小

不同類型的字節(jié)也可以比較大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function  test1()  public view returns(bool){
   return  a>b;
}

function  test2() public view returns(bool){
   return  a>=b;
}

function  test3()  public  view returns(bool){
   return  a<b;
}
   function  test4() public   view returns(bool){
   return  a<=b;
}

function  test5() public view returns(bool){
   return  a==b;
}

   function  test6() public view returns(bool){
   return  a!=b;
}

function  test7() public view returns(bool){
   return num2 >num1;
}

字節(jié)可以進行位運算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 0110 1010
// 0110 1111

//&0110 1010   106    0x6a
//|0110 1111   111    0x6f
//^0000 0101   5      0x05
//~1001 0101   149    0x95
//<1101 0100   212    0xd4
//>0011 0101   53     0x35
function  weiTest1() public view returns(bytes1){
   return a & b;
}
  function  weiTest2() public view returns(bytes1){
   return a | b;
}
  function  weiTest3() public view returns(bytes1){
   return a ^ b;
}
  function  weiTest4() public view returns(bytes1){
   return ~a;
}

  function  weiTest5() public view returns(bytes1){
   return a<<1;
}
  function  weiTest6() public view returns(bytes1){
   return a >>1;
}

完整代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
pragma solidity ^0.4.23;


contract  bytesTest{
   //0x6a6f6e736f6e
   bytes1 public num1 = 0x6a;        //106
   bytes2 public num2 = 0x6a6f;     //27247

   bytes6 public num3 = 0x6a6f6e736f6e;

    bytes1 public a = 0x6a;//0110   1010      106
    bytes1 public b = 0x6f;//0110   1111      111



   function getlength() view public returns(uint,uint,uint){
       return (num1.length,num2.length,num3.length);
   }

   // function changeLength()  public {
   //     num1.length = 9;
   // }

   function  test1()  public view returns(bool){
       return  a>b;
   }

    function  test2() public view returns(bool){
       return  a>=b;
   }

    function  test3()  public  view returns(bool){
       return  a<b;
   }
       function  test4() public   view returns(bool){
       return  a<=b;
   }

   function  test5() public view returns(bool){
       return  a==b;
   }

       function  test6() public view returns(bool){
       return  a!=b;
   }

    function  test7() public view returns(bool){
       return num2 >num1;
   }


   // 0110 1010
   // 0110 1111

   //&0110 1010   106    0x6a
   //|0110 1111   111    0x6f
   //^0000 0101   5      0x05
   //~1001 0101   149    0x95
   //<1101 0100   212    0xd4
   //>0011 0101   53     0x35
    function  weiTest1() public view returns(bytes1){
       return a & b;
   }
      function  weiTest2() public view returns(bytes1){
       return a | b;
   }
      function  weiTest3() public view returns(bytes1){
       return a ^ b;
   }
      function  weiTest4() public view returns(bytes1){
       return ~a;
   }

      function  weiTest5() public view returns(bytes1){
       return a<<1;
    }
      function  weiTest6() public view returns(bytes1){
       return a >>1;
   }

}
  • 本文鏈接: https://dreamerjonson.com/2018/11/14/solidity-9/

  • 版權聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉載請注明出處!

solidity智能合約[9]-字節(jié)數(shù)組與運算

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI