Author Topic: Hexadecimal  (Read 567 times)

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Hexadecimal
« on: May 30, 2011, 04:05:48 pm »


               I like to use bitwise operators, and have noticed no issue using the decimal system when identifying bit values. So I am really confused about why anyone would use hexadecimal notation in the scripts. Is it simply habit? Why use hexadecimal?

[Edit] I mean as opposed to binary. But then... I can't find a means to use binary... so that might answer my question.
               
               

               


                     Modifié par henesua, 30 mai 2011 - 03:29 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Hexadecimal
« Reply #1 on: May 30, 2011, 05:49:22 pm »


               For me it come down to how the script is read.  Once a script is compiled there is no difference between saying 'int x = 15;'  or  'int x = 0x0F;' after all internally they are the same number and stored the same way.  

However when I see  'int x = 15;'  in one of my scripts, My mind just thinks about it as an intenger number.  

When I see   'int x = 0x0F;'  in the script, My mind is already thinking about it as being a bit mask.  

So for me the reason for useing hex notation is readability. The same reason I would use constants or start all of my intengers lables with a 'n' ot 'i' and all of my string lables with a 's'.  

The other benifit form useing hex notation, for bitwize operations, Is how easy they are to convert to bianary  by just looking at them.  

example.     
 x =  61440   & 32768;
 In the above Is x equal to anything or is it just 0 ?   In order for me to figure that out I would be reaching for my calculator. 

Here is the same equasion in hex notation.
x= 0xF000 & 0x8000;
now in this form I can convert both number to bianary in my head and see that:
 0xF000  has bits 12 - 15 set. and
0x8000 has only bit 15 set.
making  x = 0x8000;

The easier the script is to follow in your head the simpler they will be to debug.
               
               

               


                     Modifié par Lightfoot8, 30 mai 2011 - 05:02 .
                     
                  


            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Hexadecimal
« Reply #2 on: May 30, 2011, 09:31:35 pm »


               Pretty much what lightfoot said. They are very handy for masks, as well, and for turning one hex number into far more potential values. For example, take our 'tagging' system, which has different masks at higher numbers, each for a different part of the mod, with lower numbers meaning different things depending which mask is set higher up. Here are some sample values to illustrate my point:

const int PCACC_PRELL_CORPSE_LORD                                 = 0x40000001;
const int PCACC_PRELL_HENDRON                                     = 0x40000002;
const int PCACC_PRELL_RAT_KING                                    = 0x40000004;
const int PCACC_PRELL_MINOTAUR_CHIEFTAIN                          = 0x40000008;
const int PCACC_PRELL_LAVA_KING                                   = 0x40000010;
const int PCACC_PRELL_BLOOD_MOOR_HAGS                             = 0x40000020;
const int PCACC_PRELL_SHADOW_PONTIFF                              = 0x40000040;
const int PCACC_PRELL_ANCIENT_KINGS                               = 0x40000080;
const int PCACC_PRELL_HIVE_MOTHER                                 = 0x40000100;
const int PCACC_PRELL_QUEEN_SPIDER                                = 0x40000200;
const int PCACC_PRELL_RAZHID                                      = 0x40000400;
const int PCACC_PRELL_AXILAR                                      = 0x40000800;
const int PCACC_PRELL_ZERYA                                       = 0x40001000;
const int PCACC_PRELL_HEADMASTER                                  = 0x40002000;
const int PCACC_PRELL_CAPTAIN_ANGUS                               = 0x40004000;
const int PCACC_PRELL_ELANNA_NIGHTSTAR                            = 0x40008000;
const int PCACC_PRELL_HEL                                         = 0x40010000;
const int PCACC_PRELL_REANIMATOR                                  = 0x40020000;
const int PCACC_PRELL_ICE_KOBOLD_KING                             = 0x40040000;
const int PCACC_PRELL_WATER_SHRIKE_NEST_MOTHER                    = 0x40080000;
const int PCACC_PRELL_ELDER_ORB                                   = 0x40100000;
const int PCACC_PRELL_MOTHER_OF_THE_CORN                          = 0x40200000;
const int PCACC_PRELL_FORMIAN_MATRIARCH                           = 0x40400000;
const int PCACC_PRELL_DRIDER_CHIEF                                = 0x40800000;

