VOOZH about

URL: https://www.geeksforgeeks.org/solidity/solidity-assignment-operators/

⇱ Solidity - Assignment Operators - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Solidity - Assignment Operators

Last Updated : 28 Apr, 2025

Solidity is a high-level, statically-typed programming language for Ethereum smart contracts. Python, JavaScript, and C++ impact it. Solidity has several variable and value assignment operators. Solidity supports the following types of operators:

  1. Simple Assignment Operator.
  2. Arithmetic Assignment Operator.
  3. Bitwise Assignment Operator.

Simple Assignment Operator

The simple assignment operator(=) assigns the value of the right-hand operand to the left-hand operand.

Example:

uint a;

a = 10; // Assigns the value 10 to the variable a.

Below is the Solidity program to implement a simple assignment operator.


Output:

👁 Simple Assignment Operator
 

Arithmetic Assignment Operators

These operators perform arithmetic operations and then assign the result to the left-hand operand. Solidity supports the following arithmetic assignment operators:

OperatorDenotationDescription
Addition Assignment+=Adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
Subtraction Assignment-=Subtracts the right-hand operand from the left-hand operand and assigns the result to the left-hand operand.
Multiplication Assignment*=Multiplies the right-hand operand by the left-hand operand and assigns the result to the left-hand operand.
Division Assignment/=Divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
Modulus Assignment %= Calculates the modulus (remainder) after dividing the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.

Example:

uint a = 10;

a += 5; // a = a + 5 => a = 15

a -= 3; // a = a - 3 => a = 12

a *= 2; // a = a * 2 => a = 24

a /= 4; // a = a / 4 => a = 6

a %= 5; // a = a % 5 => a = 1

Note: Solidity does not support increment (++) and decrement (--) operators, which are common in other programming languages.

Below is the Solidity program to implement Arithmetic Assignment Operators:


Output:

👁 Arithmetic Assignment Operator
 

Bitwise Assignment Operators

These operators perform bitwise operations and then assign the result to the left-hand operand. Solidity supports the following bitwise assignment operators:

OperatorDenotationDescription
Bitwise AND Assignment&=Performs a bitwise AND operation on the left-hand operand and the right-hand operand and assigns the result to the left-hand operand.
Bitwise OR Assignment |=Performs a bitwise OR operation on the left-hand operand and the right-hand operand and assigns the result to the left-hand operand.
Bitwise XOR Assignment^=Performs a bitwise XOR operation on the left-hand operand and the right-hand operand and assigns the result to the left-hand operand.
Left shift Assignment<<=Shifts the left-hand operand's bits to the left by the number of positions specified by the right-hand operand and assigns the result to the left-hand operand.
Right shift Assignment>>=Shifts the left-hand operand's bits to the right by the number of positions specified by the right-hand operand and assigns the result to the left-hand operand.

Example:

uint a = 0x0F; // 15 in hexadecimal

a &= 0x3C; // a = a & 0x3C => a = 0x0C (12 in decimal)

a |= 0x30; // a = a | 0x30 => a = 0x3C (60 in decimal)

a ^= 0x12; // a = a ^ 0x12 => a = 0x2E (46 in decimal)

a <<= 2; // a = a << 2 => a = 0xB8 (184 in decimal)

a >>= 1; // a = a >> 1 => a = 0x5C (92 in decimal)

Below is the Solidity program to implement bitwise assignment operators:


Output:

👁 Bitwise Assignment Operator
 
Comment
Article Tags:

Explore