ReasonJun

Solidity : Operator / Shift operator 본문

Blockchain/Solidity

Solidity : Operator / Shift operator

ReasonJun 2023. 10. 15. 22:13
728x90

Operators in Solidity are used to perform various operations on data. There are many different types of operators, each with its own specific function.

 

The most common types of operators in Solidity are:

  • Arithmetic operators: These operators are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.
  • Comparison operators: These operators are used to compare two values and return a Boolean result (true or false).
  • Logical operators: These operators are used to combine Boolean expressions and return a Boolean result.
  • Bitwise operators: These operators are used to perform bitwise operations on binary data.
  • Assignment operators: These operators are used to assign values to variables.
  • Conditional operators: These operators are used to control the flow of execution in a function.

In addition to these common operators, Solidity also supports a number of other specialized operators, such as type conversion operators and range operators.

 

Here are some examples of how to use operators in Solidity:

// Arithmetic operators
uint256 a = 10;
uint256 b = 20;
uint256 c = a + b; // c is equal to 30
uint256 d = a - b; // d is equal to -10
uint256 e = a * b; // e is equal to 200
uint256 f = a / b; // f is equal to 0

// Comparison operators
bool g = a > b; // g is false
bool h = a < b; // h is true
bool i = a == b; // i is false

// Logical operators
bool j = g && h; // j is false
bool k = g || h; // k is true

// Bitwise operators
byte8 l = 0x12345678;
byte8 m = 0x9ABCDEF0;
byte8 n = l & m; // n is equal to 0x12345600
byte8 o = l | m; // o is equal to 0x9ABCDEF78
byte8 p = l ^ m; // p is equal to 0x8AC24778

// Assignment operators
uint256 q = 30;
q += 10; // q is now equal to 40
q -= 5; // q is now equal to 35
q *= 2; // q is now equal to 70
q /= 3; // q is now equal to 23

// Conditional operators
if (q > 20) {
    // Do something if q is greater than 20.
} else {
    // Do something if q is less than or equal to 20.
}

 

Shift Operator

 

 

The shift operators in Solidity are << and >>. They are used to shift the bits of a number to the left or right, respectively.

 

The << operator shifts the bits of a number to the left by the number of bits specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.

 

The >> operator shifts the bits of a number to the right by the number of bits specified in the second operand. New bits are filled with zeros. Shifting a value right by one position is equivalent to dividing it by 2, shifting two positions is equivalent to dividing by 4, and so on.

 

Shift operators can be used for a variety of purposes, such as:

  • Converting between different data types: For example, you can use the << operator to convert a uint256 to a byte8.
  • Extracting specific bits from a number: For example, you can use the >> operator to extract the least significant byte of a uint256.
  • Setting specific bits in a number: For example, you can use the | operator to set the most significant bit of a uint256.

Here are some examples of how to use shift operators in Solidity:

// Convert a uint256 to a byte8.
uint256 a = 10;
byte8 b = byte8(a << 24);

// Extract the least significant byte of a uint256.
uint256 c = 0x12345678;
byte d = byte(c >> 24);

// Set the most significant bit of a uint256.
uint256 e = 0;
e |= 1 << 255;

// Shift the bits of a number to the left by 10 positions.
uint256 f = 1 << 10; // f is equal to 1024

// Shift the bits of a number to the right by 10 positions.
uint256 g = 1024 >> 10; // g is equal to 1

Shift operators can be a powerful tool for manipulating binary data in Solidity. However, it is important to understand how they work before using them, as they can easily lead to errors if used incorrectly.

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : Modifier (pure, view)  (0) 2023.10.16
Solidity : Visibility  (0) 2023.10.15
Solidity : constant  (0) 2023.10.15
Solidity : Data Type (1)  (0) 2023.10.15
Solidity : storage, memory, calldata, stack  (0) 2023.10.15
Comments