Inspired by #2097 .
Try this code:
|
// Since the bit width of 0 is 0x0, special-case it instead of complicating the math even more.
|
|
int _Width = 3;
|
|
if (_Value != nullptr) {
|
|
// Compute the bit width of the pointer (i.e. how many bits it takes to be represented).
|
|
// Add 3 to the bit width so we always round up on the division.
|
|
// Divide that by the amount of bits a hexit represents (log2(16) = log2(2^4) = 4).
|
|
// Add 2 for the 0x prefix.
|
|
_Width = static_cast<int>(2 + (_STD bit_width(reinterpret_cast<uintptr_t>(_Value)) + 3) / 4);
|
|
}
|
On Godbolt's compiler explorer: https://godbolt.org/z/z99vjeEhh
Inspired by #2097 .
Try this code:
STL/stl/inc/format
Lines 2496 to 2504 in 737ce4a
On Godbolt's compiler explorer: https://godbolt.org/z/z99vjeEhh