Why is the 17th digit of version 4 GUIDs limited to only 4 possibilities?

前端 未结 2 642
无人共我
无人共我 2021-01-19 00:23

I understand that this doesn\'t take a significant chunk off of the entropy involved, and that even if a whole nother character of the GUID was reserved (for any purpose), w

相关标签:
2条回答
  • 2021-01-19 00:52

    Quoting the estimable Mr. Lippert

    First off, what bits are we talking about when we say “the bits”? We already know that in a “random” GUID the first hex digit of the third section is always 4....there is additional version information stored in the GUID in the bits in the fourth section as well; you’ll note that a GUID almost always has 8, 9, a or b as the first hex digit of the fourth section. So in total we have six bits reserved for version information, leaving 122 bits that can be chosen at random.

    (from https://ericlippert.com/2012/05/07/guid-guide-part-three/)

    tl;dr - it's more version information. To get more specific than that I suspect you're going to have to track down the author of the spec.

    0 讨论(0)
  • 2021-01-19 00:52

    Bits, not hex

    Focusing on hexadecimal digits is confusing you.

    A UUID is not made of hex. A UUID is made of 128 bits.

    Humans would resent reading a series of 128 bits presented as a long string of 1 and 0 characters. So for the benefit of reading and writing by humans, we present the 128-bits in hex.

    Always keep in mind that when you see the series of 36 hex characters with hyphens, you are not looking at a UUID. You are looking at some text generated to represent the 128-bits of that are actually in the UUID.

    Version & Variant

    The first special meaning you mention, the “version” of UUID, is recorded using 4 bits. See section 4.1.3 of your linked spec.

    The second special meaning you indicate is the “variant”. This value takes 1-3 bits. This See section 4.1.1 of your linked spec.

    A hex character represents 4 bits (half an octet).

    • The Version number, being 4 bits, takes an entire a single hex character to itself.
      • Version 4 specifically uses the bits 01 00 which in hex is 4 as it is too in decimal (base 10) numbers.
    • The Variant, being 1-3 bits, does not take an entire hex character.
      • Outside the Microsoft world of GUIDs, the rest of the industry nowadays uses two bits: 10, for a decimal value of 2, as the variant. This pair of bits lands in the most significant bits of octet # 8. That octet looks like this, where ‘n’ means 0 or 1: 10 nn nn nn. A pair of hex characters represent each half of that octet. So your 17th hex digit, the first half of that 8th octet, 10 nn, can only have four possible values:
        • 10 00 (hex 8)
        • 10 01 (hex 9)
        • 10 10 (hex A)
        • 10 11 (hex B)
    0 讨论(0)
提交回复
热议问题