Hi I need some help with this code
-
/method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); //string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; It is givingme a wrong conversion from decimal to two's complement and I can figure out the reason. Any help please?
-
/method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); //string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; It is givingme a wrong conversion from decimal to two's complement and I can figure out the reason. Any help please?
What are you trying to do ? Where is bitstring defined ? Obviously, it's not reset in this function, so whatever value it had, 2 is added to every element. I don't see how you remotely are interacting with the decimal, except as an index into an array. So, if your array has 8 elements, as it seems, a number > 8 will just crash it, right ?
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
What are you trying to do ? Where is bitstring defined ? Obviously, it's not reset in this function, so whatever value it had, 2 is added to every element. I don't see how you remotely are interacting with the decimal, except as an index into an array. So, if your array has 8 elements, as it seems, a number > 8 will just crash it, right ?
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
This is where I started from, thanks. ............................................................. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TwosComplement { public partial class Form1 : Form { public Form1() { InitializeComponent(); for (int i = 0; i < 8; i++) { bitstring[i] = 0;} twoc_output.Text = "enter an integer"; decimal_output.Text = "enter a length 8 bitstring"; } private int[] bitstring = new int[8]; private int nDecimal; //strings for reading the bitstring in and out private string instring, outstring; private string nextbit; //helper function for flipping bits in the bitstring private int negate(int x) { if (x == 1) return 0; return 1; } //method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; } } }
-
This is where I started from, thanks. ............................................................. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TwosComplement { public partial class Form1 : Form { public Form1() { InitializeComponent(); for (int i = 0; i < 8; i++) { bitstring[i] = 0;} twoc_output.Text = "enter an integer"; decimal_output.Text = "enter a length 8 bitstring"; } private int[] bitstring = new int[8]; private int nDecimal; //strings for reading the bitstring in and out private string instring, outstring; private string nextbit; //helper function for flipping bits in the bitstring private int negate(int x) { if (x == 1) return 0; return 1; } //method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; } } }
OK, so your first issue is that the code, when it works, will only work once. Because you only reset the array once. Or is it meant to be cumulative ?
Angelinna wrote:
private int negate(int x) { if (x == 1) return 0; return 1; }
Not sure that you call this, but is it really worth writing a function for ? How about just writing this: return 1 - x; and 1-x is something you can just code inline instead of making a call, surely ? I'd have expected you'd need to use the & operator to work out what bits are set in the original number, what you're doing here seems to me to be doomed to fail. bitstring[nDecimal] = carry; This will just crash if nDecimal > 7 and won't ever do anything useful.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
OK, so your first issue is that the code, when it works, will only work once. Because you only reset the array once. Or is it meant to be cumulative ?
Angelinna wrote:
private int negate(int x) { if (x == 1) return 0; return 1; }
Not sure that you call this, but is it really worth writing a function for ? How about just writing this: return 1 - x; and 1-x is something you can just code inline instead of making a call, surely ? I'd have expected you'd need to use the & operator to work out what bits are set in the original number, what you're doing here seems to me to be doomed to fail. bitstring[nDecimal] = carry; This will just crash if nDecimal > 7 and won't ever do anything useful.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
Ya , not really sure of what am doing. Would like the code to work for both positive and negative integers and should take care of any "out of range" errors....... Otherwise would appreciate tips or web where I may read more on this. Thanks.
http://en.wikipedia.org/wiki/Two's_complement[^] reads to me like a two's complement is the same as !x + 1.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
http://en.wikipedia.org/wiki/Two's_complement[^] reads to me like a two's complement is the same as !x + 1.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
Christian Graus wrote:
reads to me like a two's complement is the same as !x + 1.
Signed numbers are stored as two's complement, so the easiest way to get it is to cast a signed number to an unsigned number:
int negAnswer = -42; uint twosComplement = (uint)negAnswer;
Despite everything, the person most likely to be fooling you next is yourself.