1

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?

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
user90726
  • 205
  • 2
  • 9
  • @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.) – user90726 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: https://simple.wikipedia.org/wiki/Hexadecimal – user90726 Aug 25 '20 at 21:54
  • @DocBrown you pinged the wrong person – user253751 Aug 26 '20 at 09:01
  • @DocBrown Yes, it looks more accurate, thanks. – user90726 Aug 26 '20 at 09:16
  • @user253751: If these are decimal numbers, you shouldn't add leading zeroes, so 15 is correct. – JacquesB Aug 26 '20 at 15:44

2 Answers2

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

Alexander
  • 3,562
  • 1
  • 19
  • 24
  • 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. – Bart van Ingen Schenau 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
  • Guys, I think you're missing the point. There are many, many different formats of pointers, let alone decimal numbers. All I'm showing is that there is a benefit to occasionally including leading zeros. Various assemblers and tools are free to use all kinds of different strategies spacing, abbreviation, etc. – Alexander Aug 26 '20 at 18:21
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.)

JacquesB
  • 57,310
  • 21
  • 127
  • 176
  • _"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? – user90726 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
  • A prefix of zero, not followed by 'x' or 'X', is regarded as an octal number (AKA octal constant) in the C and C++ languages – fpmurphy Aug 26 '20 at 14:20
  • @fpmurphy IPv6 abbreviation removes the redundant zeroes when representing a 128 bit number. This is an extension of removing just leading zeroes from a number as per what the OP's question was about. – Peter M Aug 26 '20 at 14:48