Bitwise Operations in Solidity

Bitwise operations in Solidity are operations that manipulate individual bits in binary numbers. Solidity is a high-level programming language designed for implementing smart contracts on blockchain platforms like Ethereum. It supports various bitwise operations, including AND, OR, XOR, NOT, and bit shifts.

Here’s a brief overview of each operation:

1. Bitwise AND (&)

This operation compares each bit of the first number to the corresponding bit of the second number. If both bits are 1, the corresponding result bit is set to 1; otherwise, the result bit is set to 0.

uint256 a = 5; // binary: 101

uint256 b = 3; // binary: 011

uint256 result = a & b; // binary: 001, decimal: 1

2. Bitwise OR (|)

This operation compares each bit of the first number to the corresponding bit of the second number. If either bit is 1, the corresponding result bit is set to 1; otherwise, the result bit is set to 0.

uint256 a = 5; // binary: 101

uint256 b = 3; // binary: 011

uint256 result = a | b; // binary: 111, decimal: 7

3. Bitwise XOR (^)

This operation compares each bit of the first number to the corresponding bit of the second number. If the bits are different, the corresponding result bit is set to 1; otherwise, the result bit is set to 0.

uint256 a = 5; // binary: 101

uint256 b = 3; // binary: 011

uint256 result = a ^ b; // binary: 110, decimal: 6

4. Bitwise NOT (~)

This is a unary operation that inverts the bits of its operand. If the bit is 1, it is changed to 0; if it is 0, it is changed to 1.

uint256 a = 5; // binary: 000…101 (256 bits)

uint256 result = ~a; // binary: 111…010 (256 bits), decimal: (2^256 – 1) – 5

5. Left shift (<<)

This operation shifts the bits of a number to the left by a specified number of positions, filling the vacated positions with 0s.

uint256 a = 5; // binary: 101

uint256 result = a << 1; // binary: 1010, decimal: 10

6. Right shift (>>)

This operation shifts the bits of a number to the right by a specified number of positions, discarding the shifted bits.

uint256 a = 5; // binary: 101

uint256 result = a >> 1; // binary: 010, decimal: 2

Example:

Solidity




pragma solidity ^0.8.0;
  
contract BitwiseOperations {
    function andOperation(uint256 a, uint256 b) public pure returns (uint256) {
        uint256 result = a & b;
        return result;
    }
  
    function orOperation(uint256 a, uint256 b) public pure returns (uint256) {
        uint256 result = a | b;
        return result;
    }
  
    function xorOperation(uint256 a, uint256 b) public pure returns (uint256) {
        uint256 result = a ^ b;
        return result;
    }
  
    function notOperation(uint256 a) public pure returns (uint256) {
        uint256 result = ~a;
        return result;
    }
  
    function leftShift(uint256 a, uint256 shift) public pure returns (uint256) {
        uint256 result = a << shift;
        return result;
    }
  
    function rightShift(uint256 a, uint256 shift) public pure returns (uint256) {
        uint256 result = a >> shift;
        return result;
    }
}


Output:

 

Bytes in Solidity

In Solidity, the term “bytes” refers to a dynamically-sized byte array. Solidity provides two types of byte arrays: fixed-size arrays (called “bytesN”, where N is a number between 1 and 32) and dynamic arrays (simply called “bytes”).

Similar Reads

Endianness and Bytes Data Layout in Solidity

Endianness refers to the order in which bytes are stored in memory. Solidity follows the little-endian format, where the least significant byte (LSB) is stored at the lowest address, and the most significant byte (MSB) is stored at the highest address. Solidity stores bytes in contiguous memory slots, with each slot being 32 bytes wide. This arrangement allows efficient memory access and alignment....

Fixed-size Byte Arrays (bytesN)

...

Dynamically-size Byte Arrays

Fixed-size byte arrays have a specified length, ranging from 1 to 32 bytes. The notation “bytesN” is used to represent these arrays, where “N” is an integer representing the length of the array. These arrays are useful when you know the exact size of the data you are working with....

Bitwise Operations in Solidity

...

Array of Bytes = a little difference

Dynamic byte arrays do not have a fixed length, and their size can be changed during runtime. These arrays are simply represented by the “bytes” keyword....

Bytes as Function Arguments

...

Conversion between addresses and bytes20

Bitwise operations in Solidity are operations that manipulate individual bits in binary numbers. Solidity is a high-level programming language designed for implementing smart contracts on blockchain platforms like Ethereum. It supports various bitwise operations, including AND, OR, XOR, NOT, and bit shifts....

Advanced Operations with Bytes

...