question binary representations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    question binary representations

    im doing conversions with two's complement binary numbers and i have some questions.

    how can I convert -2047 into a base 2 complement number? I know how to read the binary but there has to be a way to convert it I just don't know how.

    I also was trying to figure out how to convert MIPS binary representation for the floating-point number 0.1.


    also i solved these and wanted to make sure I was understanding the concepts.
    if this is the binary
    Code:
    1111 1111 1111 1111 1111 1111 0000 0110
    then the number represented is -248

    im also doing adding and subtracting
    if have two numbers
    Code:
    0000 0000 0000 0000 0000 0000 0101 1011
    0000 0000 0000 0000 0000 0000 0000 1101
    then the added number in binary would be
    Code:
    0000 000 0000 0000 0000 0000 0110 0000
    using the same numbers but using the negative of the second one from above, then adding the two
    Code:
    0000 0000 0000 0000 0000 0000 0101 1011
    1111 1111 1111 1111 1111 1111 1111 0010
    with these two numbers I get
    Code:
    0000 0000 0000 0000 0000 0000 0100 1110
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by gdarian216
    how can I convert -2047 into a base 2 complement number? I know how to read the binary but there has to be a way to convert it I just don't know how.

    I also was trying to figure out how to convert MIPS binary representation for the floating-point number 0.1.
    Store the number in a variable, the read the bits out of of the variable (if it's an integer) or read the bits from it's memory locations (it it's a float).

    Originally posted by gdarian216
    Code:
    1111 1111 1111 1111 1111 1111 0000 0110
    then the number represented is -248
    No it repreents -250

    [QUOTE=gdarian21 6]
    im also doing adding and subtracting
    if have two numbers
    Code:
    0000 0000 0000 0000 0000 0000 0101 1011
    0000 0000 0000 0000 0000 0000 0000 1101
    then the added number in binary would be
    Code:
    0000 000 0000 0000 0000 0000 0110 0000
    [/code]Wrong it would be

    Code:
    0000 000 0000 0000 0000 0000 0110 1000


    OK the thing to remember is that your computer only uses a binary represenation of any number, usually 2's complement for an integer. To your computer -2047 is just a series of 5 characters, it does not understand or use decimal. What it knows is how to convert it's binary representation into a decimal one when displaying data for the user. The compiler also knows how to convert decimal into binary when it is compiling a program.

    As such reading the binary representation of -2047 is trivially easy, assign the value to a variable

    [code=c]
    int value = -2047;
    [/code]

    Then use the shift and bitwise operators to read out a string of binary digits from value.

    Comment

    • lucky26ph
      New Member
      • Oct 2007
      • 3

      #3
      caN U HELP ME WITH THIS..input an integer containing only 0 and 1 binary number and prints its decimal equivalent..hel p give me to solve this problem give the codes on this problem..tnx..

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by luck26ph
        caN U HELP ME WITH THIS..input an integer containing only 0 and 1 binary number and prints its decimal equivalent..hel p give me to solve this problem give the codes on this problem..tnx..
        Please start your own thread. Hijacking someone else's is rude.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by gdarian216
          how can I convert -2047 into a base 2 complement number? I know how to read the binary but there has to be a way to convert it I just don't know how.
          You:
          1) Start with 2047
          2) reverse 1's and 0's
          3) add 1
          4) voila!

          If you start with -2047 in 2's complement then the abve will covert it back to 2047.

          Comment

          • gdarian216
            New Member
            • Oct 2006
            • 57

            #6
            Originally posted by Banfa
            Store the number in a variable, the read the bits out of of the variable (if it's an integer) or read the bits from it's memory locations (it it's a float)
            how would the bitwise function help.........i wrote a short program

            Code:
            #include<iostream>
            using namespace std;
            
            int main()
            {
            
            float i = 0.0;
            i = 0.1;
            cout << &i;
            
            }
            and i get 0xbfe392f4-

            how would i get the bit value?

            Comment

            Working...