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.
- Opinion | Talking about platform currency, which makes digital currency extension more abundant
- Coinbase Report: The number of top academic students studying cryptocurrency courses doubled in 2019 compared to last year
- Anti-Ruibo selling sentiment is getting worse and stronger, investors want to fork and take over XRP
- 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.
- 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.
- 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.
- 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.
- 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:
- Signed integers are not able to use the "**" operation.
- 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.
We will continue to update Blocking; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Britain's Brexit "great change", bitcoin but "failed" fell below 10,000 US dollars
- Bakkt will open Bitcoin recharge service to users on September 6 to prepare for futures trading
- Can Bitcoin replace gold, currency, and financial systems?
- US traders push the bitcoin bull market to rebound, Asian traders still rely on wait and see
- Bitcoin lost $10,000, and the trend is finally here.
- Viewpoint | Blockchain don't pass the pass?
- The fundraising of $1.7 billion has been low-key, and will the upcoming Telegram blockchain be supervised?