Answered by:
How to find 2's compliment for decimal numbers
Question
Answers

The algorithms are available online. Find the version you like best and convert it to C#. Here's some helper links
http://sandbox.mc.edu/~bennet/cs110/tc/dtotc.html
http://www.exploringbinary.com/twoscomplementconverter/
http://stackoverflow.com/questions/14929072/expressinghexvaluein2scomplement
http://social.msdn.microsoft.com/Forums/enUS/47f5ebc1635a4fbe974588a02f7b4fa9/2scomplementchallenge
http://www.electrical4u.com/9scomplementand10scomplement/ Marked as answer by Herro wongMicrosoft contingent staff, Moderator Wednesday, February 05, 2014 3:30 AM
All replies

The algorithms are available online. Find the version you like best and convert it to C#. Here's some helper links
http://sandbox.mc.edu/~bennet/cs110/tc/dtotc.html
http://www.exploringbinary.com/twoscomplementconverter/
http://stackoverflow.com/questions/14929072/expressinghexvaluein2scomplement
http://social.msdn.microsoft.com/Forums/enUS/47f5ebc1635a4fbe974588a02f7b4fa9/2scomplementchallenge
http://www.electrical4u.com/9scomplementand10scomplement/ Marked as answer by Herro wongMicrosoft contingent staff, Moderator Wednesday, February 05, 2014 3:30 AM

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


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

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

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 = 9sum; 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



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