none
How to find 2's compliment for decimal numbers

    Question

  • Hi

    How  to convert decimal number into 2's compliment and 9's compliment 


    karthik

    Wednesday, January 22, 2014 8:47 AM

All replies

  • I have a problem

    I am unable to find 2's compliment from decimal

    Here Is my Code


     int intVal = int.Parse(tb.Text.ToString());   
                SByte wqwe=1;// intVal == 65280
                Int16 twosComp = Convert.ToInt16(~intVal + wqwe);
    
                string binary = Convert.ToString(twosComp, 2);
                tb.Text = binary;
    Kindly Help Me


    karthik

    Wednesday, February 05, 2014 9:14 AM
  • You're truncating your int32 to an int16.  You also don't need to call convert as the number is already an int32.  What algorithm are you using?
    Wednesday, February 05, 2014 3:36 PM
  • Here is my code
               
                int16 twosComp = ~intVal + 1;         
    In this code i got an negative sign based values  like i entered +5 but i got an -5 value so that i try to represent signed 16 bit integer .I not used any  algorithm


    karthik


    • Edited by karthik sin Thursday, February 06, 2014 1:44 PM
    Thursday, February 06, 2014 1:43 PM
  • When you do 2's compilment on a base 10 number it is simply the negative of the number just like you got.  Maybe you want to output the hex value of the number.  This may be a trick question.  because 9's compliment would also be the same results if you displayed the output in base 10.  Doing a compliment of a number requires sign extension of the sign bit (MSB).  The results may be different depedning on the size of the memory you are doing.   For example -1 in int8 ix 0xFF, but then if you casst it to a int16 you get 0x00FF which isn't -1.  It is +255. 

    jdweng

    Thursday, February 06, 2014 2:01 PM
  • thanks

    I got your  point. do you have an idea for 9's compliment actually i am using this code

      try
                {
                    int ab = int.Parse(tb.Text);
                    int sum = 0;
                    tb.Text = "";
                    while (ab != 0)
                    {
                        sum = ab % 10;
                        int sum1 = 9-sum;
                        string binary = Convert.ToString(sum1, 2);
                        tb.Text += binary;
                        ab /= 10;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    Is there any   simple idea for 9's compliment


    karthik

    Thursday, February 06, 2014 2:20 PM
  • Nine's compliment means base 9 where the largest number is 0x1000. So the inverse of each digit is the following

    0 8

    1 7

    2 6

    3 5

    4 4

    5 3

    6 2

    7 1

    8 0


    jdweng

    Thursday, February 06, 2014 2:39 PM
  • can you explain Briefly ,  i cant understand are you try to creating a new user defined  base type

    as well as base 9 not in the list(Convert.ToString()).


    karthik

    Thursday, February 06, 2014 7:19 PM
  • Two's compliment compliment is for binary where you have 1's and 0's.  You invert each bit.  When you are refering to 9's compliment you are refering to base nine.  So the 1's compliment of any number is the the following

    1's compliment  = (Base - 1 ) - X where X is each digit in the base.  So base nine the digits are 0 to 8. 

    The following table is the 1's compliment

    X          1's compliment     2's compliment

    0             8                          10   (base 9)

    1             7                            8

    2             6                            7

    3             5                            6

    4             4                            5

    5             3                            4

    6             2                            3

    7             1                            2

    8             0                            1      


    jdweng

    Thursday, February 06, 2014 8:56 PM