Skip to content

Commit 91a6bbf

Browse files
committed
optimize Math#average function
1 parent 66b8a32 commit 91a6bbf

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

contracts/utils/Math.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ library Math {
1010
* @param b second number
1111
*/
1212
function average(uint256 a, uint256 b) internal pure returns (uint256) {
13-
return (a >> 1) + (b >> 1) + (((a & 1) + (b & 1)) >> 1);
13+
unchecked {
14+
return (a >> 1) + (b >> 1) + ((a & 1) & (b & 1));
15+
}
1416
}
1517

1618
/**

0 commit comments

Comments
 (0)