In some situations, hex numbers are preferred (preference, tradition, easier to spot a potential bug). An example, when you declare bit-flags:
[Flags]
enum SomeOptions
{
Opt1 = 0x1,
Opt2 = 0x2,
Opt3 = 0x4,
Opt4 = 0x8,
Opt5 = 0x10
}
It's much easier to see, which flags are combined of which ones (or if all flags are power of 2, so they are independent).
Other example:
int myNum = flags & 0x7fff;
It's easy to see, which bits you are including in your myNum
, and which not.
Also, hex numbers are commonly used when you want to express some numbers, that are power of 2, as it's easier to see, if you haven't made any mistake (compare 0x10000000
and... ehm, some big number :) ).