Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Binary number.

  1. Apr 9, 2012 #1
    how can you tell if a binary number is negative or positive?

    1111 <--- how do you know if this is 15 or -1?

    and for my project i have to make a 4 bit subtracter using LED lights to show 1's or 0's. Do I just leave it in 2's complement form?
     
  2. jcsd
  3. Apr 9, 2012 #2

    davenn

    User Avatar
    Science Advisor
    Gold Member

    hi there

    thats an interesting Q and not one I cannot answer, havent thought about that before
    I will also be interested in seeing any response

    in the mean time have a look at this www page and see if you can make sense of it ....
    http://www.allaboutcircuits.com/vol_4/chpt_2/3.html

    Dave
     
  4. Apr 10, 2012 #3

    chiro

    User Avatar
    Science Advisor

    If you are able to do an operation like a multiply or divide and you can read the flags or generate an interrupt for an overflow error than that will tell you specifically if things are being treated as signed or unsigned implicitly.

    The thing it will always depend on how the circuit treats the number. In an x86 type assembly environment we have IDIV and IMUL vs DIV and MUL for signed vs unsigned division and multiplication.

    If you are using some circuit or device that is pre-made then I would just read the specifications and see how it treats the data to see if it uses signed numbers (MSB is sign) or unsigned (MSB is used to represent highest bit of actual data).

    Usually if you have a circuit that has to do a specific operation like signed add or subtract, it will provide a flag of some sort (through an output pin) to say whether there was an error. In software it's done through either a flag or an interrupt but in hardware it will be given by an output pin. If the circuit or device has no error output of this kind then I would get a circuit or device that does if you are concerned about this.
     
  5. Apr 10, 2012 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    Have you been given the specs for your 4 bit subtractor? If so, what are they?
    Are you allowed to display the answer with a negative sign prepended? That would be the most user friendly, I'd think.
     
  6. Apr 10, 2012 #5

    jim hardy

    User Avatar
    Science Advisor
    Gold Member
    2016 Award

    I claim :

    The sign of a binary number exists only in the mind of the programmer.

    For any fixed number of bits the binary system is cyclic.
    That is, it starts at zero where all bits are 0's and increments until all bits are 1's, meaning it's reached the largest number that can be represented with that many bits.
    The next increment after all 1's flips it back to starting point, all 0's.

    So - with N bits we can represent 2^N different values.
    It is usually chosen to let half those values be negative and the other half be positive.
    Further it is (i think) universally chosen to let all 0's represent zero.

    Let us take a four bit counter for a simple example.
    We'll start at zero, then increment the counter by one [STRIKE]seventeen[/STRIKE] sixteeen times, because 2^4 = sixteen and that's how many numbers we can represent with four bits.. [STRIKE]seven[/STRIKE] sixteenth increment should restart the count.
    and we want to show the cyclic nature of binary counting.

    0000 = zero
    0001 = one
    0010 = two
    0011 = three
    0100 = four
    0101 = five
    0110 = six
    0111 = seven = largest positive number that can be represented with four bits IF you want luxury if signed numbers
    1000 = -seven = largest negative number that can be represented with four bits IF you want luxury if signed numbers , or = +eight if you don't
    1001 = -six = still counting up
    1010 = -five
    1011 = -four
    1100 = -three
    1101 = -two
    1111 = -one if you want luxury of signed numbers, or = +fifteen if you don't
    0000 = zero , still counting up, but the leading 1 disappeared off left end. (In a real machine thet'd be a carry bit but we are just studying nature of a 4-bit binary system here.)
    0001 = one , and cycle starts again.

    So you see that it is convenient but not necessary to use leftmost bit for sign.
    We could as easily have used the four bits to count from zero to fifteen.

    Count those increments to make sure i got [STRIKE]seventeen[/STRIKE] sixteen of them.

    So to your question -

    if the leftmost bit is set to 1 it is USUALLY a negative number because that's the convention.
    But since you are building the machine the choice is really up to you.
    When Mother Nature gave us the binary system she left it up to us how to use it.

    And that's why chiro mentioned signed vs unsigned arithmetic.

    Is that any help?

    old jim
     
    Last edited: Apr 10, 2012
  7. Apr 10, 2012 #6
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook