From 1eecbd69facbca74e5eb88886b81b79088a2948c Mon Sep 17 00:00:00 2001 From: JackLee <280147597@qq.com> Date: Tue, 26 Mar 2024 00:20:49 +0800 Subject: [PATCH 1/2] add bitmap and bitwise operations --- .../gas-optimization/bitmap.md | 48 +++++++++++++++++++ .../gas-optimization/constant.md | 3 ++ 2 files changed, 51 insertions(+) create mode 100644 docs/general/build/smart-contracts/gas-optimization/bitmap.md diff --git a/docs/general/build/smart-contracts/gas-optimization/bitmap.md b/docs/general/build/smart-contracts/gas-optimization/bitmap.md new file mode 100644 index 0000000000..0cd601c959 --- /dev/null +++ b/docs/general/build/smart-contracts/gas-optimization/bitmap.md @@ -0,0 +1,48 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Bitmap & Bitwise operations + +Storing data on the blockchain is extremely expensive. To decrease gas costs, innovative projects implement clever techniques. The method we're discussing today is often found in the source code of leading projects. + +Consider a `uint8`, represented in binary as `00000000`, where each bit can be either `0` or `1`. Traditionally, `1` is treated as true and `0` as false. This strategy allows for the effective and economical management of boolean values through bit manipulation. + +In Solidity, `1 << n` represents a bit shift operation, moving the number `1` left by `n` bits, with the right-hand vacated bits filled with `0`. For example, if `n` is `2`, then `1 << 2` results in `100`. + + +**Demo Code** + +The following demonstrates managing the same data using both a boolean array and bitwise operations. + +```solidity +contract Bitmap { + bool[8] boolArrayImplementation; + uint8 bitmapImplementation; + + function setBoolArrayData(bool[8] memory data) external { + boolArrayImplementation = data; + } + + function setBitmapData(uint8 data) external { + bitmapImplementation = data; + } + + // gas:35729 + function readBoolArray(uint8 index) external returns (bool) { + return boolArrayImplementation[index]; + } + + // gas:22366 + function readBitmap(uint indexFromRight) external returns (bool) { + uint256 bitAtIndex = bitmapImplementation & (1 << indexFromRight); + return bitAtIndex > 0; + } +} +``` + +In the `readBitmap` function, The `&` performs an `AND` operation on each bit of `bitmapImplementation` and `(1 << indexFromRight)`. The resulting bit is `1` only if both bits at that position are `1`, otherwise, it's `0`. This operation is commonly used to check if a specific bit is set to `1`. + +### Gas Optimization Suggestions: + +🌟Considering practical scenarios, bitwise operators can be used for managing certain variables to save gas. diff --git a/docs/general/build/smart-contracts/gas-optimization/constant.md b/docs/general/build/smart-contracts/gas-optimization/constant.md index d33e5a1da2..4c27947609 100644 --- a/docs/general/build/smart-contracts/gas-optimization/constant.md +++ b/docs/general/build/smart-contracts/gas-optimization/constant.md @@ -1,3 +1,6 @@ +--- +displayed_sidebar: generalSidebar +--- # Constant vs Immutable 1. `constant`: Declares a constant that must be initialized at the time of declaration and cannot be altered thereafter. From 9abc68b08c558b99696b755dca8f774c1856775f Mon Sep 17 00:00:00 2001 From: JackLee <280147597@qq.com> Date: Tue, 26 Mar 2024 00:24:49 +0800 Subject: [PATCH 2/2] update bitmap --- .../build/smart-contracts/gas-optimization/bitmap.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/general/build/smart-contracts/gas-optimization/bitmap.md b/docs/general/build/smart-contracts/gas-optimization/bitmap.md index 0cd601c959..916c311e4b 100644 --- a/docs/general/build/smart-contracts/gas-optimization/bitmap.md +++ b/docs/general/build/smart-contracts/gas-optimization/bitmap.md @@ -2,18 +2,18 @@ displayed_sidebar: generalSidebar --- -# Bitmap & Bitwise operations +# Bitmap & Bitwise operation Storing data on the blockchain is extremely expensive. To decrease gas costs, innovative projects implement clever techniques. The method we're discussing today is often found in the source code of leading projects. -Consider a `uint8`, represented in binary as `00000000`, where each bit can be either `0` or `1`. Traditionally, `1` is treated as true and `0` as false. This strategy allows for the effective and economical management of boolean values through bit manipulation. +Consider a `uint8`, represented in binary as `00000000`, where each bit can be either `0` or `1`. Traditionally, `1` is treated as true and `0` as false. This strategy allows for the effective and economical management of boolean value through bitwise operation. In Solidity, `1 << n` represents a bit shift operation, moving the number `1` left by `n` bits, with the right-hand vacated bits filled with `0`. For example, if `n` is `2`, then `1 << 2` results in `100`. **Demo Code** -The following demonstrates managing the same data using both a boolean array and bitwise operations. +The following demonstrates managing the same data using both a boolean array and bitwise operation. ```solidity contract Bitmap {