区块链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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沅_Yuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值