/* 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 } }
原文地址:https://www.cnblogs.com/eilinge/p/9957331.html
时间: 2024-10-07 19:52:13