Hex vs Dec

This is a discussion on Hex vs Dec within the C++ Programming forums, part of the General Programming Boards category; When giving a value to a variable, is there any advantage to give it in a value hexadecimal instead of ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    790

    Hex vs Dec

    When giving a value to a variable, is there any advantage to give it in a value hexadecimal
    instead of decimal?
    Does it save some cycles of calculations for the compiler or is it just some kind of
    "snobbery" when programmers use hex when they could just use decimal instead?
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    In some situations hex is clearer to read, because when you're used to it, you can quickly count in your mind which bytes are 1 and which are 0.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Xmas and Halloween
    Dec 25 == Oct 31
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Ducky View Post
    When giving a value to a variable, is there any advantage to give it in a value hexadecimal
    instead of decimal?
    Does it save some cycles of calculations for the compiler or is it just some kind of
    "snobbery" when programmers use hex when they could just use decimal instead?
    There is no performance advantage -- you should just use whatever is more readable in the given situation.

    For instance, If I wanted to set someones age to 15:
    age = 15; // readable
    age = 0xF; // what?

    Now let's say we want to set up a mask to get the lowest byte of information from a larger variable:
    byte_mask = 255; // What does this mean?
    byte_mask = 0xFF; // Makes sense.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    At compile time, who cares?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    It's all just bits to the computer anyway, so the compiler "transforms" it into the correct opcode, hex, decimal, octal or whatever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Ok, thank you everybody, i understand it better now.

    @Salem
    Yes i knew this one. Its a funny one. :-)
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Salem View Post
    Xmas and Halloween
    Dec 25 == Oct 31
    Huh?

    EDIT: Nevermind, now I see. I thought you were still talking about Hex...
    Last edited by cpjust; 08-19-2009 at 08:19 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Quote Originally Posted by Ducky View Post
    When giving a value to a variable, is there any advantage to give it in a value hexadecimal
    instead of decimal?
    There is an advantage for floating-point values. If you want to ensure that you get the same floating-point value using any compiler and/or runtime, use hexadecimal. This is because hex to binary conversion is exact, whereas decimal to binary conversion is inexact (in general) and can vary across platforms.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DoctorBinary View Post
    There is an advantage for floating-point values. If you want to ensure that you get the same floating-point value using any compiler and/or runtime, use hexadecimal. This is because hex to binary conversion is exact, whereas decimal to binary conversion is inexact (in general) and can vary across platforms.
    The only flies in that ointment are (i) hexadecimal floating point numbers are not supported by standard C++ (are they bringing that in from C99 into the next standard?) and/or (ii) if they use different internal representations the hex format will change as well (if you are referring to the bit patterns).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  2. hex to dec to binary
    By vijlak in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 09:38 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Hex to Dec Problem
    By gqchynaboy in forum C Programming
    Replies: 14
    Last Post: 03-02-2005, 01:58 PM
  5. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM

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