Learn solidity to write smart contracts together - integer (uint and int)

foreword

Integers are generally used more, and the existence of integers will be seen in various contracts, so this type is also indispensable on the road of learning

surroundings:

The remix compiler clicks me to jump

text

We encounter many types of integer data in sol, so our sol provides two data types of integers:

Signed integer (int) : Here we can understand it as a positive and negative integer

Unsigned integer (uint) : unsigned integer, where only positive numbers and 0 exist

In order to have better memory control sol provides, int8, int16, int32, etc., unsigned integer types up to int256

The same is true for our unsigned integers, uint8, uint16, uint32...uint256

So what we need to pay attention to in the selection is that for 0~256 bits of data, we can use uint8, then we use int8 to store signed negative numbers to positive numbers -128~127. According to the actual situation, we will go up. Can.

Some students will be curious, what does this 8, 16, 32...256 mean?

Here is our binary, because our computer is composed of 0101 code, so the underlying storage logic is binary, then here 00000000 represents 0 in binary, 11111111 represents 256 in binary, careful students will find this number The number is eight, which is here, so our storage is stored like this

int and operator

There are three types of operators supported by integer types: comparison operators, bitwise operators, and arithmetic operators

comparison operator

<= (less than or equal to) < (less than) == (equal to) != (not equal) >= (greater than or equal to) > (greater than)

bitwise operators

&&(and) || (or) ^ (exclusive or) ~ (bit inversion)

arithmetic operators

+ - Unary operator "-" Unary operator "+" * / % ** << >>

Looking at it again, it is estimated that there is a problem with the operation of shifting left and right. Here is an explanation.

Left shift: a << b can be understood as a multiplied by 2 to the b power

Right shift: a >> b can be understood as a divided by 2 to the b power

The operator is used as follows:

uint256 public _uintNum = 12345; //Integer result: 12345

uint256 public _uintNum1 = _uintNum + 1; //addition result: 12346

uint256 public _uintNum2 = 2**2; // exponent result: 4

uint256 public _uintNum3 = 7 % 2; //Remainder result: 1

bool public _boolnum = _ uintNum2 > _uintNum3;//Judgment result: true

Precautions

Declaration method

int8 a = -1;

uint16 b = 1;

uint c ;

int d ;

In integer operations, if it encounters a constant, it will be truncated. If it is a constant, it will not. A constant is a quantity that has a value when it is defined and does not change. If it encounters 0, it is illegal and an error will be reported.

The left and right displacement is to move the binary position. Now we have a binary number such as 10000, which is 32 in decimal, then we move right to 1000, and the same is true for left shift.

You can't think negatively, you can't become negative

Negative numbers cannot be exponentiated, that is "**"

pragma solidity ^0.4.23; //表示编译器的版本,^代表大版本为4即可
contract UintTest{ //创建一个合约 自定义合约名为UintTest
uint a = 10; // 定义一个变量a为10
uint b = 2; // 定义一个变量b为2
function getUint() public view returns(uint){// 定义一个函数 函数名为getUint 权限为公共 只读 返回一个uint类型 
uint sum = a ** b + 3 % 2;// 定义一个uint类型使用运算符a的b次方加3取余2最后的结果为101
return sum;// 返回sum
}
}

 

Guess you like

Origin blog.csdn.net/qq_57309855/article/details/127349858