MCPcopy Create free account
hub / github.com/argotorg/solidity / tryFormatPowerOfTwo

Function tryFormatPowerOfTwo

libsolutil/StringUtils.cpp:127–154  ·  view source on GitHub ↗

Try to format as N * 2**x

Source from the content-addressed store, hash-verified

125
126/// Try to format as N * 2**x
127std::optional<std::string> tryFormatPowerOfTwo(bigint const& _value)
128{
129 bigint prefix = _value;
130
131 // when multiple trailing zero bytes, format as N * 2**x
132 int i = 0;
133 for (; (prefix & 0xff) == 0; prefix >>= 8)
134 ++i;
135 if (i <= 2)
136 return std::nullopt;
137
138 // 0x100 yields 2**8 (N is 1 and redundant)
139 if (prefix == 1)
140 return {fmt::format("2**{}", i * 8)};
141 else if ((prefix & (prefix - 1)) == 0)
142 {
143 int j = 0;
144 for (; (prefix & 0x1) == 0; prefix >>= 1)
145 j++;
146 return {fmt::format("2**{}", i * 8 + j)};
147 }
148 else
149 return {fmt::format(
150 "{} * 2**{}",
151 toHex(toCompactBigEndian(prefix), HexPrefix::Add, HexCase::Mixed),
152 i * 8
153 )};
154}
155
156}
157

Callers 1

formatNumberReadableMethod · 0.85

Calls 2

toHexFunction · 0.85
toCompactBigEndianFunction · 0.85

Tested by

no test coverage detected