Skip to content

Instantly share code, notes, and snippets.

@raj-pranav
Created January 25, 2022 19:36
Show Gist options
  • Save raj-pranav/000be584c93771d2fccbced55de29313 to your computer and use it in GitHub Desktop.
Save raj-pranav/000be584c93771d2fccbced55de29313 to your computer and use it in GitHub Desktop.
Operators in Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
uint constant T = 10;
contract Arithmetic_Operation {
uint x = 10;
uint y = 20;
int p = -5;
int q = 11;
// Addition : (both operands should be either uint `OR` int)
uint sum_1 = x + y;
int sum_2 = p + q;
// Subtraction : (both operands should be either uint `OR` int)
// uint public sub1 = y - x; -> this is not possible as result would be -10 (a int type)
uint sub1 = y - x;
int sub2 = p - q;
int Un = -q; // Applicable only on signed integer type : int
// Multiplication : (both operands should be either uint `OR` int)
uint mul_1 = x * y;
int mul_2 = p * q;
// Divison : (both operands should be either uint `OR` int)
uint div_1 = x / y; // div_1 = 0
int div_2 = p / q; // div_2 = 0
uint div_3 = (x+1)/4; // div_3 = 2
// Modulo : Modulo operation outputs the remainder (both operands should be either uint `OR` int)
uint mod_1 = x % y; // mod_1 = 10
int mod_2 = p % q; // mod_2 = -5
// Exponent : (both operands should be either uint `OR` int)
uint exp_1 = x ** y; // exp_1 = 100000000000000000000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment