use of hex literal constant

This is a discussion on use of hex literal constant within the C++ Programming forums, part of the General Programming Boards category; hi, i always initialize enum members with integer values. sometime ago, i ran accross some header files of the Qt ...

  1. #1
    Registered User happycoder's Avatar
    Join Date
    Jun 2003
    Posts
    6

    use of hex literal constant

    hi, i always initialize enum members with integer values. sometime ago, i ran accross some header files of the Qt gui toolkit. instead of integer constants, they use hex constatns.

    eg.
    Code:
       enum Button { Help = 1, Defaults = 2, Ok = 4, Apply = 8, Cancel = 16 };
    as opposed to:

    Code:
     enum SectionFlags {
            SectionDefault             = 0x00,
            SectionSkipEmpty           = 0x01,
            SectionIncludeLeadingSep   = 0x02,
            SectionIncludeTrailingSep  = 0x04,
            SectionCaseInsensitiveSeps = 0x08
    my question is: does the fact that integer constants are promoted to (or treated as) double has something to do with this or is it just style? does using hex literal constants make the program more efficient than using integer literal constants?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Hexadecimal constants are easier to convert directly to binary in your head, so they're very useful for bit flags and masks.

    0x0 == 0000
    0x1 == 0001
    0x2 == 0010
    0x3 == 0011
    0x4 == 0100
    0x5 == 0101
    0x6 == 0110
    0x7 == 0111
    0x8 == 1000
    0x9 == 1001
    0xA == 1010
    0xB == 1011
    0xC == 1100
    0xD == 1101
    0xE == 1110
    0xF == 1111
    My best code is written with the delete key.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: use of hex literal constant

    Originally posted by happycoder
    hi, i always initialize enum members with integer values. sometime ago, i ran accross some header files of the Qt gui toolkit. instead of integer constants, they use hex constatns.

    eg.
    Code:
       enum Button { Help = 1, Defaults = 2, Ok = 4, Apply = 8, Cancel = 16 };
    as opposed to:

    Code:
     enum SectionFlags {
            SectionDefault             = 0x00,
            SectionSkipEmpty           = 0x01,
            SectionIncludeLeadingSep   = 0x02,
            SectionIncludeTrailingSep  = 0x04,
            SectionCaseInsensitiveSeps = 0x08
    my question is: does the fact that integer constants are promoted to (or treated as) double has something to do with this or is it just style? does using hex literal constants make the program more efficient than using integer literal constants?
    A constant is a constant. The program itself doesn't know or care how you specified the constant. The program just gets a raw binary number, it's the COMPILER that deals with generating that number.

    The compiler will generate completely identical code. There's no difference, to the compiler, whether you say "255" or "0xFF". The two operate exactly the same and are exactly equivalent.

    In every single case, a constant should be declared in whichever radix makes most sense. The different ways of specifying a constant are provided solely for human readability of code, to the machine, they're all the same.

    For example, it's not that easy to look at this code at a glance and figure out how many times the loop occurs:

    for (int x = 0; x < 0xEB; ++x){
    }

    and it's also not easy to look at this at a glance and know what bits are being set:

    #define MASK_RED 16711680L

    wheras the following are nice and readable:

    for (int x = 0; x < 235; ++x){
    }

    #define MASK_RED 0xFF0000L

    Generally, in your case, it's much easier to do power-of-twos (like you would for flags) in hex, e.g.:

    0x01 // 2^0
    0x02 // 2^1
    0x04 // 2^2
    0x08 // 2^3
    0x10 // 2^4
    0x20 // 2^5
    0x40 // 2^6
    0x80 // 2^7

    That answer the question?
    Last edited by Cat; 06-07-2003 at 02:40 PM.

  4. #4
    Registered User happycoder's Avatar
    Join Date
    Jun 2003
    Posts
    6
    That answer the question?
    it sure did! hex literals are most suited for bitwise flags, got it!

    i asked this question bec. i remember reading from my text books that an integer literal constant is always treated as a double, but i dont remember reading anything about hex literals. so i thought maybe hex literal constants are treated differently. now i know they are treated the same.

    maybe i should have asked if integer and hex literals are treated differently.

    tnx guys!

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Hmm, literals aren't normally treated as doubles; that could give roundoff errors, and would be dangerous.

    By definition:

    Constants with no decimal point or L are of type int.
    Constants with no decimal point and an L are of type long
    Constants with no decimal point and a U are unsigned.

    Constants with a decimal point are of type double
    Constants with a decimal point and an "F" are of type float

    E.g.:

    1 // type = int
    1L // type = long
    1UL // type = unsigned long
    1.0 // type = double
    1.0F // type = float
    1.0D // type = double (the default anyway)
    1.0L // type = long double

    Of course, automatic type promotion will happen when appropriate. E.g :

    1 / 2; // integer division, yields 0.
    1.0 / 2; // 2 is promoted to a double, and double division is performed.
    1 / 2.0; // 1 is promoted to a double
    1 / 2.0F; // 1 is promoted to a float

    Oh, here's a quiz for you: What does this print, and why?
    Code:
    if (16 == 016)
      std::cout << "16 == 016" << std::endl;
    else std::cout << "16 != 016" << std::endl;
    Last edited by Cat; 06-07-2003 at 08:50 PM.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Note that numbers with a decimal point in them, even with a zero or no trailing numbers, are declared as floats to the compiler.

  7. #7
    Registered User happycoder's Avatar
    Join Date
    Jun 2003
    Posts
    6
    Oh, here's a quiz for you: What does this print, and why?

    code:

    if (16 == 016)
    std::cout << "16 == 016" << std::endl;
    else std::cout << "16 != 016" << std::endl;
    since 16 is decimal and 016 is octal, they will never be equal.

    1.0 // type = double
    ah yes, i remember reading this in c by disection 2nd ed.

    hey, maybe its time i polish on my basic c++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21