Results 1 to 4 of 4

Thread: ques binary

  1. #1
    Senior Member
    Join Date
    Oct 2001

    ques binary

    How does the computer store negetive numbers in binary?

  2. #2
    Join Date
    Aug 2002
    It doesn´t really. It stores the number, and puts the binary for - in front of it... I guess...
    I breathe, therefore I am!
    I type, therefore I live!
    [shadow]I love, therfore I die![/shadow]

  3. #3
    That's a good question. I went and looked, and found this: http://richardbowles.tripod.com/durh...rch/binary.htm

    The top number is still 109, but the bottom number is 147, and they add up to give 256, which the computer is forced to store as 0. We have found two numbers that add to give 0. If the top number is 109, we may reasonably conclude that the bottom number can represent -109.

    Similarly, if you added 43 to the binary pattern 213 using one-byte variables which where the final carry dropped off the end of the sum, you would find that it gives 0, so we could say that the pattern for 213 also represents the number -43.

    This gives a way of interpreting binary patterns for negative numbers called two's complement. This is how you form the binary pattern for a negative number (I use the number 17 as an example):

    Change all the 1 bits to 0 and vice-versa (we call this inverting all the bits), so 00010001 becomes 11101110.

    Add 1 to the number that you produce, so 11101110 becomes 11101111.
    The pattern 11101111 now represents -17, and if you add it to 17 using one-byte variables, it will give you the answer 0. You will notice if you try a few of these, that all negative numbers have a 1 in the bit on the left of the number (what we call the most significant bit). This bit can be used to the sign of the number.
    and for ASM, http://www.geocities.com/SiliconVall.../asml1002.html

    Variable Limits and Negative Values
    How about the limits of those variable types then? See the table below:

    db "define byte" 1 byte 0-255
    dw "define word" 2 bytes 0-65535
    dd "define double" 4 bytes 0-4294967295

    "Ah, I see that the db, dw, and dd are for positive values only. How can I define a negative ones?" Well, you can assign the variables as negative values, too. However, assembler will convert them to the corresponding positive value. For example: If you assign -1 to a db variable, assembler will convert them to positive 255 integer. "How can it be? It will certainly confuse my calculation then." Nope. In fact, the converted negative values will behave similarly as if they are not flipped. Trust me. ;-) The only thing you need to beware of is just when you want to print the contents of that variable out to the screen and to distinguish the negative values from the positive ones.

    To distinguish negatives from positives, usually programmers likes to divide the variable ranges into two roughly equal parts. For bytes, if the value is between 0 and 127, it is considered as positive, the rest (128-255) are considered negative. This scheme also perpetuates in dividing words and double-words. It's not hard at all, you just remember which variables are considered negative and treat them accordingly. You may find it cumbersome at first, though.

    Now, the next question would be on how can we find the corresponding positive values for each negative numbers. Before we start, I just remind you that 1 byte equals to 8 bits. So 2 bytes is 16 bits, 4 bytes is 32 bits. I assume that you are able to convert a decimal number to binary and vice versa. I also assume that you're capable in doing binary digit addition.

    To find the corresponding positive value, you first ignore the negative sign, then convert that number into binary. Remember the variable type you are in. If it is a byte, the resulting binary number must be 8 digits. Likewise, a word must be 16 digits and a double must be 32 digits. If the result digit is less than that, pad it with zeroes. Then, flip all digits in the binary number (i.e. from 0 to 1 or from 1 to 0). After that, increase that binary by one. Convert the result back to decimal. Voila! That's the corresponding positive value.

    For example, you want to convert -5 byte to its corresponding positive value. Ignore the negative and convert 5 to binary. It's 101, right? Since we're dealing with bytes, we must have 8 digits. The result 101 is just 3 digits, so we must pad it with zeroes. Therefore, we now have 00000101. Then, we flip the digits from 0 to 1 or 1 to 0. So, we now have 11111010. The next step is to increase that number by 1: 11111010 + 1 = 11111011. Then we convert this number back to decimal: 251. Ta da! So, -5 is 251 in positive representation.

    Hmm, if you find that this calculation is cumbersome... uh... Well, you have to live with that if you'd like to learn assembly. Moreover, you need to be familiar with hexadecimal numbers too. You would need to learn some converting operations and do some arithmetic between decimal, binary and hexadecimal. If you're kinda awkward, you can always employ calculators. If you do it over and over again, you probably do the calculations by heart quickly (and amaze your friends ;-).
    from this question, I learned a thing or two myself, and I hope this helped you also.


  4. #4
    Senior Member Unl3Ashed's Avatar
    Join Date
    Aug 2002
    I've search for you in google:

    Integer numbers are represented as either words or pairs of words, depending on your processor's word size. One 32-bit machine word is the most common integer representation.

    Integer arithmetic is close to but not actually mathematical base-two. The low-order bit is 1, next 2, then 4 and so forth as in pure binary. But signed numbers are represented in twos-complement notation. The highest-order bit is a sign bit which makes the quantity negative, and every negative number can be obtained from the corresponding positive value by inverting all the bits and adding one. This is why integers on a 32-bit machine have the range -2^31 to 2^31 - 1 1 (where ^ is the `power' operation, 2^3 = 8). That 32nd bit is being used for sign.

    Some computer languages give you access to unsigned arithmetic which is straight base 2 with zero and positive numbers only.

    Most processors and some languages can do operations in floating-point numbers (this capability is built into all recent processor chips). Floating-point numbers give you a much wider range of values than integers and let you express fractions. The ways in which this is done vary and are rather too complicated to discuss in detail here, but the general idea is much like so-called `scientific notation', where one might write (say) 1.234 * 10^23; the encoding of the number is split into a mantissa (1.234) and the exponent part (23) for the power-of-ten multiplier (which means the number multiplied out would have 20 zeros on it, 23 minus the three decimal places).

    And you can also check:
    How does the computer store negative numbers ? (pdf)

    Storing Numbers on a Computer

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts