• Advertisement
Sign in to follow this  

Whats the added benefit of using hexadecimal?

This topic is 440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

While reading someone else code, I noticed that they were using hexadecimal numbers instead of regular base 10. What is the added benefit of using hexadecimal in code (or even binary for that matter).

 

For example:

 
enum Behavior
{
    Flee     = 0x00001,
    Seek     = 0x00002,
    Wander = 0x00003
 
}
 

Share this post


Link to post
Share on other sites
Advertisement
8 is bit 4, 3 is bits 1 and 2, 128 is bit 8

You're off by one in bit number.

Lowest bit is bit number 0, which is 1 or 0x01, which is also 2^0, or 1 << 0.

 

By starting bit numbers by 0, the power, the shift, and the number of zeroes right of the 1 (ie the zeroes shifted in) all match.

dec =  hex = shift = power = bits
  1 = 0x01 = 1 << 0 = 2^0 = 0001
  2 = 0x02 = 1 << 1 = 2^1 = 0010
  4 = 0x04 = 1 << 2 = 2^2 = 0100
  8 = 0x08 = 1 << 3 = 2^3 = 1000

If you start counting at 1, you get "1 << (1-1)", and "2^(1-1)" in the first line which is more cumbersome to get right all the time.

 

 

Edit:

As others have noted, hex is mostly useful if you are interested in bits than the numeric value.

 

In fact, I read hex numbers always as bit patterns, and not as hex number. For example, "0xDA" I don't read as hex "D" and "A", I read it as bit patterns "1101" and "1010", I see the bit patterns expressed by the hex digits.

Edited by Alberth

Share this post


Link to post
Share on other sites
My personal preference is to only use hex to show that I'm caring more about the bits within the variable than the value itself.

For instance:
char some_flags = 0x0D; // <- This is more descriptive to me than...
char some_flags = 13; // ... this. The bits are important, the value is not. I see 0x0D as 00001101, not '13'. I see '13' as a number and not a string of bits.
Edited by MarkS

Share this post


Link to post
Share on other sites

While reading someone else code, I noticed that they were using hexadecimal numbers instead of regular base 10. What is the added benefit of using hexadecimal in code (or even binary for that matter).
 
For example:

 
enum Behavior
{
    Flee     = 0x00001,
    Seek     = 0x00002,
    Wander = 0x00003
 
}
 

 
The reason it would be used in an enum like your example is when the programmer wants to combine the enum values.
 
When using powers of two the values look like this:
 
0x1 = 0001
0x2 = 0010
0x4 = 0100
0x8 = 1000
 
So if we have:

enum Behavior {
  Patrol = 0x1,
  Seek   = 0x2,
  Talk   = 0x4,
  Run    = 0x8
}

then behaviors can be combined by OR-ing the values.

 

For example if an NPC is patrolling and talking: 

 

Behavior CurrentBehavior = Behavior.Patrol | Behavior.Talk;  // CurrentBehavior is 0001 | 0100 -> 0101

 

or we can find out if a behavior is currently set by AND-ing the values:

 

if ((CurrentBehavior &  Behavior.Talk) == Behavior.Talk)   // ((0101 & 0100) == 0100) -> true 

Share this post


Link to post
Share on other sites

The reason it would be used in an enum like your example is when the programmer wants to combine the enum values.


But look closely at the example. ;)
 
 
enum Behavior
{
    Flee     = 0x00001, // Going to assume there are missing digits here.... In binary: 00000001
    Seek     = 0x00002, //In binary: 00000010
    Wander = 0x00003 // In binary: 00000011
 
}
In this case, Behavior.Flee | Behavior.Seek = Behavior.Wander. Behavior.Flee | Behavior.Wander = Behavior.Wander. Behavior.Seek | Behavior.Wander = Behavior.Wander. They have to be used separately, otherwise you just end up wandering. Edited by MarkS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement