Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Programming Arithmetic

Options
  • 11-05-2012 5:07pm
    #1
    Registered Users Posts: 3,038 ✭✭✭


    I'm sorry it has to come to this but I have to ask for some help with the basic arithmetic in C programming. As far as I can infer there are four important types of arithmetic:

    Binary
    Octal
    Hexadecimal
    Floating Point

    This would be easy enough in itself if it wasn't for dealing with signed numbers, 1s complement, 2s complement etc... so:
    What is the logic behind signed arithmetic?
    Why do numbers in C programming have ranges like -127 to 128 but you can do arithmetic outside of this range & have it still make sense (i.e. a number like 66666 being subtracted from ffff in hexadecimal - which apparently only has a range up to 65536 or something - or whatever it is you actually do).


Comments

  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    Binary, Octal and Hexadecimal are not types of arithmetic, they are number systems just like Decimal is a number system also. You perform arithmetic operations (add, subtract, etc.) exactly as you would with the Decimal system.

    Floating point representation is just a different way of representing numbers generally it is used to represent large numbers or non-whole numbers.

    The 1s complement of a Binary number is where you take the number and everywhere you have a 1 you replace it with a 0 and vice versa.

    So if you have the number 1011 and take the 1s complement of it you will get 0100.

    The 2s complement of a Binary number is in essence the negative of that number. To get it you first get the 1s complement of the number and then add 1. It is used to represent positive and negative numbers were the 1st bit denotes the sign if it is a 0 the number is positive and if it is a 1 the number is negative.

    5 in 4-bit 2s complement is 0101, -5 in 4-bit 2s complement is 1011.

    2s complement is generally used for representing numbers in binary form because it is easy to represent positive and negative numbers using this representation.

    Numbers in C programs have ranges because there is a fixed number of bits allocated for their storage.

    If you have 8-bits allocated to store a variable the maximum possible signed number you can store in it is 127, ( ( 2^8 ) / 2 ) - 1 = 127, and the minimum number that you can store is -128. Whereas if it was unsigned you could store numbers between 0 and 256.

    All C data types are stored in binary but programmers tend to use hexadecimal more for representing binary number because it is easier than writing out loads of 1s and 0s, 1 hex digit can represent 4 binary digits.

    From wikipedia we can see how many bits are allocated to store the basic data types.

    I hope that helps you, I did find it a bit hard to understand what you were asking for from your post so if I didn't answer your question please tell me.


  • Registered Users Posts: 3,038 ✭✭✭sponsoredwalk


    Thanks a lot Kav,

    What if you wanted to use the number 130 & were working with signed numbers?
    What if you wanted to use the number 260 & were working with unsigned numbers?
    What if you wanted -130?
    What if you add 255 + 2 in unsigned binary?

    How do you make sense of hexadecimal 2s complements? In other words, is that not very similar to the method for binary you've just explained?
    All I vaguely remember is subtracting from ffff but that made no sense :(
    Also something that really bugged me was getting numbers outside of the range hexadecimal allowed, if you get what I'm saying I'm asking the same question as asking about 255 + 2 only the hexadecimal equivalent.
    Thanks for the help, much appreciated :cool:


  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    I'll answer the questions assuming you are using 8-bits to store each value, I take it that's what you mean.

    In C numbers usually aren't stored usnig just 8-bits though.
    What if you wanted to use the number 130 & were working with signed numbers?
    130 would be converted to binary, 10000010, which when stored would actually be the number -126. Basically you get garbage output.
    What if you wanted to use the number 260 & were working with unsigned numbers?
    260 would be converted to binary, 100000100, but you would lose the most significant bit when it was being stored so you would only store 4. Garbage output again.
    What if you wanted -130?
    -130 again would be converted into binary but when stored some of the leading bits would be dropped (forgotten about) as before and you again would have garbage output.
    What if you add 255 + 2 in unsigned binary?
    Both numbers would be converted to binary and added and the result would be 1 but the system you are using may generate a carry which will set a flag telling you there was overflow (i.e. the result is too big to be stored in 8-bits) and you could then do something to work around the problem. Generating a carry only happens when an arithmetic operation is carried out.
    How do you make sense of hexadecimal 2s complements? In other words, is that not very similar to the method for binary you've just explained?
    I've never heard of hex 2s complement or do you mean the hex representation of a 2s complement binary number?

    Hexadecimal has no range because it is a number system. In theory the range is +- infinity.


  • Registered Users Posts: 3,745 ✭✭✭Eliot Rosewater


    In C hexadecimal is purely conceptual, right? That is, you can input constants in hexadecimal format and write values out in hexadecimal, but ultimately the arithmetic in your computer is done in binary. So the relationship between hexadecimal and the computer is the same as the relationship between base-10 decimal and your computer. The computer ultimately doesn't work in those bases, but C is designed so that human computer programmers can work with those bases if they want, with the computer doing all the conversion to binary.

    Thus, talking about a 2s complement of a hexadecimal number doesn't make sense unless you're talking about getting a hexadecimal number, converting it to binary, applying 2s complement, and converting the result back to hexadecimal.

    As regards the limits, they're much larger and so, starting off, are usually not something worth worrying about, except from an academic standpoint (and in exams). Regular int can take values from under minus 2 billion to over plus 2 billion.


  • Registered Users Posts: 3,038 ✭✭✭sponsoredwalk


    Thanks a lot man, because of this thread I was able to ask a person in my year to explain floating point arithmetic without me having paralyzing headaches & I (hope I) just scraped a pass in this absolutely horrendously awful subject, never again have to deal with this pure hell! :cool:


  • Advertisement
Advertisement