Introduction to Technology | Solidity Programming Language: Boolean and Integer

HelloWorld

A smart contract is like a contract, and every code you write is the content of a contract. Therefore, once the contract is deployed, the content of the contract cannot be modified. It is like you can't modify it after you signed a contract with someone else. This is also based on the intangible nature of the blockchain. In the usual programming languages, standard output is generally used to print "Hello World". Solidity is a smart contract programming language. It is a different programming language. It is based on Ethereum and is used to write "contracts." "of. So instead of using "HelloWorld" as an example, use an entry example in Solidity's official website.

pragma solidity >=0.4.0 <0.6.0; contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } } 

 

The above code is a contract called SimpleStorage written in Solidity.

  1. The first line of code is used to specify the language version of Solidity. The pragma solidity is a fixed format, followed by the version number. The version number is determined by the range, which is equal to 0.4.0 and less than 0.6.0. On the Internet, you will often see the writing method of ^0.4.21, which is supported in versions earlier than 0.5.2. For example, if you write pragma solidity ^0.5.3 in the remix-ide environment, you will be prompted with a compilation error: browser/Untitled.sol:3:1: ParserError: Source file requires different compiler version (current compiler is 0.5.2+commit.1df8f40c .Emscripten.clang – note that nightly builds are considered to be strictly less than the released version.
  2. The second code creates a contract called SimpleStorage. Contract is the keyword and SimpleStorage is the contract name. This class definition similar to java, class Person{}, is easier to understand.
  3. The third line of code sets an unsigned integer storedData. This state variable will be stored in the blockchain, just like writing to the database for persistence and reading.
  4. Define the set method, assign the state variable storedData, where public is the key, and the modified set method can be called outside the contract.
  5. Define the get method, return the value of the state variable storedData, the writing of the return value is slightly different, the keyword is returns, not return, and the return value can be multiple, wrapped in parentheses. This is a simple contract written by Solidity, which is easy to understand for people with programming experience.

2. Boolean and integer

2.1 Boolean

The bool type is the same as other languages, and the value is true or false. The operation operations are !, ||, &&, !=, ==. Note that there is no |, &.

Pragma solidity >=0.4.22 <0.6.0; contract EgBool { bool isOne; bool isTwo; function operation() public { isOne = true; if (isOne){ // dosomething }

If (!isOne){ // dosomething }

If (isOne || isTwo ){ // dosomething }

If (isOne && isTwo ){ // dosomething }

If (isOne != isTwo ){ // dosomething }

If (isOne == isTwo ){ // dosomething } } }

2.2 Integer

Integers include unsigned uint and signed int. Each type has multiple lengths, such as uint8, uint64, int128, etc., and the length ranges from 8 to 256, with a difference of 8 lengths, 8, 16, 24, 32. …256. Where uint is the same as uint256, and int is the same as int256. There are three kinds of arithmetic operations: comparison, bit operation and arithmetic operation.

  • Comparison operation: <= , < , == , != , >= , >.
  • Bit operations: & (and), | (or), ^ (exclusive OR), ~ (not).
  • Arithmetic operations: +, -, *, /, %, (power, exponentiation), << (left shift), >> (right shift) where left and right shift operations, a<<b, can be understood as a is multiplied by 2 b to the power of a*2b, and the same right shifts a>>b to a/2**b.
 pragma solidity >=0.4.22 <0.6.0; contract EgInt { int i = 0; int8 i8 = -1; int256 i256 = 256; 

Uint ui = 0; uint ui8 = 1; uint256 ui256 = 256;

Function operation() public { if ( i < i8 || i <= i8 || i == i8 || i != i8 || i > i8 || i >= i8 ){ //dosomeing }

Int a; int b; int c; c = a & b; c = a | b; c = a ^ b; c = ~ b;

c = a + b; c = a - b; c = a * b; c = a / b; c = a % b; c = a << b; c = a >> b;

//c = a**b; uint d; uint e; uint f; f = d**e; //c = d**e; } }

 

note:

  1. Signed integers are not able to use the "**" operation.
  2. Signed and unsigned cannot be type-converted and cannot be parameterized at the same time, such as c = d + e.

Author: thank HPB Blue Lotus team finishing feeds.

Wang Xiaoming blog: http://wangxiaoming.com/

Wang Xiaoming: Founder of HPB core chain. More than ten years of experience in financial big data and blockchain technology development, he has participated in the creation of UnionPay big data. The main creative blockchain teaching video program "Ming Shuo" more than 30 issues, compiled the "Ethernet official website document Chinese version", and as the main author wrote the "blockchain development guide", in the Chinese blockchain community with ID "blue lotus "famous.