Created
January 26, 2022 18:35
-
-
Save raj-pranav/ff36a69ba2598df5f0aa4ebee38287e0 to your computer and use it in GitHub Desktop.
Operators in Solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract bitwise_operator { | |
bytes1 x = 0x16; // bit representation: 00010110 | |
bytes1 y = 0x11; // bit representation: 00010001 | |
// Bitwise 'AND' | |
bytes1 byte_and = x & y; // 00010110 & 00010001 -> 00010000 -> in byte (0x10) | |
// Bitwise 'OR' | |
bytes1 byte_or = x | y; // 00010110 | 00010001 -> 00010111 -> in byte (0x17) | |
// Bitwise XOR | |
bytes1 byte_xor = x ^ y; // 00010110 ^ 00010001 -> 00000111 -> in byte (0x07) | |
// Bitwise NOT | |
bytes1 byte_not_x = ~x ; // ~00010110 -> 11101001 -> in byte (0xE9) | |
bytes1 byte_not_y = ~y ; // ~00010001 -> 11101110 -> in byte (0xEE) | |
// Bitwise Shift Right | |
bytes1 byte_right_shift = x >> 2; // 00010110 >> 2 -> 00000101 -> in byte (0x05) | |
// Bitwise Shift Left | |
bytes1 byte_left_shift = x << 2; // 00010110 << 2 -> 01011000 -> in byte (0x58) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment