Thread: Hex vs Dec

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

    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?
    Using Windows 10 with Code Blocks and MingW.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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
    Chappell Hill, Texas
    Posts
    2,332
    At compile time, who cares?
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    932
    Ok, thank you everybody, i understand it better now.

    @Salem
    Yes i knew this one. Its a funny one. :-)
    Using Windows 10 with Code Blocks and MingW.

  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,336
    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, 11:26 AM
  4. Hex to Dec Problem
    By gqchynaboy in forum C Programming
    Replies: 14
    Last Post: 03-02-2005, 02:58 PM
  5. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM