ethereum(以太坊)--整型(int)
Posted eilinge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ethereum(以太坊)--整型(int)相关的知识,希望对你有一定的参考价值。
/* uint8 uint16 ...uint256 int8 int16 int24 ..int256 uint => uint256 int => int256 int8 有符号 +1 ,-3 uint 无符号 1,2 int8 8(位) 1 111 1111 ~ 0 111 1111 - (1+2+4+8+16+32+64) ~ + (1+2+4+8+16+32+64) -127 ~ 127 255(127*2+0) uint8 0000 0000 ~ 1111 1111 (0~255) */ pragma solidity ^0.4.0; contract Ballot { int8 _a; function set(int8 a){ _a = a; } function get() constant returns(int8){ return _a; } } pragma solidity ^0.4.0; contract Ballot { function test1() constant returns(uint){ uint8 a = 100; var b = a ; return b; } function test2() constant returns(uint){ uint a; //var == uint8 /* when i <= 255, i++ than i = 256, but max of uint8 is 255,so i cannot continue */ for (uint i=0;i<=255;i++){ a = i; } return a; } } pragma solidity ^0.4.0; contract Ballot { uint8 a = 3; uint8 b = 7; //&, | ,^(异或),~(非) function yu() constant returns(uint){ return a & b; //0000 0011 //0000 0111 //0000 0011 = 3 全1出1 } function huo() constant returns(uint){ return a | b; //0000 0011 //0000 0111 //0000 0111 = 7 有1出1 } function yihuo() constant returns(uint){ return a ^ b; //0000 0011 //0000 0111 //0000 0100 = 4 相同出0 } function fei() constant returns(uint){ return ~a; //0000 0011 //1111 1100 = 255-3 } } pragma solidity ^0.4.0; contract Ballot { uint8 a = 7; uint8 b = 3; function add() constant returns(uint){ return a + b; } function sub() constant returns(uint){ return a - b; } function mul() constant returns(uint){ return a * b; } function div() constant returns(uint){ return a / b; //除(取整) 2 } function mm() constant returns(uint){ return a % b; //取余 1 } } pragma solidity ^0.4.0; contract Ballot { uint8 a = 3; //0000 0011 function qiumi(uint8 b) constant returns(uint){ return a ** b; } function leftShit(uint8 b) constant returns(uint){ return a << b; //0000 0011 b=2 ->0000 1100 = 12 } function rightShit(uint8 b) constant returns(uint){ return a >> b; //0000 0011 b=2 ->0000 0000 = 0 } }
以上是关于ethereum(以太坊)--整型(int)的主要内容,如果未能解决你的问题,请参考以下文章
区块链 | 智能合约Ethereum源代码- 以太坊RPC通信实例和原理代码分析(上)
(转)写文章 以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