Right way to show numbers in binary/hex/octal in your opinion?
rempas
rempas at tutanota.com
Thu Dec 30 10:06:45 UTC 2021
On Monday, 27 December 2021 at 06:55:37 UTC, Rumbu wrote:
> When people are dumping numbers to strings în any other base
> than 10, they are expecting to see the internal representation
> of that number. Since the sign doesn't have a reserved bit in
> the representation of integrals (like it has for floats), for
> me it doesn't make any sense if I see a negative sign before a
> hex, octal or binary value.
However, binary uses the most significant digit to identify if
the number is positive or negative. I suppose that if you want to
not "show" a digit when converting to "hex" and "octal", you
could first convert to binary (signed) and then convert this
number to "hex" or "octal" but still, these system are used for
specific purposes and they don't have a real negative
representation so it still doesn't make sense no matter how you
see it.
> The trickiest value for integrals is the one with the most
> significant bit set (e.g. 0x80). This can be -128 for byte, but
> also 128 for any other type than byte. Now, if we go the other
> way around and put a minus before 0x80, how do we convert it
> back to byte? If we assume that 0x80 is always 128, -0x80 will
> be -128 and can fit a byte. On the other side, you cannot store
> +0x80 in a byte because is out of range.
Yeah, this is why I think that showing the sign is actually a
good way to show that it is a signed negative number. If there is
no sign, the number can be either positive signed number or
unsigned. In the end, a "string" will only be used to print the
number to a user so there will be no confusion to the programmers
themselves
> This is also an issue în phobos:
>
> https://issues.dlang.org/show_bug.cgi?id=20452
> https://issues.dlang.org/show_bug.cgi?id=18290
Yeah, I can see why, lol!
More information about the Digitalmars-d
mailing list