区块链solidity学习笔记(一)

一,字符输入输出与更改

pragma solidity ^0.4.0;
contract helloworld{
    
    
    string myname="jack";
    function getname() public view returns(string){
    
    
        return myname;
    }
    function changename(string _newname)public view returns(string){
    
    
        myname=_newname;
        return _newname;
    }
}

Tips:
1,函数定义语句返回为 returns
2,参数修改输入为非数值和布尔值时须使用双引号(英文双引号)

二,真假bool值

pragma solidity ^0.4.0;
contract booltest{
    
    
    bool _a;
    int num1=200;
    int num2=100;
    function getbool() returns(bool){
    
    
        return _a;// 默认返回是false
    }
    function getbool1() returns(bool){
    
    
        return !_a;// !表示 true 变 false, false 变 true
    }
    function panduan() returns(bool){
    
    
        return num1==num2;//  false
    }
     function panduan1() returns(bool){
    
    
        return num1!=num2;//  true
    }
    // && || 与 或
    function yu() returns(bool){
    
    
        return (num1==num2) && true;// false && true =false
    }
    function yu1() returns(bool){
    
    
        return (num1!=num2) && true;// true && true = true
    }
    function huo() returns(bool){
    
    
        return (num1==num2) || true;//false || true = true
    }
    function huo1() returns(bool){
    
    
        return (num1!=num2) || true;//true || true = true
    }
    
}

Tips:
1,默认bool返回是false

三,整形特征与运算

pragma solidity ^0.4.0;
contract math{
    
    
    function add(uint a,uint b) pure public returns(uint){
    
    
        return a+b;
    }
    function jian(uint a,uint b) pure public returns(uint){
    
    
        return a-b;
    }
    function chen(uint a,uint b) pure public returns(uint){
    
    
        return a*b;
    }
    function chu(uint a,uint b) pure public returns(uint){
    
    
        return a/b;
    }
    function yu(uint a,uint b) pure public returns(uint){
    
    
        return a+b;
    }
    function min(uint a,uint b) pure public returns(uint){
    
    
        return a**b;// a的b次幂
    }
    
}

Tips:
1,pure view不消耗gas
2,int定义的参数可正可负,uint定义的参数只能取正
3,a**b代表a的b次幂

四, 底层位运算

pragma solidity ^0.4.0;
contract weiyunsuantest{
    
    
    uint8 a=3;// 00000011
    uint8 b=4;// 00000100
    function weiyu() view public returns(uint){
    
    
        return a & b; // 00000000 = 0  与
    }
    function weihou() view public returns(uint){
    
    
        return a | b; //  00000111 = 7  或
    }
    function weifang() view public returns(uint){
    
    
        return ~a; //  11111100 = 252  取反
    }
    function weiyihou() view public returns(uint){
    
    
        return a ^ b; // 00000111 = 7  异或
    }
    function weizuoyi() view public returns(uint){
    
    
        return  a << 1; // 00000110 = 6  左移
    }
    function weiyuoyi() view public returns(uint){
    
    
        return a >> 1; //  00000001 = 1   右移
    }
}

Tips:
1,异或:不同出1,相同出0

猜你喜欢

转载自blog.csdn.net/m0_47282648/article/details/109921851