-1

While reading the MongoDB Driver code. I noticed an enum with all its values as hexadecimal. I have seen this before but never asked myself why hexadecimal is the best choice. Now I am trying to further myself and understand this piece of code and the reasons MongoDB went the hhexadecimal route instead of intergers. Any help will be appreciated.

Here is the code I am referring to:

https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Bson/ObjectModel/BsonType.cs

Code snippet:

namespace MongoDB.Bson
{
    [Serializable]
    public enum BsonType
    {
        EndOfDocument = 0x00,
        Double = 0x01,
        String = 0x02,
        Document = 0x03,
        Array = 0x04,
        Binary = 0x05,
        Undefined = 0x06,
        ObjectId = 0x07,
        Boolean = 0x08,
        DateTime = 0x09,
        Null = 0x0a,
        RegularExpression = 0x0b,
        JavaScript = 0x0d,
        Symbol = 0x0e,
        JavaScriptWithScope = 0x0f,
        Int32 = 0x10,
        Timestamp = 0x11,
        Int64 = 0x12,
        MinKey = 0xff,
        MaxKey = 0x7f
    }
}
Luke101
  • 63,072
  • 85
  • 231
  • 359
  • It's common for flags that may be combined. Hexadecimal serves as a convenient shorthand for the binary values. – HABO Aug 21 '14 at 23:45
  • 1
    Usually you see this on flags. – DaveShaw Aug 21 '14 at 23:46
  • maybe this answer will help - http://stackoverflow.com/questions/13222671/why-are-flag-enums-usually-defined-with-hexadecimal-values – terrybozzio Aug 21 '14 at 23:48
  • It says [Serializable], so maybe the developers find themselves looking at hex dumps of the data from time to time, and this is a minor convenience in deciphering the hex dumps. Also MinKey and MaxKey have special binary values. In the C# code it makes no difference, so why not. – RenniePet Aug 21 '14 at 23:48
  • Re the other comments, this is an ordinary enum, not a flags-style enum, so those explanations don't work. (Unless this [Serializable] attribute is a derived version that includes [Flags] - but that's not possible is it?) In fact, it's impossible that this is a flags-style enum, unless Document implies Double + String. :-) – RenniePet Aug 21 '14 at 23:49
  • 1
    It may be that the values correspond to those in a vintage header file. Perhaps a code generator (or co-op) created the corresponding C# declaration. – HABO Aug 21 '14 at 23:55
  • "hexadecimal route instead of intergers" -- you misunderstand: hexadecimal numbers **are** integers - they are just expressed in base-16 instead of base-10 (decimal). The code would be *identical* once compiled if you changed `0x0f` to `15`, etc. – Blorgbeard Aug 21 '14 at 23:56
  • It is pretty nonsensical, [Serializable] makes no sense and MinKey is actually -1. The [original source code](https://github.com/mongodb/mongo/blob/master/src/mongo/bson/bsontypes.h) uses decimals. Best to assume the author had his own ... style. Not uncommon in Foss. – Hans Passant Aug 22 '14 at 00:08
  • @HansPassant: Are you sure? On my system "int i = (int)BsonType.MinKey;" gives 255. (This is for the above code snippet, not the original that you reference.) – RenniePet Aug 22 '14 at 00:16
  • You ought to be at least a little puzzled about a "min" that's larger than "max". I left a link to the original source, have a look. – Hans Passant Aug 22 '14 at 00:22

2 Answers2

1

In the general case, hex enum constants are justified if

  • they represent flag bits or
  • if there is some other source of (hex) documentation from which they are derived or
  • if there is some (hex) output or logging tool where they will be made visible.

Unless one of these is true they are usually a bad idea and often a really bad idea. I am not familiar with the Mongo source code, but I see no justification in the fragment given here.

The most likely reason is programmer habit. Some people seem to just think in hex. Most of us try not to.

david.pfx
  • 10,520
  • 3
  • 30
  • 63
-1

The hexadecimal system can express negative numbers the same way as in decimal: −2A to represent −4210.

However, we prefer instead to use the hexadecimal notation to express the exact bit patterns used in the processor, so a sequence of hexadecimal digits may represent a signed or even a floating point value. This way, the negative number −4210 can be written as FFFF FFD6 in a 32-bit CPU register (in two's-complement), as C228 0000 in a 32-bit FPU register or C045 0000 0000 0000 in a 64-bit FPU register (in the IEEE floating-point standard)

Sam
  • 759
  • 1
  • 4
  • 10
  • -1 This does not attempt to answer the OP's question, and it is hardly even relevant enough to move to a comment. – tripleee Aug 22 '14 at 06:37
  • as an assembly programmer i find it very useful to express the numbers in the same manner as handled within the CPU's registers as i've described in my answer and especially when programming microcontrollers. Maybe what I've written needs more clarification but that's my opinion.At the first place, this question is not a straight programming question that can have a straight solution or answer. in fact all answers to such questions are a matter of opinion. so you don't have the right to vote it down. – Sam Aug 22 '14 at 15:13