Ethernet Square to learn the next day

And a Boolean type logic or

// 逻辑非!
// 逻辑与&&
// 逻辑或||
// 不等于!=
// 等于==
pragma solidity ^0.4.5;

contract pcl {
    int _a;
    int _b;
    bool _c;

    function pcl() {
        _a=1;
        _b=2;
        _c=true;
    }

    function luojifei() constant returns (bool){
        return !_c;
    }

    function luojiyu() constant returns (bool){
        return _a == _b && _c;
    }

    function luojihuo() constant returns (bool){
        return _a==_b||_c;
    }
}

With XOR and NOR

pragma solidity ^0.4.5;

contract pcl {
    uint8 a;
    uint8 b;
    
    function pcl(){
        a=2;//0000 0010
        b=4;//0000 0100
    }
    
    function yu() constant returns (uint8){
        return a&b;//0000 0000->0
    }

    function huo() constant returns (uint8){
        return a|b;//0000 0110->6
    }
    
    function fei() constant returns (uint8){
        return ~a;//1111 1101->253
    }

    function yihuo() constant returns (uint8){
        return a^b;//0000 0110->6     只要不一样就返回true
    }
}

Type integer overflow

pragma solidity ^0.4.5;

contract pcl {
    uint8 _a;
    
    function pcl(uint8 a){
        _a=a;
    }
    
    function set(uint8 a){
        _a=a;
    }
    
    function get()constant returns (uint8){
        return _a;
    }
 
 
    function testvar() constant returns (uint16){
        
        uint16 a;
        for (uint16 i=0 ; i<=255 ; i++){
            a=i;
        }
        return a;
    }  


    function testvar_1() constant returns (uint16){
        
        uint16 a;
        for (var i=0 ; i<=255 ; i++){      //当使用var的时候,默认判断第一个传给变量的大小。比如0,用int8就能装下,那么i这个时候就是int8,就不能装下256,会溢出,然后重置导致死循环。
            a=i;
        }
        return a;
    }  
}

Of this contract and the balance

pragma solidity ^0.4.5;

contract pcl {
    address _owner;
    address _conAdd;

    function pcl() {
        _owner=msg.sender;
        _conAdd=this;
    }

    function getBalance(string panduan) constant returns (uint){
        if (keccak256(panduan)==keccak256("owner")){       //使用keccak256加密方法,对string进行编码后再比较。为什么不能直接字符串比较。**Mark**
            return _owner.balance;
        }else{
            return _conAdd.balance;
        }
    }
    function getAdd(string panduan) constant returns (address){
        if (keccak256(panduan)==keccak256("owner")){
            return _owner;
        }else{
            return _conAdd;
        }
    }
}

Transfer function send and transfer

pragma solidity ^0.4.5;

contract pcl {
    address _a;

    function pcl() {
        _a=0x70DCe49AF9485cF361d9B67a1B78777E624f4eA0;
    }

    function trans() payable{
        _a.send(msg.value);             //_a.send是当前调用合约者,向_a这个地址转账。
    }
    
    function tran_2() payable {
        _a.transfer(msg.value);         //send和transfer的区别是,transfer当发送者钱不够时,就停止转账。而send回返回一个false。(不知道为什么我测试时向合约转账就会返回false)
    }
}
Released two original articles · won praise 1 · views 120

Guess you like

Origin blog.csdn.net/xiaoyue2019/article/details/105312119