Bitwise operator and bitshifting

·

3 min read

It is not uncommon to see bitwise operator and bit shifting in solidity code. This article explained it through a simplified example. In Solidity, bitwise operators and bit shifting work similarly to other programming languages. Let's go over the basic concepts:

Bitwise Operators:

  1. AND (&): Performs a bitwise AND operation between two operands. Each bit of the result is 1 if the corresponding bits of both operands are 1, otherwise, it's 0.

  2. OR (|): Performs a bitwise OR operation between two operands. Each bit of the result is 0 if both corresponding bits of the operands are 0, otherwise, it's 1. 3.

  3. XOR (^): Performs a bitwise XOR (exclusive OR) operation between two operands. Each bit of the result is 1 if the corresponding bits of the operands are different, otherwise, it's 0.

  4. NOT (~): Performs a bitwise NOT operation on a single operand. It flips each bit of the operand, i.e., if a bit is 1, it becomes 0, and if it's 0, it becomes 1. Bit Shifting:

Bit Shifting:

  1. Left Shift (<<): Shifts the bits of the left-hand operand to the left by a specified number of positions. It effectively multiplies the operand by 2 raised to the power of the number of positions shifted.

  2. Right Shift (>>): Shifts the bits of the left-hand operand to the right by a specified number of positions. It effectively divides the operand by 2 raised to the power of the number of positions shifted. Here's an example code snippet in Solidity demonstrating the usage of bitwise operators and bit shifting:

In this example, we have two functions: leftShift and rightShift.

The leftShift function takes a number and the number of positions to shift as inputs. It performs a left shift (<<) on the number by the specified positions. The result is then returned.

The rightShift function is similar but performs a right shift (>>) instead.

Here's how you can use these functions:

pragma solidity ^0.8.0;

contract BitShiftExample {
    function leftShift(uint number, uint positions) public pure returns (uint) {
        uint result = number << positions;
        return result;
    }

    function rightShift(uint number, uint positions) public pure returns (uint) {
        uint result = number >> positions;
        return result;
    }
}

pragma solidity ^0.8.0;

contract BitShiftUsage {
    BitShiftExample private bitShiftExample;

    constructor() {
        bitShiftExample = new BitShiftExample();
    }

    function example() public view returns (uint, uint) {
        uint number = 10;  // Binary: 1010
        uint leftShifted = bitShiftExample.leftShift(number, 2);  // Result: 101000
        uint rightShifted = bitShiftExample.rightShift(number, 1);  // Result: 101
        return (leftShifted, rightShifted);
    }
}


// In the example function, we create an instance of the BitShiftExample contract and call the leftShift and rightShift functions on a number 10. The result of the left shift by 2 positions is 101000, and the result of the right shift by 1 position is 101.