hexadecimals in obj c - freaked!

Discussion in 'iOS Programming' started by duffle, Jun 3, 2014.

  1. duffle macrumors newbie

    Joined:
    Jun 3, 2014
    #1
    Hi there,

    I was recently working my way through the 6th version of Stephen G Kochans book and i only got as far as page 63 (mortified) I found the content manageable but when hexadecimals were being brought up I started to freak out and haven't gone back to it since - about a month.

    Are hexadecimals and complicated maths a very important part of obj c and other languages in general. Should I plough on regardless of my understanding of Hexadecimals??

    Thanks Ciara
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    I don't have the book, so I can't comment directly on the content of it.

    Do you need to understand hexidecimal? At the beginner stage, probably not. Hex is most useful when trying to program something involving bit operations. If you don't need to mess with bit operations, then you probably don't need to use Hex.

    Often times, authors will introduce a concept (like Hex) because you need to understand the basic concept in order to understand some later content in the book. Maybe that's not the case here and the author is just being complete. Either way, if you get to later content that requires the Hex knowledge, they you'll have to go back and practice.

    Hex is used by convenience for programmers since it's easier to see bit patterns in hex numbers versus a decimal representation.
     
  3. crossi81 macrumors member

    Joined:
    Aug 13, 2012
    #3
    Not really, unless (like mfram wrote) you have to operate on bits level. Hex is a useful representation because you need exactly 4 bit (half byte) to describe a single digit hex value (0 is 0000 and F is 1111). And while bytes are used a lot in many situations (especially "low levels" ones like IPv4 which uses 32 bits/4 bytes to store an IP address or color depth, for example a 24 bit depth which uses 8 bit for every RGB color), you can skip hex topics for basic-to-intermediate projects.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Depends on your goals. A beginner doesn't need to know hex. An intermediate or advanced programmer almost certainly does need to understand hex.

    Hex isn't hard. (I was probably 14 yo when I learned about it.)

    Calculator.app understands hex.
    Open Calculator and choose View > Programmer.
    On the right hand side click '10'
    Also click 'ASCII' on the left side
    Click 7
    Click 0
    You'll see '70' in the display. That's 70 (ten times 7) in base 10.
    You'll see 'F' in the display. The ASCII code for the letter 'F' is 70 base 10.
    Click the '16'
    The display now shows '0x46' which is hex 46, or 46 base 16.
    (4 * 16) + (6) = (64 + 6) = 70 base 10.

    That's it.
    That's hex.
    That's base 16.

    You can use Calculator.app to assist you in your understanding of hex.

    Needless to say, if you have a specific question about hex you should ask it here. That would be better than asking 'can I avoid learning about hex?'
     
  5. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    I hear you brother. When Is started reading a few years ago I came to that chapter too and pretty much jumped over that chapter. After a few more chapters I decided that Objective C was to much for me to understand as the place to start learning to program. I jumped back to C and learned that first.

    For the most part my math background was all high school basic math and a little algebra 20 years ago in college. You don't need to know a lot of math to make basic iPhone apps. You push a button and something is displayed on the screen, no math.

    That begin said, I started a game a couple weeks back and now I am sorry I did not take math years back. Having to learn degrees to radians, atan2() and firing of cannon ball math, my brain hurts.
     
  6. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #6
    I have never used hex (or bit shifting) outside of classes or books.

    I really wish books would explain good uses for these with practical examples. For as long as I've been programming (Off and on for 17 years now) you'd think I could think of a place to use these in the real world.
     
  7. duffle thread starter macrumors newbie

    Joined:
    Jun 3, 2014
    #7
    thank you all for taking the time to reply, i am currently learning about hex. No point doing something if i'm not going to do it right :)
     
  8. Amazing Iceman macrumors 68040

    Amazing Iceman

    Joined:
    Nov 8, 2008
    Location:
    Florida, U.S.A.
    #8
    There will be some situations when you may need to use Hex numbers.
    Learning it will not cause any harm. Don't be afraid of it, I learned it when I was 13, and that was decades ago. Try to understand the concept at least.
     
  9. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    Understanding hex isn't particularly important. That hex numbers can be converted to and from decimal numbers isn't important.

    Here's why they're used at all:

    In programming, you work with a lot of booleans - things which are either true or false, 1 or 0. The actual hardware in a computers, however, the RAM and CPU, namely, only work on numbers which have a larger range than that. The smallest most will work with is a byte, which is the size of 8 binary digits.

    You could store one boolean per byte, but that would mean that 7/8ths of the binary digits per byte would go unused. Performance freaks will scream about how unacceptable that is.

    So what people do is they merge several boolean variables into a more space efficient variable, a byte (which can store 8 booleans) or an int (depends on the platform, can store 16, 32, or 64 booleans).

    Suppose someone has a byte storing 4 true values and 4 false values, something like:

    1100 1010

    It's much clearer to write in binary, because you can clearly see which of the 8 values are true and false, respectively. In contrast, it would look like this as a decimal number:

    202

    Completely impossible to tell what's true and false.

    Now imagine that they have a 64 bit int that they're writing out:

    0000 0001 0010 0011 0100 0101 0110 0111
    1000 1001 1010 1011 1100 1101 1110 1111

    It's a rather wieldy and difficult to read/write/copy/whatever number. So instead of writing it out in binary, they write it in hex:

    0123456789ABCDEF

    What makes that better than decimal, you might wonder. Each group of four binary digits corresponds exactly to one hexadecimal digit. So if you only care about a binary digit in a particular place, you can look at the hexadecimal number and convert just the single hex digit from hex to binary to find out what the value is.

    But none of it matters, at all. This is all about dealing with compressing large amounts of booleans into small amounts of memory. How many booleans are you working with? Unless you're answering something in the millions, I really don't think it's going to do anything to your performance.
     
  10. duffle thread starter macrumors newbie

    Joined:
    Jun 3, 2014
    #10
    A genuine thank you again. i have been so surprised how straightforward hex is once i got started!! i got my hands on a GCSE basic engineering maths book and have just gone through the section relevant to me.

    so could someone tell me if my maths are correct:

    hex of 9F = 10011111 in binary

    thanks a mill :)
     
  11. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #11
    9F

    9 in binary is 1001 (8 + 1) and F is 15 which is all 4 bits at 1. So 10011111 is 9F.
     
  12. Dranix macrumors 6502a

    Dranix

    Joined:
    Feb 26, 2011
    Location:
    Gelnhausen, Germany
    #12
    Hmm, so what is so hard on binary and hexadecimal systems? Both are learned in around 10mins...
     

Share This Page