ETHEREUM SMART CONTRACTS DEVELOPMENT

Solidity Fundamentals

Operators: Arithmetic, Logical, Comparison, Assignment, Bitwise, Conditional

Ferdi Kurt
Coinmonks
Published in
2 min readDec 9, 2020

--

In this section, we will learn operators which are play vital role in any programming language. At the same time, operators that are used in Solidity are very similar to Javascript. We can group them in 6 prominent categories;

  1. Arithmetic Operators
  2. Logical Operators
  3. Comparison Operators
  4. Assignment Operators
  5. Bitwise Operators
  6. Conditional Operators

Arithmetic Operators

These operators are used to perform arithmetic-mathematical operations.

Logical Operators

Combining two or more conditions requires the usage of logical operators.

Comparison Operators

These operators are used to compare two values.

Assignment Operators

For assigning value to a variable, solidity supports assignment operators below.

Bitwise Operators

These operators used to perform bit-level operations like AND, OR, XOR, NOT etc.

Conditional Operators

The conditional (ternary) operators is operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as a shortcut for the if statement.

Unsigned math operations should be carefully examined. There are some scenarios that need to be watched for as they can cause havoc in production, for instance, multiplication of two unsigned integers can cause overflow, or subtraction of two unsigned integers can result in underflow.

Later in the contract security section, we will handle these type of scenarios using open source libraries like Open Zeppelin’s SafeMath.

In the next part, we will be working on reference types such as arrays, structs and mapping. Thanks for reading.

Feel free to ask any question.

Stay safe, do good work, and keep in touch!

Ferdi Kurt

--

--