DEV Community

Cover image for How to Create Arithmetic Operations Smart Contract in Blockchain Using Solidity
Dauda Lawal
Dauda Lawal

Posted on • Originally published at Medium

How to Create Arithmetic Operations Smart Contract in Blockchain Using Solidity

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 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 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.
// 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;
        }
    }
Enter fullscreen mode Exit fullscreen mode
  1. Compile the Smart Contract

Open Remix IDE and create a new file named ArithmeticContract.sol. Paste the Solidity code into Remix and compile the smart contract using the Solidity compiler.

  1. 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.

  2. 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. 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 to enhance your skills further and connect with like-minded individuals in the blockchain space.

Happy coding!

Top comments (1)

Collapse
 
amustafa16421 profile image
Mustafa_A

In what sense is this a contract?
I expected a contract being made by two parties which will be recorded on a block-chain.

This reminds me more of Design by contract / unit testing.
Can you please elaborate.