0

0xF, 0x0000000F (total width is 10 characters), and 0x0000000000000000F (total width is 18 characters) all mean 15 (decimal).

Is it correct to say that there is no reason to add leading zeros to a smaller hex number beyond 10 characters in total? In other words, is it correct to say that anything longer than the second version is excessive?

8
  • @DocBrown I'm total newbie in the area of hex numbers and so I'm not really aware about how they work. I just can't answer it. (And it's completely OK for this question to be downvoted.)
    – jsv
    Aug 25 '20 at 13:07
  • 1
    @jsv "I'm not really aware about how they work" Well, just like decimal numbers do, only with base 16 instead of 10. They are often used to represent binary numbers (base 2) because they are easier to read and fit well with the format. Aug 25 '20 at 13:23
  • Hexadecimal is just a different way to write numbers. There is nothing different about the numbers themselves.
    – Jesper
    Aug 25 '20 at 14:11
  • 3
    Should we write 15, 00000015, or 0000000000000015?
    – user253751
    Aug 25 '20 at 16:46
  • It doesn't directly answer the question, but it helps to better understand the answers: simple.wikipedia.org/wiki/Hexadecimal
    – jsv
    Aug 25 '20 at 21:54
6

Hexadecimal numbers are typically used to represent binary data which have a fixed size in bytes. Hex numbers are prefixed with zeroes so the number of digits match the range.

So if you write a byte value as hex, you always write 2 hex digits, even if the number is below 16. You write 0A, not A or 00A even though they are technically the same number.

This is especially important when writing sequences of hex numbers. For example the two byte sequence 0A 0D could in theory be written as A D but then it would not be clear anymore that it is a two byte sequence. And if you write 000A 000D it would be interpreted as a 4-byte sequence which is quite different!

You use 4 hex digits to write a 16-bit value, 8 digits for a 32 bit value, 16 digits for a 64 bit value and so forth.

So there are contexts where you would use a large number of zeroes. Lets say you write the contents of a 64-bit register - writing 0x000000000000000F is entirely appropriate. It would be confusing if you left out some of the zeroes. (Although you would probably use spaces or separators to make it more readable like 0x0000 0000 0000 000F).

In contrast you would typically never add leading zeroes to a decimal number, since the leading zeroes does not carry any information. (Indeed, in some programming languages, a leading zero would cause a decimal number to be interpreted as an octal number.)

7
  • "You use 4 hex digits to write a 16-bit value, 8 digits for a 32 bit value, 16 digits for a 64 bit value and so forth." - And 0x0A is a 8-bit value, and so it requires only 2 hex digits and not 4?
    – jsv
    Aug 25 '20 at 20:43
  • 1
    @jsv: Yes, an 8bit value (one byte) only require 2 hex digits.
    – JacquesB
    Aug 25 '20 at 21:00
  • My comments got deleted, but I'll reiterate: this was a blatant rip off of my answer. It's improved a bit with the edit 3 hours, but that's still not cool.
    – Alexander
    Aug 26 '20 at 12:58
  • Sort of relevant is the scheme used to abbreviate 128 bit IPv6 addresses whereby for example 2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b becomes 2001:db8:3c4d:15::1a2f:1a2b
    – Peter M
    Aug 26 '20 at 13:52
  • @PeterM. How is IPv6 address shortening relevant?
    – fpmurphy
    Aug 26 '20 at 14:05
6

The "appropriate" format is typically context dependent.

For example, if you're working in the context of a CPU with 64 bit registers, it might be more appropriate to write 0x0000 0000 0000 0003 instead of 0x3, just so that the register's width is communicated.

But in general, it's not much different than decimal numbers. If you have a $1,000 in your bank account, you wouldn't write $0,001,000 to be in the 6-zeros club

6
  • LOL. If you don't use lading zero's, I guess there are very few people (or even organisations) with 6 or more zero's on their bank account balance value. Aug 25 '20 at 14:10
  • @BartvanIngenSchenau Especially when $1074952.14 doesn't count! That's only one zero! To have an expected 6 zeroes, they should have about a 60-digit bank balance.
    – user253751
    Aug 25 '20 at 16:47
  • An assembler is is going to complain if you write a 64-bit immediate value as 0x0000 0000 0000 0003 - for example MOV RAX, 0x0000 0000 0000 0003
    – fpmurphy
    Aug 26 '20 at 14:30
  • 1
    @fpmurphy Yep. The format I suggested was more for user readability (e.g. a stack trace)
    – Alexander
    Aug 26 '20 at 17:36
  • @fpmurphy: NASM allows writing 0x0000_0000_0000_0003 and I think recent revisions of C and C++ allow inserting I believe apostrophes for the same purpose.
    – ecm
    Aug 26 '20 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.