const int PCACC_DRAGON_ASIMATHAS                                  = 0x50000001;
const int PCACC_DRAGON_BLOODPOOL                                  = 0x50000002;
const int PCACC_DRAGON_CESSPOOL                                   = 0x50000004;
const int PCACC_DRAGON_DEADPOOL                                   = 0x50000008;
const int PCACC_DRAGON_GLITHILDHOUL                               = 0x50000010;
const int PCACC_DRAGON_GREHNAXAS                                  = 0x50000020;
const int PCACC_DRAGON_KARDKILDONTAR                              = 0x50000040;
const int PCACC_DRAGON_LITHIUCSHAS                                = 0x50000080;
const int PCACC_DRAGON_SPAWN_DEEPONE                              = 0x50000100;
const int PCACC_DRAGON_SPAWN_UROBOROS                             = 0x50000200;
const int PCACC_DRAGON_WASTALGRANIQ                               = 0x50000400;
const int PCACC_DRAGON_DRACOLICH                                  = 0x50000800;

const int PCACC_QUEST_STAFF_OF_ANDUIN                             = 0x60000001;
const int PCACC_QUEST_ARCHEMLIS                                   = 0x60000002;
const int PCACC_QUEST_BORIAN_MUSHROOM                             = 0x60000004;
const int PCACC_QUEST_BORIAN_MUSK_CREEPER                         = 0x60000008;
const int PCACC_QUEST_HERO_CRYSTAL                                = 0x60000010;
const int PCACC_QUEST_SOLIS_GAOBIN_GOOD                           = 0x60000020;
const int PCACC_QUEST_SOLIS_GAOBIN_EVIL                           = 0x60000040;

As you can see, there are separate masks for pre-legendary accomplishments, dragon accomplishments, and quest (storyline) accomplishments:


const int PCACC_TYPE_PRELL                                        = 0x40000000;
const int PCACC_TYPE_DRAGON                                       = 0x50000000;
const int PCACC_TYPE_QUEST                                        = 0x60000000;

In binary, it would be FAR harder to see what was going on there. Likewise, even if you aren't masking in that way, hex is just much more readable, and doesn't take all that long to grow accustomed to. Here's another example, showing a non-masked variable, with each bit having a distinct meaning, our pc options variable (which stores toggleable client option settings like low graphics mode and other configurations):


const int PCOPTION_NOINVISHELMS                                   = 0x00000001;
const int PCOPTION_NOAUTOREBUFF                                   = 0x00000002;
const int PCOPTION_SHIFTERFIX                                     = 0x00000004;
const int PCOPTION_NOTURNIMMOB                                    = 0x00000008;
const int PCOPTION_TAUNTBLUFF                                     = 0x00000010;
const int PCOPTION_TAUNTPERSUADE                                  = 0x00000020;
const int PCOPTION_DEATHARROW                                     = 0x00000040;
const int PCOPTION_DOMSPELLS                                      = 0x00000080;
const int PCOPTION_NORESPAWN                                      = 0x00000100;
const int PCOPTION_NOWWCONSUME                                    = 0x00000200;
const int PCOPTION_NOTURNSMITE                                    = 0x00000400;
const int PCOPTION_HEALCIRCLE                                     = 0x00000800;
const int PCOPTION_WHISPER_GUILD                                  = 0x00001000;
const int PCOPTION_WHISPER_META                                   = 0x00002000;
const int PCOPTION_BARDSONG_SWAP                                  = 0x00004000;
const int PCOPTION_CURSESONG_SWAP                                 = 0x00008000;

Note that the powers of two form a distinct pattern. If I want the next available bit, I can immediately see that it is 0x00010000 - no need to sit there multiplying some large number by 2. This is really just another illustration of lightfoot's point about readability and ease of use.

Funky
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Hexadecimal
« Reply #3 on: May 30, 2011, 09:51:53 pm »


               Because I don't use it all the time, I'm not used to seeing that each digit contains four bits. At first glance binary is easier. However hexadecimal is nice because it compacts the bits, and thus the numbers are written shorter. Once the letters replace numerals it gets really confusing. Thankfully, none of the bits require letters.

I'll just have to get used to it if I am going to keep using it.