mailwal

Computing the max without if statements | RareSkills Animated Explainer

RareSkills

Computing the max without `if` statements in Solidity

A frequent operation is determining which of two unsigned integers is greater than the other.

We might typically write this as

We could also do it in one line with `max = x > y ? x : y;`.

However, this introduces a branch at the bytecode level, which increases the gas cost in Solidity.

Using a few clever bit manipulation tricks, we can compute the max without a conditional statement.

Learn how to do this in our latest video.

Looking for more gas optimization tricks?

Read our Gas Optimization Book

LinkedIn icon
Twitter icon
RareSkills Logo

Copyright (C) 2026 RareSkills.io. All rights reserved.
You are receiving this email because you opted in via our website.

Our mailing address is:
RareSkills.io 1887 Whitney Mesa Dr # 1223 Henderson, NV 89014-2069 USA