Skip to content
Last updated: March 11, 2025

SCWE-066: Incorrect Handling of Bitwise Operations

Stable Version v0.0.1

This content is in the version-(v0.0.1) and still under active development, so it is subject to change any time (e.g. structure, IDs, content, URLs, etc.).

Send Feedback

Relationships

Description

Bitwise operations (e.g., &, |, ^, <<, >>) can be efficient alternatives to arithmetic operations but are prone to errors if not used correctly. Misusing bitwise shifts can cause unintended value changes, integer overflows, or precision loss. Solidity lacks native overflow checks for bitwise shifts, making incorrect handling particularly dangerous in financial calculations or cryptographic functions.

Remediation

  • Ensure bitwise shifts do not exceed the size of the data type (e.g., shifting a uint8 left by 8+ bits).
  • Validate input ranges before performing shifts to prevent overflow or precision loss.
  • Avoid unnecessary bitwise operations in financial calculations unless explicitly needed.

Vulnerable Contract Example

contract Example {
    function shiftLeft(uint8 input) public pure returns (uint8) {
        return input << 8;  // ❌ Shifting beyond `uint8` capacity leads to zero
    }

    function bitwiseAnd(uint8 a, uint8 b) public pure returns (uint8) {
        return a & b;  // ❌ Without proper validation, this could lead to unintended masking
    }
}
Why is this vulnerable?

  • input << 8 shifts a uint8 completely out of range, resulting in a value of 0.
  • a & b can unintentionally mask critical bits if inputs are not properly validated.

Fixed Contract Example

contract SecureExample {
    function safeShiftLeft(uint8 input) public pure returns (uint8) {
        require(input < 32, "Shift too large");  // ✅ Validate shift range
        return input << 2;  // ✅ Safe shift within `uint8` bounds
    }

    function safeBitwiseAnd(uint8 a, uint8 b) public pure returns (uint8) {
        require(a > 0 && b > 0, "Invalid input");  // ✅ Ensure inputs are meaningful
        return a & b;
    }
}

Why is this safe?

  • Restricts shift values to prevent unexpected overflows.
  • Ensures bitwise operations do not unintentionally modify values in an unintended way.
  • Protects contract logic from potential manipulation through poorly validated inputs.

By correctly handling bitwise operations, developers can avoid unintended computation errors and ensure mathematical correctness in smart contracts.