How to Create Arithmetic Operations Smart Contract in Blockchain Using Solidity
Tags: javascript, solidity, blockchain, programming
Published: 2024-03-03 07:55:41

In blockchain development, where precision and efficiency are paramount, Solidity smart contracts play a crucial role in executing various operations. As part of the BIH Blockchain Bootcamp sponsored by Arbitrum, we present the "ArithmeticContract" - a Solidity smart contract that explores fundamental arithmetic operations on the Ethereum blockchain.

## What is Solidity Programming Language used for?

[Solidity](https://medium.com/r?url=https%3A%2F%2Fbitkan.com%2Flearn%2Fwhat-is-solidity-used-for-and-advantages-of-solidity-programming-9227) is a high-level, object-oriented programming language used to write smart contracts on blockchain platforms like Ethereum. It's used to [create smart contracts](https://medium.com/r?url=https%3A%2F%2Fwww.simplilearn.com%2Ftutorials%2Fblockchain-tutorial%2Fwhat-is-solidity-programming%23%3A%7E%3Atext%3DIt%27s%2520used%2520to%2520create%2520smart%2CEthereum%2520Virtual%2520Machine%2520%28EVM%29.) that implement business logic and generate a chain of transaction records in the blockchain system. It acts as a tool for creating machine-level code and compiling it on the Ethereum Virtual Machine (EVM).

## Overview of ArithmeticContract

The ArithmeticContract is a simple yet powerful Solidity smart contract designed to perform basic arithmetic operations on unsigned integers. Developed using Solidity version 0.8.2, the contract offers functions to identify odd and even numbers, as well as to calculate the most significant bit of a given number.
Let's dive into the key components of the contract and understand how each function operates.

### Prerequisites
Before you begin, ensure you have the following prerequisites:
1. Solidity Compiler: Install a Solidity compiler (version 0.8.2) to compile your smart contract code.

2. Remix IDE: Set up an integrated development environment (IDE) such as Remix for writing, compiling, and deploying smart contracts.

## Smart Contract Functions
1. `isOdd`
The `isOdd` function checks whether a given unsigned integer is an odd number. It employs the modulo operator to determine whether the number is divisible by 2 or not.

2. `isEven`
Contrastingly, the `isEven` function checks if a given unsigned integer is an even number. Similar to `isOdd`, it uses the modulo operator to assess divisibility by 2.

3. `mostSignificantBit`
The `mostSignificantBit` function calculates the position of the most significant bit (MSB) in the binary representation of a given unsigned integer. This function utilizes bitwise shifting to identify the position of the MSB efficiently.

## Getting Started

1. Create a New Solidity File
Open your preferred code editor and create a new file named `ArithmeticContract.sol`. Copy the provided Solidity code into this file.
```solidity
// SPDX-License-Identifier: GPL-3.0
    // SPDX-License-Identifier: GPL-3.0
    pragma solidity >=0.8.2 <0.9.0;

    contract ArithmeticContract {
        // Function for finding Odd number
        function isOdd(uint256 _number) public pure returns (bool) {
            return _number % 2 != 0;
        }

        // Function for finding even number
        function isEven(uint256 _number) public pure returns (bool) {
            return _number % 2 == 0;
        }
        
        // function for finding bit digits

        function mostSignificantBit(uint256 _number) public pure returns (uint8) {
            uint8 msb = 0;
            while (_number > 0) {
                _number = _number >> 1;
                msb++;
            }
            
            return msb;
        }
    }
```

2. Compile the Smart Contract

Open [Remix IDE](https://remix.ethereum.org/) and create a new file named `ArithmeticContract.sol`. Paste the Solidity code into Remix and compile the smart contract using the Solidity compiler.

3. Deploy the Smart Contract
Deploy the compiled smart contract on a testnet or local blockchain within Remix. If you're using a testnet, connect Remix to the appropriate network and deploy the contract.

4. Interact with the Smart Contract
Once deployed, you can interact with the smart contract using Remix's interface. Call the `isOdd`, `isEven`, and `mostSignificantBit` functions with different inputs to observe their outputs.

## Source Code and Collaboration

Explore the source code on GitHub at the [BIH_Blockchain_Bootcamp Project](https://github.com/Daslaw/BIH_Blockchain_Bootcamp/tree/main/ArithmeticContractProject). Feel free to contribute, provide feedback, or collaborate with others within the BIH Blockchain Bootcamp community.

## Connection Opportunities

As part of the BIH Training sponsored by Arbitrum, this project offers an excellent opportunity to connect with fellow blockchain developers, technical writers, and enthusiasts. Engage in discussions, share your experiences, and explore collaboration opportunities within the vibrant blockchain ecosystem.

## Conclusion
This project demonstrates Solidity coding and provides hands-on experience with blockchain development. [Join the BIH Blockchain Bootcamp community](https://bih.com.ng/) to enhance your skills further and connect with like-minded individuals in the blockchain space.

**Happy coding!**