Search code examples
optimizationbit-manipulationdivision

What's the fastest way to divide an integer by 3?


int x = n / 3;  // <-- make this faster

// for instance

int a = n * 3; // <-- normal integer multiplication

int b = (n << 1) + n; // <-- potentially faster multiplication

Solution

  • This is the fastest as the compiler will optimize it if it can depending on the output processor.

    int a;
    int b;
    
    a = some value;
    b = a / 3;